diff options
author | jilles <jilles@FreeBSD.org> | 2012-09-15 21:56:30 +0000 |
---|---|---|
committer | jilles <jilles@FreeBSD.org> | 2012-09-15 21:56:30 +0000 |
commit | 99ca87dd2daf11623d11838ece4dba4e0ab94951 (patch) | |
tree | 8f1502baabb3376aec513c46c8781c9f452a7b3d /bin/sh/cd.c | |
parent | 56fd9326eeef1f0702902d3716e1551e76049d94 (diff) | |
download | FreeBSD-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/sh/cd.c')
-rw-r--r-- | bin/sh/cd.c | 26 |
1 files changed, 7 insertions, 19 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()) { |