Debian Codegraph @

benchmarks.larceny/src/tail.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 int main(int argc, char *argv[])
6 {
7 int i, j;
8 char buf[5000];
9 char **lp = (char **) malloc(100 * sizeof(char *));
10 int nbuf = 100;
11 FILE *infp;
12 FILE *outfp;
13 char *infile = "../../src/bib";
14 char *outfile = "foo";
15 if (argc > 1)
16 infile = argv[1];
17 if (argc > 2)
18 outfile = argv[2];
19 infp = fopen(infile, "r");
20 if (infp == NULL) {
21 fprintf(stderr, "can't open %s\n", infile);
22 exit(1);
23 }
24 outfp = fopen(outfile, "w");
25 for (i = 0; fgets(buf, sizeof buf, infp) != NULL; i++) {
26 if (i >= nbuf) {
27 nbuf *= 2;
28 lp = (char **) realloc(lp, nbuf * (sizeof(char *)));
29 }
30 lp[i] = strdup(buf);
31 }
32 for (j = i-1; j >= 0; j--)
33 fprintf(outfp, "%s", lp[j]);
34 return 0;
35 }