Annotation of researchv10no/cmd/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:                        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.