Annotation of 3BSD/cmd/head.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: /*
                      3:  * head - give the first few lines of a stream or of each of a set of files
                      4:  *
                      5:  * Bill Joy UCB August 24, 1977
                      6:  */
                      7: 
                      8: int    linecnt = 10;
                      9: int    argc;
                     10: 
                     11: main(Argc, argv)
                     12:        int Argc;
                     13:        char *argv[];
                     14: {
                     15:        register int argc;
                     16:        char *name;
                     17:        register char *argp;
                     18:        static int around;
                     19:        char obuf[BUFSIZ];
                     20: 
                     21:        setbuf(stdout, obuf);
                     22:        Argc--, argv++;
                     23:        argc = Argc;
                     24:        do {
                     25:                while (argc > 0 && argv[0][0] == '-') {
                     26:                        linecnt = getnum(argv[0] + 1);
                     27:                        argc--, argv++, Argc--;
                     28:                }
                     29:                if (argc == 0 && around)
                     30:                        break;
                     31:                if (argc > 0) {
                     32:                        close(0);
                     33:                        if (freopen(argv[0], "r", stdin) == NULL) {
                     34:                                perror(argv[0]);
                     35:                                exit(1);
                     36:                        }
                     37:                        name = argv[0];
                     38:                        argc--, argv++;
                     39:                } else
                     40:                        name = 0;
                     41:                if (around)
                     42:                        putchar('\n');
                     43:                around++;
                     44:                if (Argc > 1 && name)
                     45:                        printf("==> %s <==\n", name);
                     46:                copyout(linecnt);
                     47:                fflush(stdout);
                     48:        } while (argc > 0);
                     49: }
                     50: 
                     51: copyout(cnt)
                     52:        register int cnt;
                     53: {
                     54:        register int c;
                     55:        char lbuf[BUFSIZ];
                     56: 
                     57:        while (cnt > 0 && fgets(lbuf, sizeof lbuf, stdin) != 0) {
                     58:                printf("%s", lbuf);
                     59:                fflush(stdout);
                     60:                cnt--;
                     61:        }
                     62: }
                     63: 
                     64: getnum(cp)
                     65:        register char *cp;
                     66: {
                     67:        register int i;
                     68: 
                     69:        for (i = 0; *cp >= '0' && *cp <= '9'; cp++)
                     70:                i *= 10, i += *cp - '0';
                     71:        if (*cp) {
                     72:                fprintf(stderr, "Badly formed number\n");
                     73:                exit(1);
                     74:        }
                     75:        return (i);
                     76: }

unix.superglobalmegacorp.com

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