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