annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
1 /* renice.c - renice process
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
2 *
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
3 * Copyright 2013 CE Strake <strake888 at gmail.com>
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
4 *
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
6 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
7
978
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
8 USE_RENICE(NEWTOY(renice, "<1gpun#|", TOYFLAG_BIN))
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
9
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
10 config RENICE
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
11 bool "renice"
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
12 default n
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
13 help
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
14 usage: renice [-gpu] -n increment ID ...
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
15 */
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
16
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
17 #define FOR_renice
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
18 #include "toys.h"
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
19
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
20 GLOBALS(
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
21 long nArgu;
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
22 )
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
23
978
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
24 void renice_main(void) {
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
25 int which = (toys.optflags & FLAG_g) ? PRIO_PGRP :
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
26 ((toys.optflags & FLAG_u) ? PRIO_USER : PRIO_PROCESS);
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
27 char **arg;
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
28
978
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
29 for (arg = toys.optargs; *arg; arg++) {
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
30 char *s = *arg;
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
31 int id = -1;
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
32
978
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
33 if (toys.optflags & FLAG_u) {
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
34 struct passwd *p = getpwnam(s);
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
35 if (p) id = p->pw_uid;
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
36 } else {
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
37 id = strtol(s, &s, 10);
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
38 if (*s) id = -1;
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
39 }
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
40
978
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
41 if (id < 0) {
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
42 error_msg("bad '%s'", *arg);
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
43 continue;
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
44 }
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
45
978
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
46 if (setpriority(which, id, getpriority(which, id)+TT.nArgu) < 0)
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
47 perror_msg("setpriority %d", id);
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
48 }
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
49 }