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

2012年(12)

2011年(24)

2010年(24)

2009年(75)

2008年(42)

我的朋友

分类: C/C++

2009-11-01 21:08:44

   简陋的实现,呵呵:
#include

int main(void)
{
        printf("%s\n",ttyname(1));

}
[root@bjxdurs235 20091101]# ./a.out
/dev/pts/2

贴一下代码分析:
/* tty -- print the name of the terminal connected to standard input
在标准输出上打印连接到的终端名
   Copyright (C) 1990-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.  */

/* Displays "not a tty" if stdin is not a terminal.
   Displays nothing if -s option is given.
   Exit status 0 if stdin is a tty, 1 if not, 2 if usage error,
   3 if write error.

   Written by David MacKenzie .  */
//包含进来4个标准的头文件
#include
#include
#include
#include
//包含进来3个本地头文件
#include "system.h"
#include "error.h"
#include "quote.h"

/* Exit statuses.  */
//定义一个枚举类型,含有2个变量
enum
  {
    TTY_FAILURE = 2,
    TTY_WRITE_ERROR = 3
  };

/* The official name of this program (e.g., no `g' prefix).  */
#define PROGRAM_NAME "tty"//定义PROGRAM_NAME宏,其值为一个字符串

#define AUTHORS "David MacKenzie"//定义宏AUTHORS,其值是个字符串

/* The name under which this program was run. */
char *program_name;//声明字符指针变量

/* If true, return an exit status but produce no output. */
static bool silent;//声明静态布尔变量silent

static struct option const longopts[] =
//option这个结构体的原型定义我在long-options.c里面说过了,这里同样定义了这么一个结构体数组
{
  {"silent", no_argument, NULL, 's'},
  {"quiet", no_argument, NULL, 's'},
  {GETOPT_HELP_OPTION_DECL},
  {GETOPT_VERSION_OPTION_DECL},
  {NULL, 0, NULL, 0}
};

void
usage (int status)//帮助函数
{
  if (status != EXIT_SUCCESS)//如果形参status的值不等于EXIT_SUCCESS,则打印下面的一行帮助信息
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
         program_name);
  else//否则打印下面的帮助信息
    {
      printf (_("Usage: %s [OPTION]...\n"), program_name);
      fputs (_("\
Print the file name of the terminal connected to standard input.\n\
\n\
  -s, --silent, --quiet   print nothing, only return an exit status\n\
"), stdout);//说明了程序的作用,打印当前终端的名字,如果参数是 -s, --silent, --quiet ,则不打印东西,只返回退出状态
      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 *tty;//声明变量tty是一个指向char类型的指针
  int optc;//声明变量optc是一个整型变量

  initialize_main (&argc, &argv);//初始化命令行参数和选项
  program_name = argv[0];//将命令行参数的第一个赋值给字符指针变量program_name
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  initialize_exit_failure (TTY_WRITE_ERROR);
  //initialize_exit_failure是在system.h中定义的,代码段如下:
  /*
  initialize_exit_failure (int status)
  {
    if (status != EXIT_FAILURE)
      exit_failure = status;
  }
作用是判断接受的形参,如果形参不等于EXIT_FAILURE,则将exit_failure的值设为status的值,否则什么也不做
在这里,形参TTY_WRITE_ERROR是一个枚举类型的变量,其值为3,明显是不等于EXIT_FAILURE的,所以exit_failure就被设置了,但是这个变量在哪里声明的呢?
  */
  atexit (close_stdout);//登记出口函数

  silent = false;//silent的值是false,但是不是说c语言没有布尔类型的变量的吗?不理解啊

  while ((optc = getopt_long (argc, argv, "s", longopts, NULL)) != -1)
    {
    /*
这个while循环里只有一个switch语句,循环的条件是将getopt_long得到的值赋给optc,一次取一个参数,直到取参数取到最后一个,while循环退出
    */
      switch (optc)//取到optc的值后,根据optc的值,进行下面的分支选择
    {
    case 's'://如果值是's'
      silent = true;//将true的值赋给silent
      break;退出

    case_GETOPT_HELP_CHAR;
      /*
关于这句话的原型是在system.h里面定义的:
      enum
      {
        GETOPT_HELP_CHAR = (CHAR_MIN - 2),
        GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
      };
     
#define case_GETOPT_HELP_CHAR            \
        case GETOPT_HELP_CHAR:              \
          usage (EXIT_SUCCESS);           \
          break;
可见,这也是一个case分支语句         

      */

    case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
      /*
这也是在system.h里面定义的:
#define case_GETOPT_VERSION_CHAR(Program_name, Authors)            \
        case GETOPT_VERSION_CHAR:                      \
          version_etc (stdout, Program_name, GNU_PACKAGE, VERSION, Authors,   \
                       (char *) NULL);                      \
          exit (EXIT_SUCCESS);                          \
          break;
在这个case选择里面会exit退出程序         

      */

    default:
      usage (TTY_FAILURE);//如果以上的条件都没有触发的话,调用usage帮助函数
    }
    }

  if (optind < argc)//如果optind的值小于argc的值,则调用error函数
    error (0, 0, _("extra operand %s"), quote (argv[optind]));

  tty = ttyname (STDIN_FILENO);//tty的值是通过调用库函数ttyname得到的,ttyname是作用是返回一个字符指针,指向STDIN_FILENO文件描述符打开的终端名称
  if (!silent)//如果silent的值是0的话
    {
      if (tty)//如果tty不是空指针
    puts (tty);//则打印这个字符串
      else//否则打印另一个字符串
    puts (_("not a tty"));
    }

  exit (isatty (STDIN_FILENO) ? EXIT_SUCCESS : EXIT_FAIL);
  //isatty是个库函数,如果参数是一个连接到终端的打开了的描述符,则返回1,否则返回0,在这里,如果是1,则程序返回EXIT_SUCCESS,否则返回EXIT_FAIL
}

阅读(759) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~