Annotation of 42BSD/ucb/head.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

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