/* yes - output a string repeatedly until killed
Copyright (C) 1991-1997, 1999-2004 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/* David MacKenzie */
//包含4个标准库里的头文件
#include
#include
#include
#include
//下面包含1个当前文件夹下的system.h和lib文件夹下的两个头文件
#include "system.h"
#include "error.h"
#include "long-options.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "yes"// 定义宏PROGRAM_NAME
#define AUTHORS "David MacKenzie"//定义宏AUTHORS
/* The name this program was run with. */
char *program_name;//声明字符指针全局变量program_name,这个变量和宏PROGRAM_NAME的区别,可能和PROGRAM_NAME上面的注释有关系,不太了解
void
usage (int status)//又见usage()呵呵
{
if (status != EXIT_SUCCESS)//如果usage函数接受到的整型形参status不等于EXIT_SUCCESS
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);//则向标准错误输出这么一段字符串,提示用户输入--help
else//如果status等于EXIT_SUCCESS,则输出以下的字符串
{
printf (_("\
Usage: %s [STRING]...\n\
or: %s OPTION\n\
"),
program_name, program_name);//标准的printf用法,我在awk里也经常这样用
fputs (_("\
Repeatedly output a line with all specified STRING(s), or `y'.\n\
\n\
"), stdout);//这句话表明了该程序的作用:重复地输出指定的字符串,或者字符'y'
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
}
exit (status);//函数退出状态是形参status的值
}
int
main (int argc, char **argv)//main函数定义
{
initialize_main (&argc, &argv);//初始化命令行参数
program_name = argv[0];//给program_name赋值,其值为第一个命令行参数的值
setlocale (LC_ALL, "");//下面这3个都是和环境有关的标准做法,我们写程序的时候基本不用,当然了,可移植性就不高了
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);//还是用yodao词典的解释呵呵,注册出口函数
parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
usage, AUTHORS, (char const *) NULL);//解析命令行参数
if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
usage (EXIT_FAILURE);//如果getopt_long没有能够正常的解析完命令行参数,则调用usage函数
if (argc <= optind)//如果argc的值和getopt_long调用后得到的optind的值相比,要小于或等于的话
{
optind = argc; //将optind的值设置为argc的值
argv[argc++] = "y";//argc的值增1,同时将命令行参数的最后一个设置为字符串"y"
}
for (;;)//循环一直执行下去,直到在内部退出
{
int i;//声明局部变量i,i的作用域仅仅是在这个循环的大括号里面
for (i = optind; i < argc; i++)//i的变化范围是optind到argc,由上面的代码得知,这个循环至少执行一次
if (fputs (argv[i], stdout) == EOF//fputs函数返回EOF的情况是出错的情况
|| putchar (i == argc - 1 ? '\n' : ' ') == EOF//i如果和argc减1的值相等,则输出换行,否则输出空格)
{//上面的if条件看起来永远不会是真啊,也就是说这里面的代码,不会被执行,外层的循环就永远不会退出啦,待会做个测试试试。
error (0, errno, _("standard output"));
exit (EXIT_FAILURE);
}
}
}
看完代码,得到的结论是:yes会用一个死循环来输出它接受到的命令行参数,永远不会中断,man了一下yes,果然如此啊:
yes - output a string repeatedly until killed
测试了一下,果然如此。
阅读(473) | 评论(0) | 转发(0) |