[hackers] [sbase] Respect exit status in nl(1) + some stylistic changes || sin
commit 86c88d8b3aa0e8766775d7bae53a47498fb7b61c
Author: sin <sin_AT_2f30.org>
Date: Thu Dec 4 12:00:19 2014 +0000
Respect exit status in nl(1) + some stylistic changes
diff --git a/nl.c b/nl.c
index 17e4177..f21e133 100644
--- a/nl.c
+++ b/nl.c
_AT_@ -8,7 +8,7 @@
#include "text.h"
#include "util.h"
-static void nl(FILE *);
+static void nl(const char *, FILE *);
static char mode = 't';
static const char *sep = "\t";
_AT_@ -27,16 +27,16 @@ main(int argc, char *argv[])
{
FILE *fp;
char *r;
+ int ret = 0;
ARGBEGIN {
case 'b':
r = EARGF(usage());
mode = r[0];
- if (r[0] == 'p') {
+ if (r[0] == 'p')
eregcomp(&preg, &r[1], REG_NOSUB);
- } else if (!strchr("ant", mode)) {
+ else if (!strchr("ant", mode))
usage();
- }
break;
case 'i':
incr = estrtol(EARGF(usage()), 0);
_AT_@ -49,21 +49,21 @@ main(int argc, char *argv[])
} ARGEND;
if (argc == 0) {
- nl(stdin);
+ nl("<stdin>", stdin);
} else for (; argc > 0; argc--, argv++) {
if (!(fp = fopen(argv[0], "r"))) {
weprintf("fopen %s:", argv[0]);
+ ret = 1;
continue;
}
- nl(fp);
+ nl(argv[0], fp);
fclose(fp);
}
-
- return 0;
+ return ret;
}
void
-nl(FILE *fp)
+nl(const char *name, FILE *fp)
{
char *buf = NULL;
long n = 0;
_AT_@ -71,13 +71,13 @@ nl(FILE *fp)
while (getline(&buf, &size, fp) != -1) {
if ((mode == 'a')
- || (mode == 'p'
- && !regexec(&preg, buf, 0, NULL, 0))
- || (mode == 't' && buf[0] != '\n')) {
+ || (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))
+ || (mode == 't' && buf[0] != '\n'))
printf("%6ld%s%s", n += incr, sep, buf);
- } else {
+ else
printf(" %s", buf);
- }
}
free(buf);
+ if (ferror(fp))
+ eprintf("%s: read error:", name);
}
Received on Thu Dec 04 2014 - 13:00:57 CET
This archive was generated by hypermail 2.3.0
: Thu Dec 04 2014 - 13:12:23 CET