diff options
| author | christos <christos@NetBSD.org> | 2005-10-05 21:20:46 +0000 |
|---|---|---|
| committer | christos <christos@NetBSD.org> | 2005-10-05 21:20:46 +0000 |
| commit | c2bd98ebb5d94e64cd577152ccc21419acc2a593 (patch) | |
| tree | 90819ebe0eee0aff3c92279cc30e86559cb928ec /usr.bin/nice | |
| parent | c81227bb08196ea3c4b9a5fe9670a087ee374c47 (diff) | |
From Liam Foy:
- Check for errno being set to ENOTDIR from execvp. This
will stop an incorrect value being returned if a component
of the new process image file's path is not a directory.
- Cleanup and KNF
Diffstat (limited to 'usr.bin/nice')
| -rw-r--r-- | usr.bin/nice/nice.c | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/usr.bin/nice/nice.c b/usr.bin/nice/nice.c index a28b82a62c5..a1320a0fe80 100644 --- a/usr.bin/nice/nice.c +++ b/usr.bin/nice/nice.c @@ -1,4 +1,4 @@ -/* $NetBSD: nice.c,v 1.12 2003/08/07 11:15:24 agc Exp $ */ +/* $NetBSD: nice.c,v 1.13 2005/10/05 21:20:46 christos Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. @@ -40,15 +40,16 @@ __COPYRIGHT( #if 0 static char sccsid[] = "@(#)nice.c 5.4 (Berkeley) 6/1/90"; #endif -__RCSID("$NetBSD: nice.c,v 1.12 2003/08/07 11:15:24 agc Exp $"); +__RCSID("$NetBSD: nice.c,v 1.13 2005/10/05 21:20:46 christos Exp $"); #endif /* not lint */ -#include <sys/types.h> #include <sys/time.h> #include <sys/resource.h> + #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <limits.h> #include <locale.h> #include <ctype.h> #include <errno.h> @@ -57,18 +58,18 @@ __RCSID("$NetBSD: nice.c,v 1.12 2003/08/07 11:15:24 agc Exp $"); #define DEFNICE 10 -int main __P((int, char **)); -static void usage __P((void)); +static void usage(void) __attribute__((__noreturn__)); int -main(argc, argv) - int argc; - char **argv; +main(int argc, char **argv) { + char *ep; int niceness = DEFNICE; int c; + long tmp; - setlocale(LC_ALL, ""); + setprogname(argv[0]); + (void)setlocale(LC_ALL, ""); /* handle obsolete -number syntax */ if (argc > 1 && argv[1][0] == '-' && @@ -80,16 +81,20 @@ main(argc, argv) while ((c = getopt (argc, argv, "n:")) != -1) { switch (c) { case 'n': - niceness = atoi (optarg); + errno = 0; + tmp = strtol(optarg, &ep, 10); + if (*ep != '\0' || tmp < INT_MIN || tmp > INT_MAX) + errx(EXIT_FAILURE, "invalid argument: `%s'", + optarg); + niceness = (int)tmp; break; - - case '?': default: usage(); break; } } - argc -= optind; argv += optind; + argc -= optind; + argv += optind; if (argc == 0) usage(); @@ -97,23 +102,23 @@ main(argc, argv) errno = 0; niceness += getpriority(PRIO_PROCESS, 0); if (errno) { - err (1, "getpriority"); + err(EXIT_FAILURE, "getpriority"); /* NOTREACHED */ } - if (setpriority(PRIO_PROCESS, 0, niceness)) { - warn ("setpriority"); + if (setpriority(PRIO_PROCESS, 0, niceness) == -1) { + warn("setpriority"); } - execvp(argv[0], &argv[0]); - err ((errno == ENOENT) ? 127 : 126, "%s", argv[0]); + (void)execvp(argv[0], &argv[0]); + err((errno == ENOENT || errno == ENOTDIR) ? 127 : 126, "%s", argv[0]); /* NOTREACHED */ } static void -usage() +usage(void) { (void)fprintf(stderr, - "usage: nice [ -n increment ] utility [ argument ...]\n"); - - exit(1); + "Usage: %s [ -n increment ] utility [ argument ...]\n", + getprogname()); + exit(EXIT_FAILURE); } |
