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
This commit is contained in:
Tim J. Robbins 2002-05-12 22:49:48 +00:00
parent 386f1d1bf2
commit 7e91d5f926
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=96481
2 changed files with 61 additions and 51 deletions

View File

@ -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 .

View File

@ -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);
}