/* head.c - print the first few lines in a file. If count is specified,* * that many lines are printed instead of the default value of * * ten lines. If no files are specified, head reads standard * * input. * * * * head [-] [ ...] * * * * head (C) 1988 by Gary L. Brant * * * * :ts=8 */ #include #define MAXLINE 1000 void fputs (); int head = 0; main (argc, argv) /* list 1st n lines of a file */ int argc; char *argv[]; { FILE *input, *fopen (); void fclose (); int default_lines = 10; /* default number of lines to list */ int i = 0, j, maxlines; char ch; maxlines = default_lines; while (++i < argc) { if (argv [i][0] == '-') { /* remember to bump i in loop */ maxlines = 0; /* remember to set maxlines to 0 1st */ for (j = 1; (ch = argv[i][j]) != '\0'; j++) if (ch >= '0' && ch <= '9') { maxlines *= 10; maxlines += ch - '0'; } else { maxlines = default_lines; break; } default_lines = maxlines; } else { ++head; if ((input = fopen (argv[i], "r")) == NULL) { fputs ("head: can't open ", stderr); fputs (argv[i], stderr); putc ('\n', stderr); break; } else { list (input, argv[i], maxlines); fclose (input); } } } if (!head) list (stdin, "", maxlines); } /* list head of a file */ list (fp, fn, maxlines) FILE *fp; char *fn; int maxlines; { int n; int i = 0; char line[MAXLINE]; char *fgets (); while ((i < maxlines) && (fgets (line, MAXLINE, fp)) != NULL) { if (i++ == 0) heading (fn); fputs (line, stdout); } } /* heading - print a short heading identifying file */ heading (filename) char *filename; { if (head) { fputs ("==> ", stdout); fputs (filename, stdout); fputs (" <==\n", stdout); } } _wb_parse () { }