view tests/read_big_file.c @ 6:e2b88f50e136

cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
author Louis Opter <louis@dotcloud.com>
date Sat, 01 Jan 2011 16:18:17 +0100
parents 6ce4443e7545
children
line wrap: on
line source

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include <err.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "lxcstats.h"
#include "_lxcstats.h"

int
main(void)
{
    char        *reference;
    struct stat sb;
    char        *trial;
    size_t      size;
    int         fd;

    fd = open("/etc/services", O_RDONLY);
    if (fd == -1)
        err(EXIT_FAILURE, "can't open /etc/services");

    size = _lxcst_read_file("/etc/services", &trial);
    if (!trial)
        err(EXIT_FAILURE, "can't read /etc/services");

    if (fstat(fd, &sb) != 0)
        err(EXIT_FAILURE, "can't stat /etc/services");

    reference = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (!reference)
        err(EXIT_FAILURE, "can't map /etc/services");

    if ((size_t)sb.st_size != size)
        errx(EXIT_FAILURE, "sizes are different");

    if (memcmp(reference, trial, size))
        errx(EXIT_FAILURE, "buffers are different");

    free(trial);

    return (EXIT_SUCCESS);
}