summaryrefslogtreecommitdiffstats
path: root/bin/sh/histedit.c
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/sh/histedit.c
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/sh/histedit.c')
-rw-r--r--bin/sh/histedit.c44
1 files changed, 15 insertions, 29 deletions
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.
*/
OpenPOWER on IntegriCloud