GNO-Extras/bobbi/nl.c#b00008
2020-02-09 19:48:30 -05:00

41 lines
659 B
Plaintext

#include <stdio.h>
#include <stdlib.h>
void usage(void) {
fputs("usage: nl [filename]\n", stderr);
}
int main(int argc, char *argv[]) {
if (argc > 2) {
usage();
exit(2);
}
FILE *in;
if (argc == 2) {
in = fopen(argv[1], "r");
if (!in) {
fprintf(stderr, "nl: cannot open '%s'\n", argv[1]);
exit(1);
}
}
else
in = stdin;
int line = 1;
int flag = 0;
printf("%5d ", line++);
while (!feof(in)) {
unsigned char c = fgetc(in);
if (c == '\n' || c == '\r')
flag = 1;
else if (c != 0xff) {
if (flag)
printf("\n%5d ", line++);
flag = 0;
putchar(c);
}
}
putchar('\n');
}