aboutsummaryrefslogtreecommitdiffstats
path: root/c/head.c
blob: 952d4c504537c41bf2920bf6a880df38ae82febf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "gbos.h"
/* head [-n N] [N]: print the first N lines of stdin (default 10). */
void main(void) {
    char *argv[8];
    unsigned char argc = argv_parse(argv, 8);
    char *nv = optval(argv, argc, 'n');
    unsigned char n = 10, lines = 0;
    char c;
    if (nv) n = atou(nv);
    else if (argc > 0 && argv[0][0] >= '0' && argv[0][0] <= '9') n = atou(argv[0]);
    for (;;) {
        c = readc();
        if (c == EOF) break;
        if (lines < n) putc(c);
        if (c == '\n') lines++;
    }
}