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

2012年(12)

2011年(24)

2010年(24)

2009年(75)

2008年(42)

我的朋友

分类: C/C++

2009-10-24 22:34:37

/* logname -- print user's login name
   Copyright (C) 1990-1997, 1999-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.  */

//包含进来4个标准库里的头文件
#include
#include
#include
#include

//包含4个自己包里的头文件,其中system.h位于当前文件夹,其余3个位于lib文件夹
#include "system.h"
#include "error.h"
#include "long-options.h"
#include "quote.h"

/* The official name of this program (e.g., no `g' prefix).  */
#define PROGRAM_NAME "logname" //程序名称为: logname

#define AUTHORS "FIXME: unknown" //竟然不知道作者是谁,牛啊,open source

/* The name this program was run with. */
char *program_name; //这里有个疑问,这个全局变量和宏PROGRAM_NAME有什么关系呢

void
usage (int status)//使用方法函数,这个在sync.c  True.c里面都有,估计这个软件包应该都是这样设计了一个usage()的标准函数
{
  if (status != EXIT_SUCCESS)//如果函数接受到的形参status,其值和宏EXIT_SUCCESS不相等,则
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
         program_name);//向标准错误打印一串信息
  else//如果整型形参status等于宏EXIT_SUCCESS,则打印下面这个代码段的字符串信息
    {
      printf (_("Usage: %s [OPTION]\n"), program_name);
      fputs (_("\
Print the name of the current user.\n\
\n\
"), stdout);
      fputs (HELP_OPTION_DESCRIPTION, stdout);
      fputs (VERSION_OPTION_DESCRIPTION, stdout);
      printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
    }
  exit (status);//函数的退出状态是接受的形参值,该值在整个函数调用期间一直未被更改
}

int
main (int argc, char **argv)//main主体函数
{
  char *cp;//定义字符指针变量cp,作用域只在main函数里

  initialize_main (&argc, &argv);//初始化命令行参数,在我的linux平台上,这句话相当于空
  program_name = argv[0];//argv[0]是char *类型的变量,将其值赋给全局变量program_name
  setlocale (LC_ALL, "");//初始化本地环境
  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)
      /*
man了一下,返回-1的情况是: -1 for the end of the option list
      */
    usage (EXIT_FAILURE);//如果getopt_long没有正常到达应该到达最后一个的情况,就调用usage函数,但是怎么模拟这样的情况,不太清楚

  if (optind < argc)
      /*
先看看man手册里对optind的解释:
    Then optind is the index in argv of the  first    argv-element
           that is not an option.
意思是命令行参数里第一个不是选项的索引(应该是个数目),如果这个值小于命令行参数的个数,则

      */
    {//
      error (0, 0, _("extra operand %s"), quote (argv[optind]));//调用error函数
      usage (EXIT_FAILURE);//调用usage函数,传递的形参是宏EXIT_FAILURE
    }

  /* POSIX requires using getlogin (or equivalent code).  */
  cp = getlogin ();
  /*
首先man一下这个getlogin,这应该是个系统调用:
  getlogin    returns  a pointer to a string containing the name of the user logged in on the controlling terminal of the pro-
         cess, or a null pointer if this information cannot be determined.    The string is statically allocated and might  be  over-
         written on subsequent calls to this function or to cuserid.
翻译一下,getlogin返回一个字符串指针,这个字符串包含的内容是当前进程的控制终端上登录的用户名        
如果得不到这个值,则返回null,空指针。这个字符串是静态分配内存(也就是放在堆上的静态变量),所以在下次调用或者发生cuserid调用的时候会被覆盖
简单的翻了一下apue的目录,没有发现对getlogin系统调用的说明,ok,就理解这个man就ok了  */
  if (cp)//如果cp不是空指针,就是说,getlogin系统调用可以获取到这个字符串
    {
      puts (cp);//向标准输出打印该字符串
      /*
没事看看puts的man吧:
      int puts(const char *s);函数声明,接受一个字符指针形参
      puts() writes the string s and a trailing newline to stdout.
      其作用是向标准输出写这个字符串,并打印一个换行

      */
      exit (EXIT_SUCCESS);//main函数顺利退出
    }
  /* POSIX prohibits using a fallback technique.  */

  error (0, 0, _("no login name"));//如果代码段执行到这里,就说明有错误发生了
  exit (EXIT_FAILURE);//这时候退出状态应该是失败状态
}
------------------------------
使用一下看看效果啦:
[root@bjxdurs235 ~]# logname --help
Usage: logname [OPTION]
Print the name of the current user.

      --help     display this help and exit
      --version  output version information and exit

Report bugs to .
[root@bjxdurs235 ~]# logname -h
logname: invalid option -- h
Try `logname --help' for more information.
[root@bjxdurs235 ~]# logname -abc
logname: invalid option -- a
Try `logname --help' for more information.
[root@bjxdurs235 ~]# logname
liufeng

    在看这个程序的时候,主要是看了几个man,如getopt_long  getlogin,还有puts,呵呵,多看man,相当有好处啊,我现在已经逐渐习惯man手册了,确实很方便,而且讲解权威有透彻,在某种程度上,比google还要好啊
阅读(807) | 评论(0) | 转发(0) |
0

上一篇:True.c/有疑问啊

下一篇:yes.c/死循环

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