comparison toys/pending/renice.c @ 978:6d3c39cb8a9d

Cleanup renice and implement '|' (required option) in argument parsing.
author Rob Landley <rob@landley.net>
date Wed, 31 Jul 2013 03:24:58 -0500
parents 9a96527bea94
children
comparison
equal deleted inserted replaced
977:9a96527bea94 978:6d3c39cb8a9d
3 * Copyright 2013 CE Strake <strake888 at gmail.com> 3 * Copyright 2013 CE Strake <strake888 at gmail.com>
4 * 4 *
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ 5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/
6 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html 6 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html
7 7
8 USE_RENICE(NEWTOY(renice, "gpun#", TOYFLAG_BIN)) 8 USE_RENICE(NEWTOY(renice, "<1gpun#|", TOYFLAG_BIN))
9 9
10 config RENICE 10 config RENICE
11 bool "renice" 11 bool "renice"
12 default n 12 default n
13 help 13 help
19 19
20 GLOBALS( 20 GLOBALS(
21 long nArgu; 21 long nArgu;
22 ) 22 )
23 23
24 void renice_main (void) { 24 void renice_main(void) {
25 int ii; 25 int which = (toys.optflags & FLAG_g) ? PRIO_PGRP :
26 int which = toys.optflags & FLAG_g ? PRIO_PGRP : 26 ((toys.optflags & FLAG_u) ? PRIO_USER : PRIO_PROCESS);
27 toys.optflags & FLAG_u ? PRIO_USER : 27 char **arg;
28 PRIO_PROCESS;
29 28
30 if (!(toys.optflags & FLAG_n)) error_exit ("no increment given"); 29 for (arg = toys.optargs; *arg; arg++) {
30 char *s = *arg;
31 int id = -1;
31 32
32 for (ii = 0; ii < toys.optc; ii++) { 33 if (toys.optflags & FLAG_u) {
33 id_t id; 34 struct passwd *p = getpwnam(s);
35 if (p) id = p->pw_uid;
36 } else {
37 id = strtol(s, &s, 10);
38 if (*s) id = -1;
39 }
34 40
35 if (isdigit (toys.optargs[ii][0])) id = strtoul (toys.optargs[ii], 0, 10); 41 if (id < 0) {
36 else if (toys.optflags & FLAG_u) id = getpwnam (toys.optargs[ii]) -> pw_uid; 42 error_msg("bad '%s'", *arg);
37 else {
38 error_msg ("not a number: %s", toys.optargs[ii]);
39 toys.exitval = 1;
40 continue; 43 continue;
41 } 44 }
42 45
43 if (setpriority (which, id, getpriority (which, id) + TT.nArgu) < 0) { 46 if (setpriority(which, id, getpriority(which, id)+TT.nArgu) < 0)
44 error_msg ("failed to setpriority of %d", id); 47 perror_msg("setpriority %d", id);
45 toys.exitval = 1;
46 }
47 } 48 }
48 } 49 }