Subject: Sample code in getopt(3)
To: None <tech-userlevel@netbsd.org>
From: Ben Harris <bjh21@netbsd.org>
List: tech-userlevel
Date: 06/25/2003 00:49:01
getopt(3) contains the following sample code for parsing -<number>
options:
int length;
char *p;
while ((c = getopt(argc, argv, "0123456789")) != -1) {
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
p = argv[optind - 1];
if (p[0] == '-' && p[1] == ch && !p[2])
length = atoi(++p);
else
length = atoi(argv[optind] + 1);
break;
}
}
Can anyone explain what the second branch of the "if" is for, and how to
trigger it? For bonus points, explain how to add an extra option (say,
"-a") in such a way that an argument of "-a0" doesn't cause a segfault
there.
[ Background: I'm trying to clean up split(1), which contains similar
code, and can be segfaulted by running "split --1". ]
--
Ben Harris <bjh21@netbsd.org>
Portmaster, NetBSD/acorn26 <URL:http://www.netbsd.org/Ports/acorn26/>