Audit renice(1)

1) Get rid of strtop(), which was a NiH-version of estrtonum().
2) Boolean-style-fixes.
3) Update usage, reflecting num-idiom, also update manpage accordingly.
4) Don't break after usage().
5) Rewrite main loop with *argv instead of argv[i].
6) Don't play around with who < 0 and stuff.
7) Rename status to ret for consistency.
This commit is contained in:
FRIGN 2015-03-17 22:15:09 +01:00
parent c7deb4f2b7
commit 4d946a274f
2 changed files with 20 additions and 47 deletions

View File

@ -1,4 +1,4 @@
.Dd January 29, 2015 .Dd March 17, 2015
.Dt RENICE 1 .Dt RENICE 1
.Os sbase .Os sbase
.Sh NAME .Sh NAME
@ -6,7 +6,7 @@
.Nd change niceness of processes .Nd change niceness of processes
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Fl n Ar inc .Fl n Ar num
.Op Fl g | Fl p | Fl u .Op Fl g | Fl p | Fl u
.Ar id ... .Ar id ...
.Sh DESCRIPTION .Sh DESCRIPTION
@ -21,14 +21,14 @@ Interpret each
.Ar id .Ar id
as a process group ID | process ID | user name or ID. as a process group ID | process ID | user name or ID.
The middle option is default. The middle option is default.
.It Fl n Ar inc .It Fl n Ar num
Change niceness by Change niceness by
.Ar inc , .Ar num ,
with niceness ranging from with niceness ranging from
.Sy -20 .Sy -20
(highest priority) (highest priority)
to to
.Sy +19 .Sy +20
(lowest priority). (lowest priority).
.El .El
.Sh SEE ALSO .Sh SEE ALSO

View File

@ -7,35 +7,12 @@
#include "util.h" #include "util.h"
static int strtop(const char *);
static int renice(int, int, long);
static int
strtop(const char *s)
{
char *end;
long n;
errno = 0;
n = strtol(s, &end, 10);
if (*end != '\0') {
weprintf("%s: not an integer\n", s);
return -1;
}
if (errno != 0 || n <= 0 || n > INT_MAX) {
weprintf("%s: invalid value\n", s);
return -1;
}
return (int)n;
}
static int static int
renice(int which, int who, long adj) renice(int which, int who, long adj)
{ {
errno = 0; errno = 0;
adj += getpriority(which, who); adj += getpriority(which, who);
if (errno != 0) { if (errno) {
weprintf("getpriority %d:", who); weprintf("getpriority %d:", who);
return 0; return 0;
} }
@ -52,7 +29,7 @@ renice(int which, int who, long adj)
static void static void
usage(void) usage(void)
{ {
eprintf("renice -n inc [-g | -p | -u] ID ...\n"); eprintf("renice -n num [-g | -p | -u] id ...\n");
} }
int int
@ -60,7 +37,7 @@ main(int argc, char *argv[])
{ {
const char *adj = NULL; const char *adj = NULL;
long val; long val;
int i, which = PRIO_PROCESS, status = 0; int which = PRIO_PROCESS, ret = 0;
struct passwd *pw; struct passwd *pw;
int who; int who;
@ -79,34 +56,30 @@ main(int argc, char *argv[])
break; break;
default: default:
usage(); usage();
break;
} ARGEND; } ARGEND;
if (argc == 0 || !adj) if (!argc || !adj)
usage(); usage();
val = estrtonum(adj, PRIO_MIN, PRIO_MAX); val = estrtonum(adj, PRIO_MIN, PRIO_MAX);
for (i = 0; i < argc; i++) { for (; *argv; argc--, argv++) {
who = -1;
if (which == PRIO_USER) { if (which == PRIO_USER) {
errno = 0; errno = 0;
pw = getpwnam(argv[i]); if (!(pw = getpwnam(*argv))) {
if (!pw) { if (errno)
if (errno != 0) weprintf("getpwnam %s:", *argv);
weprintf("getpwnam %s:", argv[i]);
else else
weprintf("getpwnam %s: no user found\n", argv[i]); weprintf("getpwnam %s: no user found\n", *argv);
status = 1; ret = 1;
continue; continue;
} }
who = pw->pw_uid; who = pw->pw_uid;
} else {
who = estrtonum(*argv, 1, INT_MAX);
} }
if (who < 0) if (!renice(which, who, val))
who = strtop(argv[i]); ret = 1;
if (who < 0 || !renice(which, who, val))
status = 1;
} }
return status; return ret;
} }