summaryrefslogtreecommitdiff
path: root/libc/stdio/perror.c
diff options
context:
space:
mode:
authorManuel Novoa III <mjn3@codepoet.org>2001-02-19 00:28:09 +0000
committerManuel Novoa III <mjn3@codepoet.org>2001-02-19 00:28:09 +0000
commitd521275a86bb72f824dd76e6cb4e35d899f385da (patch)
treeb156e1a492898f7294952d62f821d6227d1a5472 /libc/stdio/perror.c
parentdfe2d42547de8197f850f3ff0dfdc3caa4682518 (diff)
Lots of stdio cleanups. Several bug fixes, addition of a number of functions
to supplement macros in stdio.h, change perror to use stdio package instead of "write". Also add back in weak stdio initialization for static lib case.
Diffstat (limited to 'libc/stdio/perror.c')
-rw-r--r--libc/stdio/perror.c35
1 files changed, 21 insertions, 14 deletions
diff --git a/libc/stdio/perror.c b/libc/stdio/perror.c
index d6274c056..04cd688d8 100644
--- a/libc/stdio/perror.c
+++ b/libc/stdio/perror.c
@@ -1,19 +1,26 @@
-#include <unistd.h>
-#include <string.h>
+#include <stdio.h>
#include <errno.h>
-void perror(str)
-__const char *str;
-{
- register char *ptr;
+/*
+ * Manuel Novoa III Feb 2001
+ *
+ * Replaced old version that did write(2,...)'s with a version using
+ * stream functions. If the program is calling perror, it's a safe
+ * bet that printf and friends are used as well. It is also possible
+ * that the calling program could buffer stderr, or reassign it.
+ * Also, the old version did not conform the standards when the
+ * passed char * was either NULL or pointed to an empty string.
+ */
- if (str) {
- write(2, str, strlen(str));
- write(2, ": ", 2);
- } else
- write(2, "perror: ", 8);
+void perror(__const char *str)
+{
+ static const char perror_str[] = ": ";
+ const char *sep;
- ptr = strerror(errno);
- write(2, ptr, strlen(ptr));
- write(2, "\n", 1);
+ sep = perror_str;
+ if (!(str && *str)) { /* Caller did not supply a prefix message */
+ sep += 2; /* or passed an empty string. */
+ str = sep;
+ }
+ fprintf(stderr, "%s%s%s\n", str, sep, strerror(errno));
}