aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/nice/nice.c
diff options
context:
space:
mode:
authorTim J. Robbins <tjr@FreeBSD.org>2002-05-12 22:49:48 +0000
committerTim J. Robbins <tjr@FreeBSD.org>2002-05-12 22:49:48 +0000
commit7e91d5f9263e60ac76e525afc99e073ba980d79c (patch)
treedee5753b18dfad4bdd9f660df13f59717d154d80 /usr.bin/nice/nice.c
parent386f1d1bf218c69a01b4b176ead140ec4eb69cff (diff)
downloadsrc-7e91d5f9263e60ac76e525afc99e073ba980d79c.tar.gz
src-7e91d5f9263e60ac76e525afc99e073ba980d79c.zip
Support the SUSv3 -n option and the "--" end of options marker.
Replace "command" with "utility" in the manual page & source to be more consistent with the terminology used in the standard, and to hint that shell builtin commands won't work. Submitted by: Peter Avalos <pavalos@theshell.com> (partially) Approved by: mike
Notes
Notes: svn path=/head/; revision=96481
Diffstat (limited to 'usr.bin/nice/nice.c')
-rw-r--r--usr.bin/nice/nice.c43
1 files changed, 29 insertions, 14 deletions
diff --git a/usr.bin/nice/nice.c b/usr.bin/nice/nice.c
index d9d91c4a1cfb..13634a13b804 100644
--- a/usr.bin/nice/nice.c
+++ b/usr.bin/nice/nice.c
@@ -53,6 +53,7 @@ __FBSDID("$FreeBSD$");
#include <ctype.h>
#include <err.h>
#include <errno.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -67,35 +68,49 @@ main(argc, argv)
int argc;
char *argv[];
{
- int niceness = DEFNICE;
+ long niceness = DEFNICE;
+ int ch;
+ char *ep;
- if (argc < 2)
- usage();
+ /* Obsolescent syntax: -number, --number */
+ if (argc >= 2 && argv[1][0] == '-' && (argv[1][1] == '-' ||
+ isdigit((unsigned char)argv[1][1])) && strcmp(argv[1], "--") != 0)
+ if (asprintf(&argv[1], "-n%s", argv[1] + 1) < 0)
+ err(1, "asprintf");
- if (argv[1][0] == '-') {
- if (argv[1][1] == '-' || isdigit(argv[1][1])) {
- niceness = atoi(argv[1] + 1);
- ++argv;
- } else
- errx(1, "illegal option -- %s", argv[1]);
+ while ((ch = getopt(argc, argv, "n:")) != -1) {
+ switch (ch) {
+ case 'n':
+ errno = 0;
+ niceness = strtol(optarg, &ep, 10);
+ if (ep == optarg || *ep != '\0' || errno ||
+ niceness < INT_MIN || niceness > INT_MAX)
+ errx(1, "%s: invalid nice value", optarg);
+ break;
+ default:
+ usage();
+ }
}
+ argc -= optind;
+ argv += optind;
- if (argv[1] == NULL)
+ if (argc == 0)
usage();
errno = 0;
niceness += getpriority(PRIO_PROCESS, 0);
if (errno)
err(1, "getpriority");
- if (setpriority(PRIO_PROCESS, 0, niceness))
+ if (setpriority(PRIO_PROCESS, 0, (int)niceness))
err(1, "setpriority");
- execvp(argv[1], &argv[1]);
- err(errno == ENOENT ? 127 : 126, "%s", argv[1]);
+ execvp(*argv, argv);
+ err(errno == ENOENT ? 127 : 126, "%s", *argv);
}
void
usage()
{
- (void)fprintf(stderr, "usage: nice [-number] command [arguments]\n");
+
+ (void)fprintf(stderr, "usage: nice [-n incr] utility [arguments]\n");
exit(1);
}