继续

其实接前面,整个while循环是这样的:

    while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:", long_options, &option_index)) != -)
{
switch (c)
{
......
} ......
}

这一句,c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:", long_options, &option_index),

除了获得initdb 后跟着的是 -D 还是 -E之类的,还有一个用途,就是给 全局变量 optarg 赋值。这个全局变量其实挺坑爹的。

/*
* 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)
{
......
if (!*place)
{ /* update scanning pointer */
...... if (place[] && place[] == '-' && place[])
{
......for (i = ; longopts[i].name != NULL; i++)
{
if (strlen(longopts[i].name) == namelen
&& strncmp(place, longopts[i].name, namelen) == )
{
if (longopts[i].has_arg)
{
if (place[namelen] == '=')
optarg = place + namelen + ;
else if (optind < argc - )
{
optind++;
optarg = argv[optind];
}
else
{
......
}
}
else
{
......
} ......
}
}
......
}
} ...... if (oli[] != ':')
{ /* don't need argument */ ......
}
else
{ /* need an argument */
if (*place) /* no white space */
optarg = place;
else if (argc <= ++optind)
{ /* no arg */ ......
}
else
/* white space */
optarg = argv[optind];
place = EMSG;
++optind;
}
return optopt;
}

我经过运行,得到的是:

optarg 变量的值为  /home/pgsql/DemoDir。

通过下面的代码,pg_data 被赋值为  /home/pgsql/DemoDir。

    /* process command-line options */

    while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:", long_options, &option_index)) != -)
{
switch (c)
{
case 'A':
authmethod = xstrdup(optarg);
break;
case 'D': pg_data = xstrdup(optarg);
break;
......
}
......
}

继续分析。

main函数的下一段,其中的 optiond,也是由前面的  getopt_long 函数计算出来的,我运行时候,其值为 3。

如下代码,是异常处理,可以跳过。

    if (optind < argc)
{
pg_data = xstrdup(argv[optind]);
optind++;
}
if (optind < argc)
{
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + ]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
progname);
exit();
}

PostgreSQL的 initdb 源代码分析之三的更多相关文章

  1. PostgreSQL的initdb 源代码分析之六

    继续分析 下面的是获取运行此程序的用户名称,主要还是为了防止在linux下用root来运行的情形. effective_user = get_id(); ) username = effective_ ...

  2. PostgreSQL的 initdb 源代码分析之二

    继续分析 下面这一段,当 initdb --version 或者  initdb --help 才有意义. ) { ], || strcmp(argv[], ) { usage(progname); ...

  3. PostgreSQL的 initdb 源代码分析之二十四

    继续分析: make_template0(); 展开: 无需再作解释,就是创建template0数据库 /* * copy template1 to template0 */ static void ...

  4. PostgreSQL的 initdb 源代码分析之十五

    继续分析: if (pwprompt || pwfilename) get_set_pwd(); 由于我启动initdb的时候,没有设置口令相关的选项,故此略过. 接下来: setup_depend( ...

  5. PostgreSQL的 initdb 源代码分析之十三

    继续分析: /* Bootstrap template1 */ bootstrap_template1(); 展开: 我这里读入的文件是:/home/pgsql/project/share/postg ...

  6. PostgreSQL的 initdb 源代码分析之十二

    继续分析 /* Now create all the text config files */ setup_config(); 将其展开: 实质就是,确定各种参数,分别写入 postgresql.co ...

  7. PostgreSQL的 initdb 源代码分析之十一

    继续分析: /* Top level PG_VERSION is checked by bootstrapper, so make it first */ write_version_file(NUL ...

  8. PostgreSQL的 initdb 源代码分析之七

    继续分析:由于我使用initdb的时候,没有指定 locale,所以会使用OS的缺省locale,这里是 en_US.UTF-8 printf(_("The files belonging ...

  9. PostgreSQL的initdb 源代码分析之五

    接前面,继续分析: putenv("TZ=GMT") 设置了时区信息. find_other_exec(argv[0], "postgres", PG_BACK ...

随机推荐

  1. 使php支持mbstring库

    多国语言并存就意味着多字节,PHP内置的字符串长度函数strlen无法正确处理中文字符串,它得到的只是字符串所占的字节数.对于GB2312的中文编码,strlen得到的值是汉字个数的2倍,而对于UTF ...

  2. Squid故障

    1.COSS will not function without large file support (off_t is 4 bytes long. Please reconsider recomp ...

  3. Spring学习之基本概念

    Spring 基本概念 Spring优点: 1.Spring不同于其它的Framework,它要提供的是一种管理你的业务对象的方法. 2.DI有效的降低了耦合度 3.AOP提供了通用任务的集中管理 4 ...

  4. SharePoint 2010遍历文档库中所有的文件,文件夹

    转:http://hi.baidu.com/sygwin/item/f99600849d51a12b110ef3eb 创建一个可视WebPart,并拖放一个label控件到ascx文件上,用于显示结果 ...

  5. Druid连接池简单入门

    偶尔的机会解释Druid连接池,后起之秀,但是评价不错,另外由于是阿里淘宝使用过的所以还是蛮看好的. 1.jar包依赖--Druid依赖代码 <dependency> <groupI ...

  6. golang学习之指针、内存分配

    func pointer_test() { //空指针,输出为nil var p *int fmt.Printf("p: %v\n", p) //指向局部变量,变量值初始为0 va ...

  7. Codeforces 167B Wizards and Huge Prize(概率dp)

    题意: n个人,开始有一个容量为k得背包,击败一个人背包可以获得一定容量或得到一个财富(放入背包内),给出击败每个人的概率,求至少击败l个人,且背包容量大于获得的总财富值的概率 分析: 状态好确定,d ...

  8. 【原】Storm Tutorial

    Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...

  9. lighttpd+fastcgi模块分析

    一开始不怎么明白fastcgi和cgi的区别,查了资料说,fastcgi多了一个进程池,不要每次都fork和退出 这个不是重点,还是对着代码看吧 怎样在lighttpd运行php呢,需要下面这样配置 ...

  10. Tkinter教程之Frame篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811339 '''Tkinter教程之Frame篇'''#Frame就是屏幕上的一块矩形区域, ...