annotate toys/posix/renice.c @ 1183:0752b2d58909 draft

Rename xmsprintf() to just xmprintf(). Partly because there's no supplied target string ala sprintf, and partly because I can never remember what order the m and s go in.
author Rob Landley <rob@landley.net>
date Thu, 16 Jan 2014 09:26:50 -0600
parents 53a9bcb938f0
children 57f2a26fa92c
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 *
979
53a9bcb938f0 Move renice from pending to posix, default y, fix link to standard.
Rob Landley <rob@landley.net>
parents: 978
diff changeset
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/renice.html
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
6
978
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
7 USE_RENICE(NEWTOY(renice, "<1gpun#|", TOYFLAG_BIN))
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
8
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
9 config RENICE
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
10 bool "renice"
979
53a9bcb938f0 Move renice from pending to posix, default y, fix link to standard.
Rob Landley <rob@landley.net>
parents: 978
diff changeset
11 default y
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
12 help
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
13 usage: renice [-gpu] -n increment ID ...
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
14 */
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 #define FOR_renice
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
17 #include "toys.h"
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
18
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
19 GLOBALS(
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
20 long nArgu;
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
21 )
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
22
978
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
23 void renice_main(void) {
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
24 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
25 ((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
26 char **arg;
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
27
978
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
28 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
29 char *s = *arg;
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
30 int id = -1;
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
31
978
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
32 if (toys.optflags & FLAG_u) {
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
33 struct passwd *p = getpwnam(s);
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
34 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
35 } else {
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
36 id = strtol(s, &s, 10);
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
37 if (*s) id = -1;
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
38 }
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
39
978
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
40 if (id < 0) {
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
41 error_msg("bad '%s'", *arg);
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
42 continue;
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
43 }
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
44
978
6d3c39cb8a9d Cleanup renice and implement '|' (required option) in argument parsing.
Rob Landley <rob@landley.net>
parents: 977
diff changeset
45 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
46 perror_msg("setpriority %d", id);
977
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
47 }
9a96527bea94 Add renice.
M. Farkas-Dyck <strake888@gmail.com>
parents:
diff changeset
48 }