From 7e91d5f9263e60ac76e525afc99e073ba980d79c Mon Sep 17 00:00:00 2001 From: "Tim J. Robbins" Date: Sun, 12 May 2002 22:49:48 +0000 Subject: 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 (partially) Approved by: mike --- usr.bin/nice/nice.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) (limited to 'usr.bin/nice/nice.c') 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 #include #include +#include #include #include #include @@ -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); } -- cgit v1.2.3