aboutsummaryrefslogtreecommitdiff
path: root/coreutils/nice.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-10-08 23:36:17 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-10-08 23:36:17 +0000
commitca3c981c07ade7f8fd50ba4bb452a2fadaddc326 (patch)
tree8763d9f89197adda0ef45ac57418743766345fe4 /coreutils/nice.c
parent7039a66b58706457c7423de60556e04545432943 (diff)
downloadbusybox-w32-ca3c981c07ade7f8fd50ba4bb452a2fadaddc326.tar.gz
busybox-w32-ca3c981c07ade7f8fd50ba4bb452a2fadaddc326.tar.bz2
busybox-w32-ca3c981c07ade7f8fd50ba4bb452a2fadaddc326.zip
start_stop_daemon: add -N <nice> compat
[re]nice: add support for -nNNN w/o spaces, -NNN (nice only), simplified code
Diffstat (limited to 'coreutils/nice.c')
-rw-r--r--coreutils/nice.c44
1 files changed, 13 insertions, 31 deletions
diff --git a/coreutils/nice.c b/coreutils/nice.c
index a347001e3..dbd90648d 100644
--- a/coreutils/nice.c
+++ b/coreutils/nice.c
@@ -7,39 +7,14 @@
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */ 8 */
9 9
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <limits.h>
14#include <errno.h>
15#include <unistd.h>
16#include <sys/resource.h> 10#include <sys/resource.h>
17#include "busybox.h" 11#include "busybox.h"
18 12
19static inline int int_add_no_wrap(int a, int b)
20{
21 int s = a + b;
22
23 if (b < 0) {
24 if (s > a) s = INT_MIN;
25 } else {
26 if (s < a) s = INT_MAX;
27 }
28
29 return s;
30}
31
32int nice_main(int argc, char **argv) 13int nice_main(int argc, char **argv)
33{ 14{
34 static const char Xetpriority_msg[] = "cannot %cet priority";
35
36 int old_priority, adjustment; 15 int old_priority, adjustment;
37 16
38 errno = 0; /* Needed for getpriority error detection. */
39 old_priority = getpriority(PRIO_PROCESS, 0); 17 old_priority = getpriority(PRIO_PROCESS, 0);
40 if (errno) {
41 bb_perror_msg_and_die(Xetpriority_msg, 'g');
42 }
43 18
44 if (!*++argv) { /* No args, so (GNU) output current nice value. */ 19 if (!*++argv) { /* No args, so (GNU) output current nice value. */
45 bb_printf("%d\n", old_priority); 20 bb_printf("%d\n", old_priority);
@@ -48,19 +23,26 @@ int nice_main(int argc, char **argv)
48 23
49 adjustment = 10; /* Set default adjustment. */ 24 adjustment = 10; /* Set default adjustment. */
50 25
51 if ((argv[0][0] == '-') && (argv[0][1] == 'n') && !argv[0][2]) { /* "-n" */ 26 if (argv[0][0] == '-') {
27 if (argv[0][1] == 'n') { /* -n */
28 if (argv[0][2]) { /* -nNNNN (w/o space) */
29 argv[0] += 2; argv--; argc++;
30 }
31 } else { /* -NNN (NNN may be negative) == -n NNN */
32 argv[0] += 1; argv--; argc++;
33 }
52 if (argc < 4) { /* Missing priority and/or utility! */ 34 if (argc < 4) { /* Missing priority and/or utility! */
53 bb_show_usage(); 35 bb_show_usage();
54 } 36 }
55 adjustment = xatoi(argv[1]); 37 adjustment = xatoi_range(argv[1], INT_MIN/2, INT_MAX/2);
56 argv += 2; 38 argv += 2;
57 } 39 }
58 40
59 { /* Set our priority. Handle integer wrapping for old + adjust. */ 41 { /* Set our priority. */
60 int new_priority = int_add_no_wrap(old_priority, adjustment); 42 int prio = old_priority + adjustment;
61 43
62 if (setpriority(PRIO_PROCESS, 0, new_priority) < 0) { 44 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
63 bb_perror_msg_and_die(Xetpriority_msg, 's'); 45 bb_perror_msg_and_die("setpriority(%d)", prio);
64 } 46 }
65 } 47 }
66 48