26 #if !defined(HAVE_STRERROR) && !defined(__LCLINT__)
30 extern char * sys_errlist[];
32 if ((0 <= errno) && (errno < sys_nerr))
33 return sys_errlist[errno];
35 return POPT_(
"unknown errno");
41 static void prtcon(
const char *msg,
poptContext con)
43 if (msg) fprintf(stderr,
"%s", msg);
44 fprintf(stderr,
"\tcon %p os %p nextCharArg \"%s\" nextArg \"%s\" argv[%d] \"%s\"\n",
70 if (opt->
arg == NULL)
continue;
72 void * arg = opt->
arg;
96 if (opt->
arg == NULL)
continue;
98 void * arg = opt->
arg;
119 const void * myData,
int shorty)
132 if (opt->
arg != NULL)
138 }
else if (cbopt != NULL &&
163 const struct poptOption * options,
unsigned int flags)
167 if (con == NULL)
return NULL;
168 memset(con, 0,
sizeof(*con));
195 if (getenv(
"POSIXLY_CORRECT") || getenv(
"POSIX_ME_HARDER"))
199 char * t = malloc(strlen(name) + 1);
200 if (t) con->
appName = strcpy(t, name);
224 if (con == NULL)
return;
267 for (i = con->
numExecs - 1; i >= 0; i--) {
268 item = con->
execs + i;
282 if (con->
doExec == NULL) {
297 {
char *s = malloc((longName ? strlen(longName) : 0) + 3);
300 sprintf(s,
"--%s", longName);
302 sprintf(s,
"-%c", shortName);
313 const char *
longName,
size_t longNameLen,
315 const char * nextArg)
355 if (longName == NULL && nextArg && *nextArg)
367 if (longName && nextArg && *nextArg) {
369 av = alloca((ac + 1 + 1) *
sizeof(*av));
370 for (i = 0; i < ac; i++)
380 return (rc ? rc : 1);
388 const char ** argv = NULL;
396 if (item->
argv == NULL || item->
argc < 1 ||
400 argv = malloc(
sizeof(*argv) *
404 if (!strchr(item->
argv[0],
'/') && con->
execPath != NULL) {
405 char *s = malloc(strlen(con->
execPath) + strlen(item->
argv[0]) +
sizeof(
"/"));
411 if (argv[argc++] == NULL) {
416 if (item->
argc > 1) {
417 memcpy(argv + argc, item->
argv + 1,
sizeof(*argv) * (item->
argc - 1));
418 argc += (item->
argc - 1);
434 #if defined(hpux) || defined(__hpux)
435 rc = setresgid(getgid(), getgid(),-1);
437 rc = setresuid(getuid(), getuid(),-1);
445 #if defined(HAVE_SETUID)
446 rc = setgid(getgid());
448 rc = setuid(getuid());
450 #elif defined (HAVE_SETREUID)
451 rc = setregid(getgid(), getgid());
453 rc = setreuid(getuid(), getuid());
463 fprintf(stderr,
"==> execvp(%s) argv[%d]:", argv[0], argc);
464 for (avp = argv; *avp; avp++)
465 fprintf(stderr,
" '%s'", *avp);
466 fprintf(stderr,
"\n");
471 rc = execvp(argv[0], (
char *
const *)argv);
478 free((
void *)argv[0]);
488 const void ** callbackData,
495 if (singleDash && !shortName && (longName && *longName ==
'\0'))
507 if (arg == NULL)
continue;
508 opt2 =
findOption(arg, longName, longNameLen, shortName, callback,
509 callbackData, singleDash);
510 if (opt2 == NULL)
continue;
512 if (!(callback && *callback))
return opt2;
513 if (!(callbackData && *callbackData == NULL))
return opt2;
520 }
else if (longName != NULL && opt->
longName != NULL &&
522 (!strncmp(longName, opt->
longName, longNameLen) && strlen(opt->
longName) == longNameLen))
525 }
else if (shortName && shortName == opt->
shortName) {
534 if (callback) *callback = NULL;
535 if (callbackData) *callbackData = NULL;
554 unsigned argx,
int delete_arg)
567 if (os->
argv != NULL)
568 for (i = os->
next; i < os->
argc; i++) {
572 if (*os->
argv[i] ==
'-')
579 if (os->
argb != NULL)
586 }
while (arg == NULL);
596 const char * a = NULL;
599 size_t tn = strlen(s) + 1;
602 te = t = malloc(tn);;
603 if (t == NULL)
return NULL;
604 while ((c = *s++) !=
'\0') {
612 if (!(s[0] ==
'#' && s[1] ==
':' && s[2] ==
'+'))
626 strncpy(te, a, alen); te += alen;
635 t = realloc(t, strlen(t) + 1);
662 if (arg == NULL || (((
unsigned long)arg) & (
sizeof(*arg)-1)))
667 srandom((
unsigned)getpid());
668 srandom((
unsigned)random());
670 aLong = random() % (aLong > 0 ? aLong : -aLong);
700 if (arg == NULL || (((
unsigned long)arg) & (
sizeof(*arg)-1)))
705 srandom((
unsigned)getpid());
706 srandom((
unsigned)random());
708 aLong = random() % (aLong > 0 ? aLong : -aLong);
743 const char * origOptString = NULL;
745 const void * cbData = NULL;
746 const char * longArg = NULL;
770 const char * optString;
781 if (con->
os->
argv != NULL)
784 if (origOptString == NULL)
788 (*origOptString ==
'-' && origOptString[1] ==
'\0'))
802 optString = origOptString;
804 if (optString[0] ==
'\0')
807 if (optString[1] ==
'-' && !optString[2]) {
815 if (*optString ==
'-')
816 singleDash = 0, optString++;
821 for (oe = optString; *oe && *oe !=
'='; oe++)
823 optStringLen = oe - optString;
828 if (
handleAlias(con, optString, optStringLen,
'\0', longArg)) {
838 if (!opt && !singleDash)
861 if (
handleAlias(con, NULL, 0, *origOptString, origOptString + 1))
867 if (*origOptString !=
'\0')
879 if (*origOptString !=
'\0')
922 if (con->
os->
argv != NULL) {
951 aLong = strtol(con->
os->
nextArg, &end, 0);
952 if (!(end && *end ==
'\0'))
957 if (aLong == LONG_MIN || aLong == LONG_MAX)
962 if (aLong > INT_MAX || aLong < INT_MIN)
971 {
double aDouble = 0.0;
976 int saveerrno = errno;
978 aDouble = strtod(con->
os->
nextArg, &end);
988 *((
double *) opt->
arg) = aDouble;
990 #define _ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
991 if ((
_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
993 if ((FLT_MIN -
_ABS(aDouble)) > DBL_EPSILON)
995 *((
float *) opt->
arg) = aDouble;
1005 POPT_(
"option type (%d) not implemented in popt\n"),
1049 return (opt ? opt->
val : -1);
1064 const char * ret = NULL;
1072 const char * ret = NULL;
1097 if (con == NULL)
return con;
1113 if (con->
execs != NULL)
1114 for (i = 0; i < con->
numExecs; i++) {
1115 item = con->
execs + i;
1141 memset(item, 0,
sizeof(*item));
1142 item->option.longName = alias.
longName;
1143 item->option.shortName = alias.
shortName;
1145 item->option.arg = 0;
1146 item->option.val = 0;
1147 item->option.descrip = NULL;
1148 item->option.argDescrip = NULL;
1149 item->argc = alias.
argc;
1150 item->argv = alias.
argv;
1161 items = &con->
execs;
1173 *items = realloc((*items), ((*nitems) + 1) *
sizeof(**items));
1174 if ((*items) == NULL)
1177 item = (*items) + (*nitems);
1200 const char *badOpt = NULL;
1219 return POPT_(
"missing argument");
1221 return POPT_(
"unknown option");
1223 return POPT_(
"mutually exclusive logical operations requested");
1225 return POPT_(
"opt->arg should not be NULL");
1227 return POPT_(
"aliases nested too deeply");
1229 return POPT_(
"error in parameter quoting");
1231 return POPT_(
"invalid numeric value");
1233 return POPT_(
"number too large or too small");
1235 return POPT_(
"memory allocation failed");
1239 return POPT_(
"unknown error");
1251 for (argc = 0; argv[
argc]; argc++)
1279 for (i = 1; i <
argc; i++) {
1284 for (i = 1; i <
argc; i++) {
1287 argv[j] = (j < numargs) ? argv[i] : NULL;