Annotation of coherent/d/usr/src/examples/fseek.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: 
                      3: extern long atol();
                      4: void fatal();
                      5: 
                      6: main(argc, argv)
                      7: int argc; char *argv[];
                      8: {
                      9:        register FILE *ifp;
                     10:        register int c;
                     11:        long nchars, size;
                     12: 
                     13:        if (argc < 2 || argc > 3)
                     14:                fatal("Usage: fseek file [ nchars ]");
                     15:        nchars = (argc == 3) ? atol(argv[2]) : 100L;
                     16:        if ((ifp = fopen(argv[1], "r")) == NULL)
                     17:                fatal("cannot open input file");
                     18:        /* Seek to end */
                     19:        if (fseek(ifp, 0L, 2) == -1)
                     20:                fatal("seek error");
                     21:        /* Find current size */
                     22:        size = ftell(ifp);
                     23:        size = (size < nchars) ? 0L : size - nchars;
                     24: 
                     25:        /* Seek to point */
                     26:        if (fseek(ifp, size, 0) == -1)
                     27:                fatal("seek error");
                     28:        while ((c = getc(ifp)) != EOF)
                     29:                /* Copy rest to stdout */
                     30:                putchar(c);
                     31:        if (fclose(ifp) == EOF)
                     32:                fatal("cannot close");
                     33:        exit(0);
                     34: }
                     35: 
                     36: void fatal(message)
                     37: char *message;
                     38: {
                     39:        fprintf(stderr, "fseek: %s\n", message);
                     40:        exit(1);
                     41: }
                     42: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.