@H_
301_2@
在initdb.c中,传入的参数为:
while ((c = getopt_long(argc,argv,"dD:E:kL:nNU:WA:sST:X:",long_options,&option_index)) != -1)
@H_
301_2@
该
函数依次验证argv[i] 1<=i <argc
@H_
301_2@
另外 "dD:E:kL:nNU:WA:sST:X:",这种写法也是有针对性的。后面无冒号则不带参数,否则必须带参数
@H_
301_2@
@H_
301_2@
/*
* getopt_long
* Parse argc/argv argument vector,with long options.
*
* This implementation does not use optreset. Instead,we guarantee that
* it can be restarted on a new argv array after a prevIoUs call returned -1,* if the caller resets optind to 1 before the first call of the new series.
* (Internally,this means we must be sure to reset "place" to EMSG before
* returning -1.)
*/
int
getopt_long(int argc,char *const argv[],const char *optstring,const struct option * longopts,int *longindex)
{
static char *place = EMSG; /* option letter processing */
char *oli; /* option letter list index */
if (!*place)
{ /* update scanning pointer */
if (optind >= argc)
{
place = EMSG;
return -1;
}
place = argv[optind];
if (place[0] != '-')
{
place = EMSG;
return -1;
}
place++;
if (place[0] && place[0] == '-' && place[1] == '\0')
{ /* found "--" */
++optind;
place = EMSG;
return -1;
}
if (place[0] && place[0] == '-' && place[1])
{
/* long option */
size_t namelen;
int i;
place++;
namelen = strcspn(place,"=");
for (i = 0; longopts[i].name != NULL; i++)
{
if (strlen(longopts[i].name) == namelen
&& strncmp(place,longopts[i].name,namelen) == 0)
{
if (longopts[i].has_arg)
{
if (place[namelen] == '=')
optarg = place + namelen + 1;
else if (optind < argc - 1)
{
optind++;
optarg = argv[optind];
}
else
{
if (optstring[0] == ':')
return BADARG;
if (opterr)
fprintf(stderr,"%s: option requires an argument -- %s\n",argv[0],place);
place = EMSG;
optind++;
return BADCH;
}
}
else
{
optarg = NULL;
if (place[namelen] != 0)
{
/* XXX error? */
}
}
optind++;
if (longindex)
*longindex = i;
place = EMSG;
if (longopts[i].flag == NULL)
return longopts[i].val;
else
{
*longopts[i].flag = longopts[i].val;
return 0;
}
}
}
if (opterr && optstring[0] != ':')
fprintf(stderr,"%s: illegal option -- %s\n",place);
place = EMSG;
optind++;
return BADCH;
}
}
/* short option */
optopt = (int) *place++;
oli = strchr(optstring,optopt);
if (!oli)
{
if (!*place)
++optind;
if (opterr && *optstring != ':')
fprintf(stderr,"%s: illegal option -- %c\n",optopt);
return BADCH;
}
if (oli[1] != ':')
{ /* don't need argument */
optarg = NULL;
if (!*place)
++optind;
}
else
{ /* need an argument */
if (*place) /* no white space */
optarg = place;
else if (argc <= ++optind)
{ /* no arg */
place = EMSG;
if (*optstring == ':')
return BADARG;
if (opterr)
fprintf(stderr,"%s: option requires an argument -- %c\n",optopt);
return BADCH;
}
else
/* white space */
optarg = argv[optind];
place = EMSG;
++optind;
}
return optopt;
}
optarg 会传送到initdb.c中 ,其值为各个 参数对应的值 (如 -D ./data 那么optarg 为“./data”)
@H_