[hackers] [sbase] Add weprintf() and replace fprintf(stderr, ...) calls || sin
commit b8edf3b4ee860f49f8059354704ac9a379930ef1
Author: sin <sin_AT_2f30.org>
Date: Wed Nov 13 11:39:24 2013 +0000
Add weprintf() and replace fprintf(stderr, ...) calls
There is still some programs left to be updated for this.
Many of these programs would stop on the first file that they
could not open.
diff --git a/cat.c b/cat.c
index 816cb41..6db1761 100644
--- a/cat.c
+++ b/cat.c
_AT_@ -26,9 +26,10 @@ main(int argc, char *argv[])
if(argc == 0) {
concat(stdin, "<stdin>", stdout, "<stdout>");
} else for(i = 0; i < argc; i++) {
- if(!(fp = fopen(argv[i], "r")))
- eprintf("fopen %s:", argv[i]);
-
+ if(!(fp = fopen(argv[i], "r"))) {
+ weprintf("fopen %s:", argv[i]);
+ continue;
+ }
concat(fp, argv[i], stdout, "<stdout>");
fclose(fp);
}
diff --git a/cksum.c b/cksum.c
index 725c392..8347a88 100644
--- a/cksum.c
+++ b/cksum.c
_AT_@ -1,5 +1,4 @@
/* See LICENSE file for copyright and license details. */
-#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
_AT_@ -77,8 +76,7 @@ main(int argc, char *argv[])
} else {
for(; argc > 0; argc--, argv++) {
if (!(fp = fopen(argv[0], "r"))) {
- fprintf(stderr, "fopen %s: %s
", argv[0],
- strerror(errno));
+ weprintf("fopen %s:", argv[0]);
continue;
}
cksum(fp, argv[0]);
diff --git a/cut.c b/cut.c
index 7e30361..b058301 100644
--- a/cut.c
+++ b/cut.c
_AT_@ -155,8 +155,10 @@ main(int argc, char *argv[])
if(!argc)
cut(stdin);
else for(; argc--; argv++) {
- if(!(fp = strcmp(*argv, "-") ? fopen(*argv, "r") : stdin))
- eprintf("fopen %s:", *argv);
+ if(!(fp = strcmp(*argv, "-") ? fopen(*argv, "r") : stdin)) {
+ weprintf("fopen %s:", *argv);
+ continue;
+ }
cut(fp);
fclose(fp);
}
diff --git a/du.c b/du.c
index e7a64f3..525ed9b 100644
--- a/du.c
+++ b/du.c
_AT_@ -3,7 +3,6 @@
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
-#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
_AT_@ -120,8 +119,7 @@ du(const char *path)
dp = opendir(path);
if (!dp) {
- fprintf(stderr, "opendir: %s: %s
", path,
- strerror(errno));
+ weprintf("opendir %s:", path);
goto done;
}
diff --git a/expand.c b/expand.c
index beebece..f859452 100644
--- a/expand.c
+++ b/expand.c
_AT_@ -41,7 +41,8 @@ main(int argc, char *argv[])
} else {
for (; argc > 0; argc--) {
if (!(fp = fopen(*argv, "r"))) {
- eprintf("fopen %s:", *argv);
+ weprintf("fopen %s:", *argv);
+ continue;
}
dsc.name = *argv;
dsc.fp = fp;
diff --git a/fold.c b/fold.c
index 4dfaf11..5516aae 100644
--- a/fold.c
+++ b/fold.c
_AT_@ -1,10 +1,8 @@
/* See LICENSE file for copyright and license details. */
#include <ctype.h>
-#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
#include <unistd.h>
#include "text.h"
#include "util.h"
_AT_@ -48,8 +46,7 @@ main(int argc, char *argv[])
fold(stdin, width);
} else for(; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) {
- fprintf(stderr, "fopen %s: %s
", argv[0],
- strerror(errno));
+ weprintf("fopen %s:", argv[0]);
continue;
}
fold(fp, width);
diff --git a/head.c b/head.c
index 6578f89..ccd619c 100644
--- a/head.c
+++ b/head.c
_AT_@ -1,5 +1,4 @@
/* See LICENSE file for copyright and license details. */
-#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
_AT_@ -35,8 +34,7 @@ main(int argc, char *argv[])
head(stdin, "<stdin>", n);
} else for(; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) {
- fprintf(stderr, "fopen %s: %s
", argv[0],
- strerror(errno));
+ weprintf("fopen %s:", argv[0]);
continue;
}
head(fp, argv[0], n);
diff --git a/nl.c b/nl.c
index e35b043..cf458b8 100644
--- a/nl.c
+++ b/nl.c
_AT_@ -50,8 +50,10 @@ main(int argc, char *argv[])
if(argc == 0) {
nl(stdin);
} else for(; argc > 0; argc--, argv++) {
- if(!(fp = fopen(argv[0], "r")))
- eprintf("fopen %s:", argv[0]);
+ if(!(fp = fopen(argv[0], "r"))) {
+ weprintf("fopen %s:", argv[0]);
+ continue;
+ }
nl(fp);
fclose(fp);
}
diff --git a/sort.c b/sort.c
index 078b70f..f992423 100644
--- a/sort.c
+++ b/sort.c
_AT_@ -40,8 +40,10 @@ main(int argc, char *argv[])
if(argc == 0) {
getlines(stdin, &linebuf);
} else for(; argc > 0; argc--, argv++) {
- if(!(fp = fopen(argv[0], "r")))
- eprintf("fopen %s:", argv[0]);
+ if(!(fp = fopen(argv[0], "r"))) {
+ weprintf("fopen %s:", argv[0]);
+ continue;
+ }
getlines(fp, &linebuf);
fclose(fp);
}
diff --git a/stat.c b/stat.c
index c6f7da6..37d8676 100644
--- a/stat.c
+++ b/stat.c
_AT_@ -42,9 +42,7 @@ main(int argc, char *argv[])
for (i = 0; i < argc; i++) {
fn = Lflag ? stat : lstat;
if (fn(argv[i], &st) == -1) {
- fprintf(stderr, "%s %s: ", Lflag ? "stat" : "lstat",
- argv[i]);
- perror(NULL);
+ weprintf("%s %s:", Lflag ? "stat" : "lstat", argv[i]);
ret = EXIT_FAILURE;
continue;
}
diff --git a/strings.c b/strings.c
index c407b56..01feb66 100644
--- a/strings.c
+++ b/strings.c
_AT_@ -26,8 +26,10 @@ main(int argc, char *argv[])
dostrings(stdin, "<stdin>");
} else {
for (; argc > 0; argc--, argv++) {
- if (!(fp = fopen(argv[0], "r")))
- eprintf("open %s:", argv[0]);
+ if (!(fp = fopen(argv[0], "r"))) {
+ weprintf("fopen %s:", argv[0]);
+ continue;
+ }
dostrings(fp, argv[0]);
fclose(fp);
}
diff --git a/tail.c b/tail.c
index b01b0e8..c481e99 100644
--- a/tail.c
+++ b/tail.c
_AT_@ -1,5 +1,4 @@
/* See LICENSE file for copyright and license details. */
-#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
_AT_@ -42,8 +41,7 @@ main(int argc, char *argv[])
} else {
for (; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) {
- fprintf(stderr, "fopen %s: %s
", argv[0],
- strerror(errno));
+ weprintf("fopen %s:", argv[0]);
continue;
}
tail(fp, argv[0], n);
diff --git a/util.h b/util.h
index 620390a..f7e8780 100644
--- a/util.h
+++ b/util.h
_AT_@ -20,4 +20,4 @@ long estrtol(const char *, int);
void fnck(const char *, const char *, int (*)(const char *, const char *));
void putword(const char *);
void recurse(const char *, void (*)(const char *));
-
+void weprintf(const char *, ...);
diff --git a/util/eprintf.c b/util/eprintf.c
index a21fe42..4b12902 100644
--- a/util/eprintf.c
+++ b/util/eprintf.c
_AT_@ -45,3 +45,17 @@ venprintf(int status, const char *fmt, va_list ap)
exit(status);
}
+void
+weprintf(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+
+ if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
+ fputc(' ', stderr);
+ perror(NULL);
+ }
+}
\ No newline at end of file
diff --git a/wc.c b/wc.c
index 5b89d40..ba76a68 100644
--- a/wc.c
+++ b/wc.c
_AT_@ -47,8 +47,10 @@ main(int argc, char *argv[])
wc(stdin, NULL);
} else {
for (i = 0; i < argc; i++) {
- if(!(fp = fopen(argv[i], "r")))
- eprintf("fopen %s:", argv[i]);
+ if(!(fp = fopen(argv[i], "r"))) {
+ weprintf("fopen %s:", argv[i]);
+ continue;
+ }
wc(fp, argv[i]);
fclose(fp);
}
Received on Wed Nov 13 2013 - 12:42:30 CET
This archive was generated by hypermail 2.3.0
: Wed Nov 13 2013 - 12:48:17 CET