继续

其实接前面,整个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. C++类的构造、拷贝构造、析构函数等

    1: 一个空的class在C++编译器处理过后就不再为空,编译器会自动地为我们声明一些member function,如果你写 class A{}; 编译器处理后,就相当于: class A{ pub ...

  2. session服务器Nginx+Tomcat+Memcached集群Session共享

    cookie是怎样工作的? 例如,我们创立了一个名字为login的Cookie来包含访问者的信息,创立Cookie时,服务器端的Header如下面所示,这里假设访问者的注册名是“Michael Jor ...

  3. Android进度加载的Loading效果

    网上看到的一个开源项目的loading效果,效果很赞,记录一下: 开源项目地址如下:https://github.com/RomainPiel/Titanic

  4. 【转】.. Android应用内存泄露分析、改善经验总结

    原文网址:http://wetest.qq.com/lab/view/107.html?from=ads_test2_qqtips&sessionUserType=BFT.PARAMS.194 ...

  5. 构造函数后面的base()

    先执行父类的对应的构造函数,再执行当前的构造函数. 关于子类对象的构造函数和父类构造函数的执行顺序 以下内容转自:http://blog.csdn.net/todototry/article/deta ...

  6. iOS 获取已连接的wifi信息

    转:http://blog.csdn.net/marujunyy/article/details/16843173 首先需要   #import <SystemConfiguration/Cap ...

  7. [转] ArcGIS engine中气泡标注的添加、修改

    小生 原文 ArcGIS engine中气泡标注的添加.修改! 你微微地笑着,不同我说什么话.而我觉得,为了这个,我已等待得久了.                                   ...

  8. ubuntu下安装selenium2.0 环境

    参考:http://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html ubuntu 安装过程: 1.安装:setuptools $ apt-ge ...

  9. CSS使用简介

    1.CSS 指层叠样式表 (Cascading Style Sheets) 2.说明: 样式定义如何显示 HTML 元素:     样式通常存储在样式表中:     把样式添加到 HTML 4.0 中 ...

  10. Emacs Lisp 功能扩展集锦

    http://docs.huihoo.com/homepage/shredderyin/emacs_elisp.html Emacs 具有超强的扩展性.这是当今没有任何其它编辑器可以比拟 的强大特点. ...