From 6e482061cdafe941e6c7e96b4cd6c53660600df1 Mon Sep 17 00:00:00 2001 From: tjr 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.1 | 69 +++++++++++++++++++++++++---------------------------- usr.bin/nice/nice.c | 43 ++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 51 deletions(-) diff --git a/usr.bin/nice/nice.1 b/usr.bin/nice/nice.1 index 3ed43a3..9948e3a 100644 --- a/usr.bin/nice/nice.1 +++ b/usr.bin/nice/nice.1 @@ -37,40 +37,26 @@ .Os .Sh NAME .Nm nice -.Nd execute a command at a low scheduling priority +.Nd execute a utility at an altered scheduling priority .Sh SYNOPSIS .Nm -.Op Fl Ns Ar number -.Ar command -.Op Ar arguments +.Op Fl n Ar increment +.Ar utility +.Op Ar argument ... .Sh DESCRIPTION The .Nm utility runs -.Ar command -at a low priority. -(Think of low and slow). -If -.Fl Ns Ar number -is not given, -.Nm -assumes the value 10. -The priority is a value in the range -20 to 20. -The default priority is 0, priority 20 is the lowest possible. -The -.Nm -utility will execute -.Ar command -at priority -.Ar number -relative to the priority -of -.Nm . -Higher priorities than the -current process priority can only requested by the -super-user. -Negative numbers are expressed as -.Fl - Ns Ar number . +.Ar utility +at an altered scheduling priority, by incrementing its +.Dq nice +value by the specified +.Ar increment , +or a default value of 10. +The lower the nice value of a process, the higher its scheduling priority. +.Pp +The superuser may specify a negative increment in order to run a utility +with a higher scheduling prority. .Pp Some shells may provide a builtin .Nm @@ -79,32 +65,32 @@ Consult the .Xr builtin 1 manual page. .Sh EXAMPLES -$ nice -5 date +$ nice -n 5 date .Pp -Execute command +Execute utility .Sq date at priority 5 assuming the priority of the shell is 0. .Pp -# nice -16 nice --35 date +# nice -n 16 nice -n -35 date .Pp -Execute command +Execute utility .Sq date at priority -19 assuming the priority of the shell is 0 and you are the super-user. .Sh DIAGNOSTICS If -.Ar command +.Ar utility is invoked, the exit status of .Nm is the exit status of -.Ar command . +.Ar utility . .Pp An exit status of 126 indicates -.Ar command +.Ar utility was found, but could not be executed. An exit status of 127 indicates -.Ar command +.Ar utility could not be found. .Sh SEE ALSO .Xr builtin 1 , @@ -114,8 +100,17 @@ could not be found. .Xr getpriority 2 , .Xr setpriority 2 , .Xr renice 8 +.Sh COMPATIBILITY +The traditional +.Fl Ns Ar increment +option has been deprecated but is still supported. +.Sh STANDARDS +The +.Nm +utility conforms to +.St -p1003.1-2001 . .Sh HISTORY A .Nm -command appeared in +utility appeared in .At v6 . diff --git a/usr.bin/nice/nice.c b/usr.bin/nice/nice.c index d9d91c4..13634a1 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.1