mirror of
https://github.com/bobbimanners/GNO-Extras.git
synced 2024-10-31 13:07:44 +00:00
Added simple nl (number lines) utility
This commit is contained in:
parent
4dffe8a31b
commit
a5742b8e10
@ -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
11
bobbi/Makefile#000000
Normal 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
40
bobbi/nl.c#b00008
Normal 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');
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user