Annotation of 43BSDReno/usr.bin/head/head.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1980, 1987 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted
                      6:  * provided that: (1) source distributions retain this entire copyright
                      7:  * notice and comment, and (2) distributions including binaries display
                      8:  * the following acknowledgement:  ``This product includes software
                      9:  * developed by the University of California, Berkeley and its contributors''
                     10:  * in the documentation or other materials provided with the distribution
                     11:  * and in all advertising materials mentioning features or use of this
                     12:  * software. Neither the name of the University nor the names of its
                     13:  * contributors may be used to endorse or promote products derived
                     14:  * from this software without specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: char copyright[] =
                     22: "@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
                     23:  All rights reserved.\n";
                     24: #endif /* not lint */
                     25: 
                     26: #ifndef lint
                     27: static char sccsid[] = "@(#)head.c     5.5 (Berkeley) 6/1/90";
                     28: #endif /* not lint */
                     29: 
                     30: #include <stdio.h>
                     31: #include <ctype.h>
                     32: /*
                     33:  * head - give the first few lines of a stream or of each of a set of files
                     34:  *
                     35:  * Bill Joy UCB August 24, 1977
                     36:  */
                     37: 
                     38: main(argc, argv)
                     39:        int     argc;
                     40:        char    **argv;
                     41: {
                     42:        register int    ch, cnt;
                     43:        int     firsttime, linecnt = 10;
                     44: 
                     45:        if (argc > 1 && argv[1][0] == '-') {
                     46:                if (!isdigit(argv[1][1])) {
                     47:                        fprintf(stderr, "head: illegal option -- %c\n", argv[1][1]);
                     48:                        goto usage;
                     49:                }
                     50:                if ((linecnt = atoi(argv[1] + 1)) < 0) {
                     51: usage:                 fputs("usage: head [-line_count] [file ...]\n", stderr);
                     52:                        exit(1);
                     53:                }
                     54:                --argc; ++argv;
                     55:        }
                     56:        /* setlinebuf(stdout); */
                     57:        for (firsttime = 1, --argc, ++argv;; firsttime = 0) {
                     58:                if (!*argv) {
                     59:                        if (!firsttime)
                     60:                                exit(0);
                     61:                }
                     62:                else {
                     63:                        if (!freopen(*argv, "r", stdin)) {
                     64:                                fprintf(stderr, "head: can't read %s.\n", *argv);
                     65:                                exit(1);
                     66:                        }
                     67:                        if (argc > 1) {
                     68:                                if (!firsttime)
                     69:                                        putchar('\n');
                     70:                                printf("==> %s <==\n", *argv);
                     71:                        }
                     72:                        ++argv;
                     73:                }
                     74:                for (cnt = linecnt; cnt; --cnt)
                     75:                        while ((ch = getchar()) != EOF)
                     76:                                if (putchar(ch) == '\n')
                     77:                                        break;
                     78:        }
                     79:        /*NOTREACHED*/
                     80: }

unix.superglobalmegacorp.com

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