summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2012-09-15 21:56:30 +0000
committerjilles <jilles@FreeBSD.org>2012-09-15 21:56:30 +0000
commit99ca87dd2daf11623d11838ece4dba4e0ab94951 (patch)
tree8f1502baabb3376aec513c46c8781c9f452a7b3d /bin
parent56fd9326eeef1f0702902d3716e1551e76049d94 (diff)
downloadFreeBSD-src-99ca87dd2daf11623d11838ece4dba4e0ab94951.zip
FreeBSD-src-99ca87dd2daf11623d11838ece4dba4e0ab94951.tar.gz
sh: Prefer internal nextopt() to libc getopt().
This reduces code duplication and code size. /usr/bin/printf is not affected. Side effect: different error messages when certain builtins are passed invalid options.
Diffstat (limited to 'bin')
-rw-r--r--bin/sh/cd.c26
-rw-r--r--bin/sh/eval.c17
-rw-r--r--bin/sh/histedit.c44
-rw-r--r--bin/sh/jobs.c15
-rw-r--r--bin/sh/var.c18
5 files changed, 37 insertions, 83 deletions
diff --git a/bin/sh/cd.c b/bin/sh/cd.c
index 1330c85..fa6f492 100644
--- a/bin/sh/cd.c
+++ b/bin/sh/cd.c
@@ -79,7 +79,7 @@ static char *prevdir; /* previous working directory */
static char *cdcomppath;
int
-cdcmd(int argc, char **argv)
+cdcmd(int argc __unused, char **argv __unused)
{
const char *dest;
const char *path;
@@ -89,9 +89,8 @@ cdcmd(int argc, char **argv)
int rc;
int errno1 = ENOENT;
- optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
phys = Pflag;
- while ((ch = getopt(argc, argv, "eLP")) != -1) {
+ while ((ch = nextopt("eLP")) != '\0') {
switch (ch) {
case 'e':
getcwderr = 1;
@@ -102,18 +101,13 @@ cdcmd(int argc, char **argv)
case 'P':
phys = 1;
break;
- default:
- error("unknown option: -%c", optopt);
- break;
}
}
- argc -= optind;
- argv += optind;
- if (argc > 1)
+ if (*argptr != NULL && argptr[1] != NULL)
error("too many arguments");
- if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
+ if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
error("HOME not set");
if (*dest == '\0')
dest = ".";
@@ -330,14 +324,13 @@ updatepwd(char *dir)
}
int
-pwdcmd(int argc, char **argv)
+pwdcmd(int argc __unused, char **argv __unused)
{
char *p;
int ch, phys;
- optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
phys = Pflag;
- while ((ch = getopt(argc, argv, "LP")) != -1) {
+ while ((ch = nextopt("LP")) != '\0') {
switch (ch) {
case 'L':
phys = 0;
@@ -345,15 +338,10 @@ pwdcmd(int argc, char **argv)
case 'P':
phys = 1;
break;
- default:
- error("unknown option: -%c", optopt);
- break;
}
}
- argc -= optind;
- argv += optind;
- if (argc != 0)
+ if (*argptr != NULL)
error("too many arguments");
if (!phys && getpwd()) {
diff --git a/bin/sh/eval.c b/bin/sh/eval.c
index c9e814b..8973f69 100644
--- a/bin/sh/eval.c
+++ b/bin/sh/eval.c
@@ -1223,7 +1223,7 @@ breakcmd(int argc, char **argv)
* The `command' command.
*/
int
-commandcmd(int argc, char **argv)
+commandcmd(int argc __unused, char **argv __unused)
{
const char *path;
int ch;
@@ -1231,9 +1231,7 @@ commandcmd(int argc, char **argv)
path = bltinlookup("PATH", 1);
- optind = optreset = 1;
- opterr = 0;
- while ((ch = getopt(argc, argv, "pvV")) != -1) {
+ while ((ch = nextopt("pvV")) != '\0') {
switch (ch) {
case 'p':
path = _PATH_STDPATH;
@@ -1244,20 +1242,15 @@ commandcmd(int argc, char **argv)
case 'V':
cmd = TYPECMD_BIGV;
break;
- case '?':
- default:
- error("unknown option: -%c", optopt);
}
}
- argc -= optind;
- argv += optind;
if (cmd != -1) {
- if (argc != 1)
+ if (*argptr == NULL || argptr[1] != NULL)
error("wrong number of arguments");
- return typecmd_impl(2, argv - 1, cmd, path);
+ return typecmd_impl(2, argptr - 1, cmd, path);
}
- if (argc != 0)
+ if (*argptr != NULL)
error("commandcmd bad call");
/*
diff --git a/bin/sh/histedit.c b/bin/sh/histedit.c
index 6371599..a8c376a 100644
--- a/bin/sh/histedit.c
+++ b/bin/sh/histedit.c
@@ -182,7 +182,7 @@ setterm(const char *term)
}
int
-histcmd(int argc, char **argv)
+histcmd(int argc, char **argv __unused)
{
int ch;
const char *editor = NULL;
@@ -206,13 +206,10 @@ histcmd(int argc, char **argv)
if (argc == 1)
error("missing history argument");
- optreset = 1; optind = 1; /* initialize getopt */
- opterr = 0;
- while (not_fcnumber(argv[optind]) &&
- (ch = getopt(argc, argv, ":e:lnrs")) != -1)
+ while (not_fcnumber(*argptr) && (ch = nextopt("e:lnrs")) != '\0')
switch ((char)ch) {
case 'e':
- editor = optarg;
+ editor = shoptarg;
break;
case 'l':
lflg = 1;
@@ -226,13 +223,7 @@ histcmd(int argc, char **argv)
case 's':
sflg = 1;
break;
- case ':':
- error("option -%c expects argument", optopt);
- case '?':
- default:
- error("unknown option: -%c", optopt);
}
- argc -= optind, argv += optind;
savehandler = handler;
/*
@@ -276,31 +267,26 @@ histcmd(int argc, char **argv)
/*
* If executing, parse [old=new] now
*/
- if (lflg == 0 && argc > 0 &&
- ((repl = strchr(argv[0], '=')) != NULL)) {
- pat = argv[0];
+ if (lflg == 0 && *argptr != NULL &&
+ ((repl = strchr(*argptr, '=')) != NULL)) {
+ pat = *argptr;
*repl++ = '\0';
- argc--, argv++;
+ argptr++;
}
/*
* determine [first] and [last]
*/
- switch (argc) {
- case 0:
+ if (*argptr == NULL) {
firststr = lflg ? "-16" : "-1";
laststr = "-1";
- break;
- case 1:
- firststr = argv[0];
- laststr = lflg ? "-1" : argv[0];
- break;
- case 2:
- firststr = argv[0];
- laststr = argv[1];
- break;
- default:
+ } else if (argptr[1] == NULL) {
+ firststr = argptr[0];
+ laststr = lflg ? "-1" : argptr[0];
+ } else if (argptr[2] == NULL) {
+ firststr = argptr[0];
+ laststr = argptr[1];
+ } else
error("too many arguments");
- }
/*
* Turn into event numbers.
*/
diff --git a/bin/sh/jobs.c b/bin/sh/jobs.c
index 99efc80..05e2e88 100644
--- a/bin/sh/jobs.c
+++ b/bin/sh/jobs.c
@@ -250,15 +250,13 @@ restartjob(struct job *jp)
int
-jobscmd(int argc, char *argv[])
+jobscmd(int argc __unused, char *argv[] __unused)
{
char *id;
int ch, mode;
- optind = optreset = 1;
- opterr = 0;
mode = SHOWJOBS_DEFAULT;
- while ((ch = getopt(argc, argv, "lps")) != -1) {
+ while ((ch = nextopt("lps")) != '\0') {
switch (ch) {
case 'l':
mode = SHOWJOBS_VERBOSE;
@@ -269,18 +267,13 @@ jobscmd(int argc, char *argv[])
case 's':
mode = SHOWJOBS_PIDS;
break;
- case '?':
- default:
- error("unknown option: -%c", optopt);
}
}
- argc -= optind;
- argv += optind;
- if (argc == 0)
+ if (*argptr == NULL)
showjobs(0, mode);
else
- while ((id = *argv++) != NULL)
+ while ((id = *argptr++) != NULL)
showjob(getjob(id), mode);
return (0);
diff --git a/bin/sh/var.c b/bin/sh/var.c
index 6041459..48a0dc5 100644
--- a/bin/sh/var.c
+++ b/bin/sh/var.c
@@ -640,10 +640,11 @@ showvarscmd(int argc __unused, char **argv __unused)
*/
int
-exportcmd(int argc, char **argv)
+exportcmd(int argc __unused, char **argv)
{
struct var **vpp;
struct var *vp;
+ char **ap;
char *name;
char *p;
char *cmdname;
@@ -651,26 +652,19 @@ exportcmd(int argc, char **argv)
int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
cmdname = argv[0];
- optreset = optind = 1;
- opterr = 0;
values = 0;
- while ((ch = getopt(argc, argv, "p")) != -1) {
+ while ((ch = nextopt("p")) != '\0') {
switch (ch) {
case 'p':
values = 1;
break;
- case '?':
- default:
- error("unknown option: -%c", optopt);
}
}
- argc -= optind;
- argv += optind;
- if (values && argc != 0)
+ if (values && *argptr != NULL)
error("-p requires no arguments");
- if (argc != 0) {
- while ((name = *argv++) != NULL) {
+ if (*argptr != NULL) {
+ for (ap = argptr; (name = *ap) != NULL; ap++) {
if ((p = strchr(name, '=')) != NULL) {
p++;
} else {
OpenPOWER on IntegriCloud