aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2013-07-31 03:24:58 -0500
committerRob Landley <rob@landley.net>2013-07-31 03:24:58 -0500
commit9e89d47a29dbcc353004c968c0b5da05ff89c653 (patch)
treeee15612f96efc88b7ed7439cb0594bde63959981
parent0c03786b5ab1a81bf5abdbd4bc0be9d7379df9dd (diff)
downloadtoybox-9e89d47a29dbcc353004c968c0b5da05ff89c653.tar.gz
Cleanup renice and implement '|' (required option) in argument parsing.
-rw-r--r--lib/args.c17
-rw-r--r--toys/pending/renice.c41
2 files changed, 36 insertions, 22 deletions
diff --git a/lib/args.c b/lib/args.c
index ae608e03..c551f868 100644
--- a/lib/args.c
+++ b/lib/args.c
@@ -108,7 +108,7 @@ struct getoptflagstate
struct opts *opts;
struct longopts *longopts;
int noerror, nodash_now, stopearly;
- unsigned excludes;
+ unsigned excludes, requires;
};
// Use getoptflagstate to parse parse one command line option from argv
@@ -289,7 +289,10 @@ void parse_optflaglist(struct getoptflagstate *gof)
// (This goes right to left so we need the whole list before we can start.)
idx = 0;
for (new = gof->opts; new; new = new->next) {
- new->dex[1] = 1<<idx++;
+ unsigned u = 1<<idx++;
+
+ new->dex[1] = u;
+ if (new->flags & 1) gof->requires |= u;
if (new->type) {
new->arg = (void *)nextarg;
*(nextarg++) = new->val[2].l;
@@ -442,6 +445,16 @@ notflag:
gof.minargs, letters[!(gof.minargs-1)]);
if (toys.optc>gof.maxargs)
error_exit("Max %d argument%s", gof.maxargs, letters[!(gof.maxargs-1)]);
+ if (gof.requires && !(gof.requires & toys.optflags)) {
+ struct opts *req;
+ char needs[32], *s = needs;
+
+ for (req = gof.opts; req; req = req->next)
+ if (req->flags & 1) *(s++) = req->c;
+ *s = 0;
+
+ error_exit("Needs %s-%s", s[1] ? "one of " : "", needs);
+ }
toys.exithelp = 0;
if (CFG_TOYBOX_FREE) {
diff --git a/toys/pending/renice.c b/toys/pending/renice.c
index 305ad194..6d0bdac1 100644
--- a/toys/pending/renice.c
+++ b/toys/pending/renice.c
@@ -5,7 +5,7 @@
* See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/
* See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html
-USE_RENICE(NEWTOY(renice, "gpun#", TOYFLAG_BIN))
+USE_RENICE(NEWTOY(renice, "<1gpun#|", TOYFLAG_BIN))
config RENICE
bool "renice"
@@ -21,28 +21,29 @@ GLOBALS(
long nArgu;
)
-void renice_main (void) {
- int ii;
- int which = toys.optflags & FLAG_g ? PRIO_PGRP :
- toys.optflags & FLAG_u ? PRIO_USER :
- PRIO_PROCESS;
-
- if (!(toys.optflags & FLAG_n)) error_exit ("no increment given");
-
- for (ii = 0; ii < toys.optc; ii++) {
- id_t id;
+void renice_main(void) {
+ int which = (toys.optflags & FLAG_g) ? PRIO_PGRP :
+ ((toys.optflags & FLAG_u) ? PRIO_USER : PRIO_PROCESS);
+ char **arg;
+
+ for (arg = toys.optargs; *arg; arg++) {
+ char *s = *arg;
+ int id = -1;
+
+ if (toys.optflags & FLAG_u) {
+ struct passwd *p = getpwnam(s);
+ if (p) id = p->pw_uid;
+ } else {
+ id = strtol(s, &s, 10);
+ if (*s) id = -1;
+ }
- if (isdigit (toys.optargs[ii][0])) id = strtoul (toys.optargs[ii], 0, 10);
- else if (toys.optflags & FLAG_u) id = getpwnam (toys.optargs[ii]) -> pw_uid;
- else {
- error_msg ("not a number: %s", toys.optargs[ii]);
- toys.exitval = 1;
+ if (id < 0) {
+ error_msg("bad '%s'", *arg);
continue;
}
- if (setpriority (which, id, getpriority (which, id) + TT.nArgu) < 0) {
- error_msg ("failed to setpriority of %d", id);
- toys.exitval = 1;
- }
+ if (setpriority(which, id, getpriority(which, id)+TT.nArgu) < 0)
+ perror_msg("setpriority %d", id);
}
}