PostgreSQL的 initdb 源代码分析之三
继续
其实接前面,整个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 源代码分析之三的更多相关文章
- PostgreSQL的initdb 源代码分析之六
继续分析 下面的是获取运行此程序的用户名称,主要还是为了防止在linux下用root来运行的情形. effective_user = get_id(); ) username = effective_ ...
- PostgreSQL的 initdb 源代码分析之二
继续分析 下面这一段,当 initdb --version 或者 initdb --help 才有意义. ) { ], || strcmp(argv[], ) { usage(progname); ...
- PostgreSQL的 initdb 源代码分析之二十四
继续分析: make_template0(); 展开: 无需再作解释,就是创建template0数据库 /* * copy template1 to template0 */ static void ...
- PostgreSQL的 initdb 源代码分析之十五
继续分析: if (pwprompt || pwfilename) get_set_pwd(); 由于我启动initdb的时候,没有设置口令相关的选项,故此略过. 接下来: setup_depend( ...
- PostgreSQL的 initdb 源代码分析之十三
继续分析: /* Bootstrap template1 */ bootstrap_template1(); 展开: 我这里读入的文件是:/home/pgsql/project/share/postg ...
- PostgreSQL的 initdb 源代码分析之十二
继续分析 /* Now create all the text config files */ setup_config(); 将其展开: 实质就是,确定各种参数,分别写入 postgresql.co ...
- PostgreSQL的 initdb 源代码分析之十一
继续分析: /* Top level PG_VERSION is checked by bootstrapper, so make it first */ write_version_file(NUL ...
- PostgreSQL的 initdb 源代码分析之七
继续分析:由于我使用initdb的时候,没有指定 locale,所以会使用OS的缺省locale,这里是 en_US.UTF-8 printf(_("The files belonging ...
- PostgreSQL的initdb 源代码分析之五
接前面,继续分析: putenv("TZ=GMT") 设置了时区信息. find_other_exec(argv[0], "postgres", PG_BACK ...
随机推荐
- FFMPEG视音频编解码零基础学习方法
在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视音频编解码的人,有的已经是有多年经验的“大神”,有的是刚开始学习的初学者.在和大家探讨的过程中,我忽然发现了一个问题:在“ ...
- PHP最佳实践(译)
原文: PHP Best Practices-A short, practical guide for common and confusing PHP tasks 译者:youngsterxyf 最 ...
- 【二叉树、堆】15轻院校赛-J-堆
原题:http://acm.zzuli.edu.cn/problem.php?cid=1099&pid=9 [描述] [输入] [输出] Sample Input 3 1 10 3 10 5 ...
- PLSQL Developer报“动态执行表不可访问,本会话的自动统计被禁止”的解决方案
现象与提示: 第一次用PLSQL Developer连接数据库,若用sys用户登录并操作则正常,若用普通用户比如haishu登录并创建一个表则报错"动态执行表不可访问,本会话的自动统计被禁止 ...
- 【LeetCode 173】Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- BBED的安装
BBED是Block Browser EDitor的缩写,只有linux/unix版本,没有windows版本. 11g中默认是不带bbed的,如果要使用,可以在10g中拷贝过来,然后再进行编译使用. ...
- CentOS6.2编译安装codelite5.3
这两天实验室要求在服务器上安装一个codelite,于是由我对服务器下手,上网看了下悲剧的发现codelite不支持在centos上直接安装,只能编译安装,经过一番折腾之后,成功的装上了,下面是我的安 ...
- Java每日一则-002
Java中包的层级关系 java中的包在逻辑上是没有套嵌的,也就是说: java.lang 和 java.lang.awt 是两个平行的包,地位相等,互不相关.只不过一个名字叫java.lang另一个 ...
- C++ 我想这样用(五)
上一篇说了[C with Class]语法的第一部分,下面继续第二部分: 第二部分:面向过程的编程风格 什么是面向过程我想如果你还不知道,那你绝对不是C程序员!其实我个人感觉面向过程.模块式的C编程风 ...
- 【Hadoop代码笔记】Hadoop作业提交之TaskTracker 启动task
一.概要描述 在上篇博文描述了TaskTracker从Jobtracker如何从JobTracker获取到要执行的Task.在从JobTracker获取到LaunchTaskAction后,执行add ...