Michael Opdenacker contributed a readahead applet.
[oweals/busybox.git] / miscutils / readahead.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * readahead implementation for busybox
4  *
5  * Preloads the given files in RAM, to reduce access time.
6  * Does this by calling the readahead(2) system call.
7  *
8  * Copyright (C) 2006  Michael Opdenacker <michael@free-electrons.com>
9  *
10  * Licensed under GPLv2 or later, see file License in this tarball for details.
11  */
12
13 #include "busybox.h"
14
15 int readahead_main(int argc, char **argv)
16 {
17         FILE *f;
18         struct stat stat_buf;
19         int retval = EXIT_SUCCESS;
20
21         if (argc == 1) bb_show_usage();
22
23         while (*++argv) {
24                 if ((f = bb_wfopen(*argv, "r")) != NULL) {
25                         int r, fd=fileno(f);
26                         
27                         xstat(*argv, &stat_buf);
28                         r = readahead(fd, 0, fdlength(fd));
29                         fclose(f);
30                         if (r >= 0) continue;
31                 }
32                 retval = EXIT_FAILURE;
33         }
34
35         return retval;
36 }