#ifndef lint static char *sccsid = "@(#)head.c 4.1 (Berkeley) 10/1/80"; #endif #include #include #include #include #include /* * head - give the first few lines of a stream or of each of a set of files * * Bill Joy UCB August 24, 1977 */ int linecnt = 10; char Intorev[] = "\033[7m"; char Outarev[] = "\033[0m"; int totty = 0; main(Argc, argv) register int Argc; register char *argv[]; { register int argc; char *name; static int around; char obuf[BUFSIZ]; register c; struct stat f; setbuf(stdout, obuf); totty = isatty(1); Argc--, argv++; argc = Argc; if (Argc) linecnt = 20/Argc; if (linecnt < 2) linecnt = 2; do { while (argc > 0 && argv[0][0] == '-') { linecnt = getnum(argv[0] + 1); argc--, argv++, Argc--; } if (argc == 0 && around) break; if (argc > 0) { close(0); if (freopen(argv[0], "r", stdin) == NULL) { perror(argv[0]); exit(1); } name = argv[0]; argc--, argv++; fstat(fileno(stdin), &f); c = f.st_mode & S_IFMT; if (c == S_IFDIR) continue; } else name = 0; /* if (around && !totty) putchar('\n'); */ around++; if (Argc > 1 && name) { if (totty) printf("%s%s%s ", Intorev, name, Outarev); else printf("%s ", name); } copyout(linecnt); fflush(stdout); } while (argc > 0); } copyout(cnt) register int cnt; { char lbuf[BUFSIZ]; int l; int lines = 0; while (cnt > 0 && fgets(lbuf, sizeof lbuf, stdin) != 0) { printf("%s", lbuf); l = strlen(lbuf); if ((l > 1) && iscntrl(lbuf[l-1])) ++lines; fflush(stdout); cnt--; } if (lines == 0) putchar('\n'); } getnum(cp) register char *cp; { register int i; for (i = 0; *cp >= '0' && *cp <= '9'; cp++) i *= 10, i += *cp - '0'; if (*cp) { fprintf(stderr, "Badly formed number\n"); exit(1); } return (i); }