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.
This commit is contained in:
sin 2013-11-13 11:39:24 +00:00
parent 34f7a688cb
commit b8edf3b4ee
15 changed files with 47 additions and 34 deletions

7
cat.c
View File

@ -26,9 +26,10 @@ main(int argc, char *argv[])
if(argc == 0) { if(argc == 0) {
concat(stdin, "<stdin>", stdout, "<stdout>"); concat(stdin, "<stdin>", stdout, "<stdout>");
} else for(i = 0; i < argc; i++) { } else for(i = 0; i < argc; i++) {
if(!(fp = fopen(argv[i], "r"))) if(!(fp = fopen(argv[i], "r"))) {
eprintf("fopen %s:", argv[i]); weprintf("fopen %s:", argv[i]);
continue;
}
concat(fp, argv[i], stdout, "<stdout>"); concat(fp, argv[i], stdout, "<stdout>");
fclose(fp); fclose(fp);
} }

View File

@ -1,5 +1,4 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -77,8 +76,7 @@ main(int argc, char *argv[])
} else { } else {
for(; argc > 0; argc--, argv++) { for(; argc > 0; argc--, argv++) {
if (!(fp = fopen(argv[0], "r"))) { if (!(fp = fopen(argv[0], "r"))) {
fprintf(stderr, "fopen %s: %s\n", argv[0], weprintf("fopen %s:", argv[0]);
strerror(errno));
continue; continue;
} }
cksum(fp, argv[0]); cksum(fp, argv[0]);

6
cut.c
View File

@ -155,8 +155,10 @@ main(int argc, char *argv[])
if(!argc) if(!argc)
cut(stdin); cut(stdin);
else for(; argc--; argv++) { else for(; argc--; argv++) {
if(!(fp = strcmp(*argv, "-") ? fopen(*argv, "r") : stdin)) if(!(fp = strcmp(*argv, "-") ? fopen(*argv, "r") : stdin)) {
eprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
continue;
}
cut(fp); cut(fp);
fclose(fp); fclose(fp);
} }

4
du.c
View File

@ -3,7 +3,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <dirent.h> #include <dirent.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -120,8 +119,7 @@ du(const char *path)
dp = opendir(path); dp = opendir(path);
if (!dp) { if (!dp) {
fprintf(stderr, "opendir: %s: %s\n", path, weprintf("opendir %s:", path);
strerror(errno));
goto done; goto done;
} }

View File

@ -41,7 +41,8 @@ main(int argc, char *argv[])
} else { } else {
for (; argc > 0; argc--) { for (; argc > 0; argc--) {
if (!(fp = fopen(*argv, "r"))) { if (!(fp = fopen(*argv, "r"))) {
eprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
continue;
} }
dsc.name = *argv; dsc.name = *argv;
dsc.fp = fp; dsc.fp = fp;

5
fold.c
View File

@ -1,10 +1,8 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <ctype.h> #include <ctype.h>
#include <errno.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
@ -48,8 +46,7 @@ main(int argc, char *argv[])
fold(stdin, width); fold(stdin, width);
} else for(; argc > 0; argc--, argv++) { } else for(; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) { if(!(fp = fopen(argv[0], "r"))) {
fprintf(stderr, "fopen %s: %s\n", argv[0], weprintf("fopen %s:", argv[0]);
strerror(errno));
continue; continue;
} }
fold(fp, width); fold(fp, width);

4
head.c
View File

@ -1,5 +1,4 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -35,8 +34,7 @@ main(int argc, char *argv[])
head(stdin, "<stdin>", n); head(stdin, "<stdin>", n);
} else for(; argc > 0; argc--, argv++) { } else for(; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) { if(!(fp = fopen(argv[0], "r"))) {
fprintf(stderr, "fopen %s: %s\n", argv[0], weprintf("fopen %s:", argv[0]);
strerror(errno));
continue; continue;
} }
head(fp, argv[0], n); head(fp, argv[0], n);

6
nl.c
View File

@ -50,8 +50,10 @@ main(int argc, char *argv[])
if(argc == 0) { if(argc == 0) {
nl(stdin); nl(stdin);
} else for(; argc > 0; argc--, argv++) { } else for(; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) if(!(fp = fopen(argv[0], "r"))) {
eprintf("fopen %s:", argv[0]); weprintf("fopen %s:", argv[0]);
continue;
}
nl(fp); nl(fp);
fclose(fp); fclose(fp);
} }

6
sort.c
View File

@ -40,8 +40,10 @@ main(int argc, char *argv[])
if(argc == 0) { if(argc == 0) {
getlines(stdin, &linebuf); getlines(stdin, &linebuf);
} else for(; argc > 0; argc--, argv++) { } else for(; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) if(!(fp = fopen(argv[0], "r"))) {
eprintf("fopen %s:", argv[0]); weprintf("fopen %s:", argv[0]);
continue;
}
getlines(fp, &linebuf); getlines(fp, &linebuf);
fclose(fp); fclose(fp);
} }

4
stat.c
View File

@ -42,9 +42,7 @@ main(int argc, char *argv[])
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
fn = Lflag ? stat : lstat; fn = Lflag ? stat : lstat;
if (fn(argv[i], &st) == -1) { if (fn(argv[i], &st) == -1) {
fprintf(stderr, "%s %s: ", Lflag ? "stat" : "lstat", weprintf("%s %s:", Lflag ? "stat" : "lstat", argv[i]);
argv[i]);
perror(NULL);
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
continue; continue;
} }

View File

@ -26,8 +26,10 @@ main(int argc, char *argv[])
dostrings(stdin, "<stdin>"); dostrings(stdin, "<stdin>");
} else { } else {
for (; argc > 0; argc--, argv++) { for (; argc > 0; argc--, argv++) {
if (!(fp = fopen(argv[0], "r"))) if (!(fp = fopen(argv[0], "r"))) {
eprintf("open %s:", argv[0]); weprintf("fopen %s:", argv[0]);
continue;
}
dostrings(fp, argv[0]); dostrings(fp, argv[0]);
fclose(fp); fclose(fp);
} }

4
tail.c
View File

@ -1,5 +1,4 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -42,8 +41,7 @@ main(int argc, char *argv[])
} else { } else {
for (; argc > 0; argc--, argv++) { for (; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) { if(!(fp = fopen(argv[0], "r"))) {
fprintf(stderr, "fopen %s: %s\n", argv[0], weprintf("fopen %s:", argv[0]);
strerror(errno));
continue; continue;
} }
tail(fp, argv[0], n); tail(fp, argv[0], n);

2
util.h
View File

@ -20,4 +20,4 @@ long estrtol(const char *, int);
void fnck(const char *, const char *, int (*)(const char *, const char *)); void fnck(const char *, const char *, int (*)(const char *, const char *));
void putword(const char *); void putword(const char *);
void recurse(const char *, void (*)(const char *)); void recurse(const char *, void (*)(const char *));
void weprintf(const char *, ...);

View File

@ -45,3 +45,17 @@ venprintf(int status, const char *fmt, va_list ap)
exit(status); 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);
}
}

6
wc.c
View File

@ -47,8 +47,10 @@ main(int argc, char *argv[])
wc(stdin, NULL); wc(stdin, NULL);
} else { } else {
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if(!(fp = fopen(argv[i], "r"))) if(!(fp = fopen(argv[i], "r"))) {
eprintf("fopen %s:", argv[i]); weprintf("fopen %s:", argv[i]);
continue;
}
wc(fp, argv[i]); wc(fp, argv[i]);
fclose(fp); fclose(fp);
} }