[hackers] [sbase] Audit renice(1) || FRIGN

From: <git_AT_suckless.org>
Date: Thu, 19 Mar 2015 11:13:23 +0100 (CET)

commit ce7d2ca08a06c926b1dbd26270e7aa9927cf3a18
Author: FRIGN <dev_AT_frign.de>
Date: Tue Mar 17 22:15:09 2015 +0100

    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.

diff --git a/renice.1 b/renice.1
index 81c5028..de0ea13 100644
--- a/renice.1
+++ b/renice.1
_AT_@ -1,4 +1,4 @@
-.Dd January 29, 2015
+.Dd March 17, 2015
 .Dt RENICE 1
 .Os sbase
 .Sh NAME
_AT_@ -6,7 +6,7 @@
 .Nd change niceness of processes
 .Sh SYNOPSIS
 .Nm
-.Fl n Ar inc
+.Fl n Ar num
 .Op Fl g | Fl p | Fl u
 .Ar id ...
 .Sh DESCRIPTION
_AT_@ -21,14 +21,14 @@ Interpret each
 .Ar id
 as a process group ID | process ID | user name or ID.
 The middle option is default.
-.It Fl n Ar inc
+.It Fl n Ar num
 Change niceness by
-.Ar inc ,
+.Ar num ,
 with niceness ranging from
 .Sy -20
 (highest priority)
 to
-.Sy +19
+.Sy +20
 (lowest priority).
 .El
 .Sh SEE ALSO
diff --git a/renice.c b/renice.c
index 2d0584f..289ae8a 100644
--- a/renice.c
+++ b/renice.c
_AT_@ -7,35 +7,12 @@
 
 #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
 renice(int which, int who, long adj)
 {
         errno = 0;
         adj += getpriority(which, who);
- if (errno != 0) {
+ if (errno) {
                 weprintf("getpriority %d:", who);
                 return 0;
         }
_AT_@ -52,7 +29,7 @@ renice(int which, int who, long adj)
 static void
 usage(void)
 {
- eprintf("renice -n inc [-g | -p | -u] ID ...\n");
+ eprintf("renice -n num [-g | -p | -u] id ...\n");
 }
 
 int
_AT_@ -60,7 +37,7 @@ main(int argc, char *argv[])
 {
         const char *adj = NULL;
         long val;
- int i, which = PRIO_PROCESS, status = 0;
+ int which = PRIO_PROCESS, ret = 0;
         struct passwd *pw;
         int who;
 
_AT_@ -79,34 +56,30 @@ main(int argc, char *argv[])
                 break;
         default:
                 usage();
- break;
         } ARGEND;
 
- if (argc == 0 || !adj)
+ if (!argc || !adj)
                 usage();
 
         val = estrtonum(adj, PRIO_MIN, PRIO_MAX);
- for (i = 0; i < argc; i++) {
- who = -1;
+ for (; *argv; argc--, argv++) {
                 if (which == PRIO_USER) {
                         errno = 0;
- pw = getpwnam(argv[i]);
- if (!pw) {
- if (errno != 0)
- weprintf("getpwnam %s:", argv[i]);
+ if (!(pw = getpwnam(*argv))) {
+ if (errno)
+ weprintf("getpwnam %s:", *argv);
                                 else
- weprintf("getpwnam %s: no user found\n", argv[i]);
- status = 1;
+ weprintf("getpwnam %s: no user found\n", *argv);
+ ret = 1;
                                 continue;
                         }
                         who = pw->pw_uid;
+ } else {
+ who = estrtonum(*argv, 1, INT_MAX);
                 }
- if (who < 0)
- who = strtop(argv[i]);
-
- if (who < 0 || !renice(which, who, val))
- status = 1;
+ if (!renice(which, who, val))
+ ret = 1;
         }
 
- return status;
+ return ret;
 }
Received on Thu Mar 19 2015 - 11:13:23 CET

This archive was generated by hypermail 2.3.0 : Thu Mar 19 2015 - 11:24:23 CET