[BACK]Return to head.c CVS log [TXT][DIR] Up to [local] / prex / usr / bin / head

Annotation of prex/usr/bin/head/head.c, Revision 1.1.1.1

1.1       nbrk        1: /*
                      2:  * Copyright (c) 1980, 1987, 1992, 1993
                      3:  *     The Regents of the University of California.  All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. Neither the name of the University nor the names of its contributors
                     14:  *    may be used to endorse or promote products derived from this software
                     15:  *    without specific prior written permission.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  */
                     29:
                     30: #include <sys/types.h>
                     31:
                     32: #include <ctype.h>
                     33: #include <err.h>
                     34: #include <errno.h>
                     35: #include <stdio.h>
                     36: #include <stdlib.h>
                     37: #include <string.h>
                     38: #include <unistd.h>
                     39:
                     40: /*
                     41:  * head - give the first few lines of a stream or of each of a set of files
                     42:  *
                     43:  * Bill Joy UCB August 24, 1977
                     44:  */
                     45:
                     46: #ifdef CMDBOX
                     47: #define main(argc, argv)       head_main(argc, argv)
                     48: #endif
                     49:
                     50: void head(FILE *, int);
                     51: void obsolete(char *[]);
                     52: static void usage(void);
                     53:
                     54: int eval;
                     55:
                     56: int
                     57: main(int argc, char *argv[])
                     58: {
                     59:        register int ch;
                     60:        FILE *fp;
                     61:        int first, linecnt;
                     62:        char *ep;
                     63:
                     64:        obsolete(argv);
                     65:        linecnt = 10;
                     66:        while ((ch = getopt(argc, argv, "n:")) != EOF)
                     67:                switch(ch) {
                     68:                case 'n':
                     69:                        linecnt = strtol(optarg, &ep, 10);
                     70:                        if (*ep || linecnt <= 0)
                     71:                                err(1, "illegal line count -- %s", optarg);
                     72:                        break;
                     73:                case '?':
                     74:                default:
                     75:                        usage();
                     76:                }
                     77:        argc -= optind;
                     78:        argv += optind;
                     79:
                     80:        if (*argv)
                     81:                for (first = 1; *argv; ++argv) {
                     82:                        if ((fp = fopen(*argv, "r")) == NULL) {
                     83:                                err(0, "%s: %s", *argv, strerror(errno));
                     84:                                continue;
                     85:                        }
                     86:                        if (argc > 1) {
                     87:                                (void)printf("%s==> %s <==\n",
                     88:                                    first ? "" : "\n", *argv);
                     89:                                first = 0;
                     90:                        }
                     91:                        head(fp, linecnt);
                     92:                        (void)fclose(fp);
                     93:                }
                     94:        else
                     95:                head(stdin, linecnt);
                     96:        exit(eval);
                     97: }
                     98:
                     99: void
                    100: head(fp, cnt)
                    101:        FILE *fp;
                    102:        register int cnt;
                    103: {
                    104:        register int ch;
                    105:
                    106:        while (cnt--)
                    107:                while ((ch = getc(fp)) != EOF) {
                    108:                        if (putchar(ch) == EOF)
                    109:                                err(1, "stdout: %s", strerror(errno));
                    110:                        if (ch == '\n')
                    111:                                break;
                    112:                }
                    113: }
                    114:
                    115: void
                    116: obsolete(argv)
                    117:        char *argv[];
                    118: {
                    119:        char *ap;
                    120:
                    121:        while ((ap = *++argv)) {
                    122:                /* Return if "--" or not "-[0-9]*". */
                    123:                if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
                    124:                        return;
                    125:                if ((ap = malloc(strlen(*argv) + 2)) == NULL)
                    126:                        err(1, "%s", strerror(errno));
                    127:                ap[0] = '-';
                    128:                ap[1] = 'n';
                    129:                (void)strcpy(ap + 2, *argv + 1);
                    130:                *argv = ap;
                    131:        }
                    132: }
                    133:
                    134: static void
                    135: usage()
                    136: {
                    137:        (void)fprintf(stderr, "usage: head [-n lines] [file ...]\n");
                    138:        exit(1);
                    139: }

CVSweb