/* head.c -- display first lines of file */ /* $Id$ */ /* Carlos Duarte, 960612 */ #include #include #include void usage(void) { fprintf(stderr, "usage: head [-c chars] [-n lines] [-lines] [-qv] [files...]\n"); exit(1); } void head(char *fn, int lines) { int c; FILE *f; if (fn == 0 || strcmp(fn, "-") == 0) f = stdin; else if ((f = fopen(fn, "r")) == NULL) { perror(fn); return; } if (lines < 0) { int n = -lines; while (n-- && (c = getc(f)) != EOF) putchar(c); } else { int n = lines; if (n) do { if ((c = getc(f)) == EOF) break; putchar(c); } while (c != '\n' || --n > 0); } if (f != stdin) fclose(f); } void print_header(char *fn) { if (fn == NULL || strcmp(fn, "-") == 0) printf("==> standard input <==\n"); else printf("==> %s <==\n", fn); } #define V_AUTO 0 #define V_ALL 1 #define V_NEVER 2 int main(int ac, char *av[]) { int i; char *arg; int headers; int lines; headers = V_AUTO; lines = 10; for (i=1; i