/* * our_head.c -- a C program that prints the first L lines of its input, * where L defaults to 10 but can be specified by the caller of the * program. * * (This program is inefficient and does not check its error * conditions. It is meant to illustrate filters.) */ #include #include #include int main(int argc, char** argv) { int i = 0; int nlines; char ch; int ret; if (argc == 2) { nlines = atoi(argv[1]); } else if (argc == 1) { nlines = 10; } else { fprintf(stderr, "usage: our_head [nlines]\n"); exit(1); } for (i = 0; i < nlines; i++) { do { /* read in the first character from fd 0 */ ret = read(0, &ch, 1); /* if there are no more characters to read, then exit */ if (ret == 0) exit(0); write(1, &ch, 1); } while (ch != '\n'); } exit(0); }