summaryrefslogtreecommitdiffstats
path: root/src/readahead.c
blob: 9d4478829ae569ea1f33305d8d58620ae4399bb5 (plain)
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
54
55
56
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sysexits.h>

static const char *program = "readahead";

void usage(void) {
	printf("usage: %s [-hv] FILE [...]\n", program);
	exit(EX_USAGE);
}

int main( int argc, char *argv[]) {
	int c, verbose=0;

	/* parse options */
	while ( (c = getopt(argc, argv, "hv")) >= 0 ) {
		switch (c) {
		case 'v':	verbose++;
				break;
		case 'h':
		default:	usage();
				break;
		}	
	}
	
	/* check that at least one file is specified */
	if (optind == argc)
		usage();
	
	/* parse files */
	c = EX_OK;
	while (optind < argc) {
		struct stat st;
		FILE *f = fopen(argv[optind], "r");


		/* check that file exists */
		if (f == NULL) {
			perror(argv[optind]);
			c = EX_NOINPUT;
		} else {
			stat(argv[optind], &st);
			readahead( fileno(f), 0, (size_t)st.st_size );
			if (verbose) 
				printf("%s\n", argv[optind]);
			fclose(f);
		}
		optind++;
	}
	return (c);
}