由 391 至 404 , 通过duplicate 一个stdout的descriptor来判读stdout是否有效.
代码如下:
main的代码
//clien/mysql.cc
int main(int argc,char *argv[])
{
char buff[80];
MY_INIT(argv[0]); // 初始话化THR_KEY_mysys指向的线程环境,并初始化一些互斥量和锁.
DBUG_ENTER("main");
DBUG_PROCESS(argv[0]);
#pragma region SET_TERMINAL //设置交互终端的状态.
delimiter_str= delimiter;
default_prompt = my_strdup(getenv("MYSQL_PS1") ?
getenv("MYSQL_PS1") :
"mysql> ",MYF(MY_WME));
current_prompt = my_strdup(default_prompt,MYF(MY_WME));
prompt_counter=0;
outfile[0]=0; // no (default) outfile
strmov(pager, "stdout"); // the default, if --pager wasn't given
{
char *tmp=getenv("PAGER");
if (tmp && strlen(tmp))
{
default_pager_set= 1;
strmov(default_pager, tmp);
}
}
if (!isatty(0) || !isatty(1))
{
status.batch=1; opt_silent=1;
ignore_errors=0;
}
else
status.add_to_history=1;
status.exit_status=1;
#pragma endregion SET_TERMINAL
{ #pragma region PROMOTE_STDOUT //保证stdout有效,且可以访问.
/*
The file descriptor-layer may be out-of-sync with the file-number layer,
so we make sure that "stdout" is really open. If its file is closed then
explicitly close the FD layer.
*/
int stdout_fileno_copy;
stdout_fileno_copy= dup(fileno(stdout)); /* Okay if fileno fails. */
if (stdout_fileno_copy == -1)
fclose(stdout);
else
close(stdout_fileno_copy); /* Clean up dup(). */
}
#pragma endregion PROMOTE_STDOUT
...
}
没有评论:
发表评论