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) {
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);
}

View File

@ -1,5 +1,4 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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\n", argv[0],
strerror(errno));
weprintf("fopen %s:", argv[0]);
continue;
}
cksum(fp, argv[0]);

6
cut.c
View File

@ -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);
}

4
du.c
View File

@ -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>
@ -120,8 +119,7 @@ du(const char *path)
dp = opendir(path);
if (!dp) {
fprintf(stderr, "opendir: %s: %s\n", path,
strerror(errno));
weprintf("opendir %s:", path);
goto done;
}

View File

@ -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;

5
fold.c
View File

@ -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"
@ -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\n", argv[0],
strerror(errno));
weprintf("fopen %s:", argv[0]);
continue;
}
fold(fp, width);

4
head.c
View File

@ -1,5 +1,4 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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\n", argv[0],
strerror(errno));
weprintf("fopen %s:", argv[0]);
continue;
}
head(fp, argv[0], n);

6
nl.c
View File

@ -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);
}

6
sort.c
View File

@ -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);
}

4
stat.c
View File

@ -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;
}

View File

@ -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);
}

4
tail.c
View File

@ -1,5 +1,4 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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\n", argv[0],
strerror(errno));
weprintf("fopen %s:", argv[0]);
continue;
}
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 putword(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);
}
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);
} 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);
}