summaryrefslogtreecommitdiffstats
path: root/usr.bin/perror
diff options
context:
space:
mode:
authorgnn <gnn@FreeBSD.org>2009-05-20 22:19:22 +0000
committergnn <gnn@FreeBSD.org>2009-05-20 22:19:22 +0000
commit9bb1878d0e5298a72144d79a33ca85c445213fc4 (patch)
tree6930c9013cdec6ab528bd912db78f3a4393cc75c /usr.bin/perror
parent91a54071dfd4bcfb53f7fe96f4942453f6599529 (diff)
downloadFreeBSD-src-9bb1878d0e5298a72144d79a33ca85c445213fc4.zip
FreeBSD-src-9bb1878d0e5298a72144d79a33ca85c445213fc4.tar.gz
A few more style changes as well as a more broad allowance for
errors to be given by the caller. Change output to be easier for use in scripts. Submitted by: bce
Diffstat (limited to 'usr.bin/perror')
-rw-r--r--usr.bin/perror/perror.c46
1 files changed, 19 insertions, 27 deletions
diff --git a/usr.bin/perror/perror.c b/usr.bin/perror/perror.c
index 4ef79cb..19b79ce 100644
--- a/usr.bin/perror/perror.c
+++ b/usr.bin/perror/perror.c
@@ -31,48 +31,40 @@ __FBSDID("$FreeBSD$");
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <err.h>
#include <sys/errno.h>
-#define MAX_ERR 256
-
-static void
-usage()
-{
-
- fprintf(stderr, "usage: perror number\n");
- fprintf(stderr, "number must be between 1 and %d\n", ELAST);
- exit(1);
-}
+static void usage();
int
main(int argc, char **argv)
{
-
- char errstr[MAX_ERR];
char *cp;
- int errnum;
+ char *errstr;
+ long errnum;
if (argc != 2)
usage();
+ errno = 0;
+
errnum = strtol(argv[1], &cp, 0);
- if (((errnum == 0) && (errno == EINVAL)) || (*cp != '\0')) {
- fprintf(stderr, "Argument %s not a number.\n", argv[1]);
- usage();
- }
+ if (errno != 0)
+ err(1, NULL);
- if ((errnum <=0) || (errnum > ELAST)) {
- fprintf(stderr, "Number %d out of range.\n", errnum);
- usage();
- }
-
- if (strerror_r(errnum, errstr, sizeof(errstr)) < 0) {
- fprintf(stderr, "Could not find error number %d.\n", errnum);
- usage();
- }
+ if ((errstr = strerror(errnum)) == NULL)
+ err(1, NULL);
- printf("Error %d is \"%s\"\n", errnum, errstr);
+ printf("%s\n", errstr);
exit(0);
}
+
+static void
+usage()
+{
+ fprintf(stderr, "usage: perror number\n");
+ exit(1);
+}
+
OpenPOWER on IntegriCloud