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

2012年(12)

2011年(24)

2010年(24)

2009年(75)

2008年(42)

我的朋友

分类: C/C++

2009-11-12 00:07:44

/* hostname - set or print the name of current host system
 设置或者打印当前主机系统的名字  */
/* Written by Jim Meyering.  */
//包含进来4个标准的头文件
#include
#include
#include
#include
//包含进来5个本地的头文件
#include "system.h"
#include "long-options.h"
#include "error.h"
#include "quote.h"
#include "xgethostname.h"
/* The official name of this program (e.g., no `g' prefix).  */
#define PROGRAM_NAME "hostname"//定义宏PROGRAM_NAME,其值是一个字符串
#define AUTHORS "Jim Meyering"//定义宏AUTHORS,其值是一个字符串
//条件编译语句,如果有宏HAVE_SETHOSTNAME的定义,并且没有定义宏sethostname
#if HAVE_SETHOSTNAME && !defined sethostname
//则声明返回类型是整型的函数sethostname
int sethostname ();
#endif//条件编译结束
//也是条件编译,如果没有定义宏HAVE_SETHOSTNAME,并且定义了宏HAVE_SYSINFO,并且定义了宏HAVE_SYS_SYSTEMINFO_H
#if !defined HAVE_SETHOSTNAME && defined HAVE_SYSINFO && \
     defined HAVE_SYS_SYSTEMINFO_H
//则包含这个头文件    
# include
//函数sethostname的实现
int
sethostname (char *name, size_t namelen)//函数接受两个形参,一个字符指针,一个size_t类型的变量
{
  /* Using sysinfo() is the SVR4 mechanism to set a hostname. */
  //调用系统调用sysinfo,来设置主机名
  return (sysinfo (SI_SET_HOSTNAME, name, namelen) < 0 ? -1 : 0);
}
//定义宏HAVE_SETHOSTNAME,其值为1,在这个条件编译里面,将HAVE_SETHOSTNAME设置了。
# define HAVE_SETHOSTNAME 1  /* Now we have it... */
#endif//条件编译结束
/* The name this program was run with. */
char *program_name; //声明全局字符指针变量program_name
void
usage (int status)//帮助函数,和以前一样,接受的是一个整型形参
{
  if (status != EXIT_SUCCESS)//如果status不等于EXIT_SUCCESS,则向标准错误输出一个字符串,提示用户使用--help
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
      program_name);
  else//否则,如果status等于EXIT_SUCCESS,则打印下面的东东
    {
      printf (_("\
Usage: %s [NAME]\n\
  or:  %s OPTION\n\
Print or set the hostname of the current system.\n\
\n\
"),
             program_name, program_name);
      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函数
{
  char *hostname;//定义字符指针变量hostname
  initialize_main (&argc, &argv);//初始化命令行参数
  program_name = argv[0];//将命令行参数的第一个值赋给变量program_name
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);
  atexit (close_stdout);//登记出口函数
  parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
        usage, AUTHORS, (char const *) NULL);//解析命令行长选项
  if (getopt_long (argc, argv, "", NULL, NULL) != -1)//调用getopt_long处理命令行参数,如果调用失败,则调用usage帮助函数
    usage (EXIT_FAILURE);
  if (argc == optind + 1)//如果argc的值等于optind+1的值
    {//则执行这个语句块里的代码
#ifdef HAVE_SETHOSTNAME//如果定义了宏HAVE_SETHOSTNAME
      /* Set hostname to operand.  */
      char const *name = argv[optind];//初始化指针常量name,其值是argv[optind]
      if (sethostname (name, strlen (name)) != 0)//调用sethostname()函数,间接地调用sysinfo系统调用设置主机名,如果sethostname调用失败,则调用error函数
 error (EXIT_FAILURE, errno, _("cannot set name to %s"), quote (name));
#else//否则,如果没有定义HAVE_SETHOSTNAME这个宏,则直接调用error函数
      error (EXIT_FAILURE, 0,
      _("cannot set hostname; this system lacks the functionality"));
#endif//根据宏是否有定义来选择编译的条件编译结束
    }
  if (argc <= optind)//如果argc的值小于或者等于optind则执行下面的语句块
    {
      hostname = xgethostname ();//hostnama的值是通过xgethostname.h里面定义的xgethostname函数得到的字符串,这个xgethostname.h xgethostname.c都是在本地的lib文件夹下面
      if (hostname == NULL)//如果得到的hostname是个空指针,则调用error函数,并打印出来这个hostname这个字符串
 error (EXIT_FAILURE, errno, _("cannot determine hostname"));
      printf ("%s\n", hostname);
    }
  if (optind + 1 < argc)//如果optind+1的值小于argc的值,则调用error函数并调用usage,打印帮助
    {
      error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
      usage (EXIT_FAILURE);
    }
  exit (EXIT_SUCCESS);//执行到这里的时候,退出状态是EXIT_SUCCESS
}
阅读(1421) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~