Chinaunix首页 | 论坛 | 博客
  • 博客访问: 916290
  • 博文数量: 177
  • 博客积分: 8613
  • 博客等级: 中将
  • 技术积分: 2835
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-12 04:16
文章分类
文章存档

2012年(12)

2011年(24)

2010年(24)

2009年(75)

2008年(42)

我的朋友

分类: C/C++

2009-10-23 22:00:05

/* Exit with a status code indicating success.
   Copyright (C) 1999-2003, 2005 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.  */

//包含一些必要的头文件
#include
#include
#include
#include "system.h"

/* Act like "true" by default; false.c overrides this.  */
#ifndef EXIT_STATUS //条件编译,如果没有宏定义EXIT_STATUS,就给它来个宏定义,其值为EXIT_SUCCESS
# define EXIT_STATUS EXIT_SUCCESS
#endif//条件编译结束

#if EXIT_STATUS == EXIT_SUCCESS//也是个条件编译,如果宏EXIT_STATUS等于EXIT_SUCCESS
# define PROGRAM_NAME "true"//则将宏PROGRAM_NAME定义为true
#else
# define PROGRAM_NAME "false"//否则定义为false
#endif//条件编译结束

#define AUTHORS "Jim Meyering"//标准做法,看man的时候有个程序作者,估计也是从这种标准做法里得到的值

/* The name this program was run with. */
char *program_name;//定义全局字符指针变量program_name

void
usage (int status) //定义函数usage,接受一个整型形参
{
  printf (_("\
Usage: %s [ignored command line arguments]\n\
  or:  %s OPTION\n\
"),
      program_name, program_name);//标准输出使用帮助
  printf ("%s\n\n",
      _(EXIT_STATUS == EXIT_SUCCESS
        ? "Exit with a status code indicating success."
        : "Exit with a status code indicating failure."));//根据EXIT_STATUS的值,选择打印哪个串
/*
下面的这三个宏,都和sync.c一样,引用了System.h里面定义的宏
*/
  fputs (HELP_OPTION_DESCRIPTION, stdout);
  fputs (VERSION_OPTION_DESCRIPTION, stdout);
  printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
  printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
  exit (status); //退出值为传入的形参值status
}

int
main (int argc, char **argv)//标准的main函数声明
{
  initialize_main (&argc, &argv);//和sync.c类似,平台相关,这里没有实际用处
  program_name = argv[0];//给全局变量program_name赋初始值,为命令行参数的第一个字符串
  setlocale (LC_ALL, "");//初始化本地环境
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);//这两行也相当于没有

  atexit (close_stdout);//注册退出时调用的函数

  /* Recognize --help or --version only if it's the only command-line
     argument.  */
  if (argc == 2)//如果命令行参数的个数是2的话,执行下面大括号里的语句块
    {
      if (STREQ (argv[1], "--help"))
          /*
这个STREQ比较有意思,先看它的原始宏定义:
#define STREQ(a, b) (strcmp (a, b) == 0)
代入后,得到语句的意思是:如果命令行参数的第二个字符串和"--help"相等,则返回0,在相等的
时候,才调用下面的语句,也就是usage函数

          */
    usage (EXIT_STATUS);

      if (STREQ (argv[1], "--version"))//如果命令行参数里的第二个参数和字符串"--version"相等
    version_etc (stdout, PROGRAM_NAME, GNU_PACKAGE, VERSION, AUTHORS,
             (char *) NULL);//则调用version_etc()函数,打印版本信息
    }

  exit (EXIT_STATUS);//函数的返回值为EXIT_STATUS
}


    看完源码,还有false.c在那个源文件里只是:
#define EXIT_STATUS EXIT_FAILURE
#include "true.c"
//定义宏EXIT_STATUS,其值为 : EXIT_FAILURE
//包含进一个c源文件: true.c

    man了一下true和false,都是do nothing ,设置个退出值,但是我在执行:
[root@bjxdurs235 20091023]# true --help
[root@bjxdurs235 20091023]# true --version
[root@bjxdurs235 20091023]# false --help
[root@bjxdurs235 20091023]# fasle --version
-bash: fasle: command not found
[root@bjxdurs235 20091023]# false --version
[root@bjxdurs235 20091023]#
这些命令的返回都是空,这个和源码是不符合的啊。
阅读(452) | 评论(0) | 转发(0) |
0

上一篇:sync.c

下一篇:logname.c/获取当前登录用户名

给主人留下些什么吧!~~