File: cachedel.c

package info (click to toggle)
nocache 0.9-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 164 kB
  • ctags: 70
  • sloc: ansic: 512; makefile: 74; sh: 26
file content (53 lines) | stat: -rw-r--r-- 1,144 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int exiterr(const char *s)
{
    perror(s);
    exit(-1);
}

int main(int argc, char *argv[])
{
    int i, n = 1;
    int fd;
    char *fn;
    struct stat st;

    if(argc == 4 && !strcmp("-n", argv[1])) {
            n = atoi(argv[2]);
            fn = argv[3];
    } else if(argc != 2) {
        fprintf(stderr, "usage: %s [-n <n>] <file> "
            "-- call fadvise(DONTNEED) <n> times on file", argv[0]);
        exit(1);
    } else {
        fn = argv[1];
    }

    fd = open(fn, O_RDONLY);
    if(fd == -1)
        exiterr("open");

    if(fstat(fd, &st) == -1)
        exiterr("fstat");
    if(!S_ISREG(st.st_mode)) {
        fprintf(stderr, "%s: S_ISREG: not a regular file", fn);
        return EXIT_FAILURE;
    }
    if(st.st_size == 0) {
        fprintf(stderr, "%s: file size is 0!\n", fn);
        return EXIT_FAILURE;
    }

    for(i = 0; i < n; i++)
        if(posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) == -1)
            exiterr("posix_fadvise");

    return EXIT_SUCCESS;
}