Added simple nl (number lines) utility

This commit is contained in:
Bobbi Webber-Manners 2020-02-09 19:48:30 -05:00
parent 4dffe8a31b
commit a5742b8e10
3 changed files with 51 additions and 8 deletions

View File

@ -1,8 +0,0 @@
all: sortdir
sortdir: sortdir.c
occ -w -o sortdir sortdir.c
install: sortdir
cp sortdir /usr/local/bin

11
bobbi/Makefile#000000 Normal file
View File

@ -0,0 +1,11 @@
all: nl sortdir
nl: nl.c
occ -w -o nl nl.c
sortdir: sortdir.c
occ -w -o sortdir sortdir.c
install: sortdir nl
cp nl sortdir /usr/local/bin

40
bobbi/nl.c#b00008 Normal file
View File

@ -0,0 +1,40 @@
#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');
}