1: #ifndef lint
   2: static char *sccsid = "@(#)head.c	4.1 (Berkeley) 10/1/80";
   3: #endif
   4: #include <stdio.h>
   5: /*
   6:  * head - give the first few lines of a stream or of each of a set of files
   7:  *
   8:  * Bill Joy UCB August 24, 1977
   9:  */
  10: 
  11: int linecnt = 10;
  12: 
  13: main(Argc, argv)
  14:     register int Argc;
  15:     register char *argv[];
  16: {
  17:     register int argc;
  18:     char *name;
  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:     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: }

Defined functions

copyout defined in line 52; used 1 times
  • in line 47
getnum defined in line 64; used 1 times
  • in line 27
main defined in line 13; never used

Defined variables

linecnt defined in line 11; used 2 times
sccsid defined in line 2; never used
Last modified: 1982-09-04
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 683
Valid CSS Valid XHTML 1.0 Strict