[hackers] [sbase] Audit nice(1) || FRIGN
commit f84a4429681abddd6febd41013c89ac82f76492c
Author: FRIGN <dev_AT_frign.de>
Date: Mon Mar 2 16:53:13 2015 +0100
Audit nice(1)
1) val is sufficient as "int" (read the standard)
2) BUGFIX: If getpriority fails, it returns -1 and sets errno.
Previously, it would correctly catch the errno but not take
care of the fact that by then val has been decremented by 1.
Only change val if the getpriority-call has been successful.
3) Add LIMIT()-macro from st to increase readability.
4) setpriority returns < 0 on failure
5) Remove bikeshedding-comment. Read the standard if you wonder.
6) return-value trick from env(1)
diff --git a/README b/README
index 266dee3..cc19a7f 100644
--- a/README
+++ b/README
_AT_@ -47,7 +47,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=* mkfifo yes none
=* mktemp non-posix none
=* mv yes none (-i)
-=* nice yes none
+=*| nice yes none
= nl no -d, -f, -h, -l, -p
=* nohup yes none
#* paste yes none
diff --git a/nice.1 b/nice.1
index a048f19..9c616a2 100644
--- a/nice.1
+++ b/nice.1
_AT_@ -1,4 +1,4 @@
-.Dd January 28, 2015
+.Dd March 1, 2015
.Dt NICE 1
.Os sbase
.Sh NAME
_AT_@ -25,7 +25,7 @@ ranging from
.Sy -20
(highest priority)
to
-.Sy +19
+.Sy +20
(lowest priority).
Default is 10.
.El
diff --git a/nice.c b/nice.c
index 9f5b1a3..70a5c2d 100644
--- a/nice.c
+++ b/nice.c
_AT_@ -16,8 +16,7 @@ usage(void)
int
main(int argc, char *argv[])
{
- long val = 10;
- int savederrno;
+ int val = 10, r, savederrno;
ARGBEGIN {
case 'n':
_AT_@ -28,20 +27,22 @@ main(int argc, char *argv[])
break;
} ARGEND;
- if (argc == 0)
+ if (!argc)
usage();
errno = 0;
- val += getpriority(PRIO_PROCESS, 0);
- if (errno != 0)
+ r = getpriority(PRIO_PROCESS, 0);
+ if (errno)
weprintf("getpriority:");
- val = MAX(PRIO_MIN, MIN(val, PRIO_MAX));
- if (setpriority(PRIO_PROCESS, 0, val) != 0)
+ else
+ val += r;
+ LIMIT(val, PRIO_MIN, PRIO_MAX);
+ if (setpriority(PRIO_PROCESS, 0, val) < 0)
weprintf("setpriority:");
- /* POSIX specifies the nice failure still invokes the command */
execvp(argv[0], argv);
savederrno = errno;
weprintf("execvp %s:", argv[0]);
- return (savederrno == ENOENT)? 127 : 126;
+
+ return 126 + (savederrno == ENOENT);
}
diff --git a/util.h b/util.h
index 4d89560..a107a07 100644
--- a/util.h
+++ b/util.h
_AT_@ -13,6 +13,8 @@
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#undef MAX
#define MAX(x,y) ((x) > (y) ? (x) : (y))
+#undef LIMIT
+#define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
#define LEN(x) (sizeof (x) / sizeof *(x))
Received on Tue Mar 03 2015 - 14:32:26 CET
This archive was generated by hypermail 2.3.0
: Tue Mar 03 2015 - 14:36:27 CET