免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1031 | 回复: 0
打印 上一主题 下一主题

tty.c/显示终端输入文件名 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-02 22:21 |只看该作者 |倒序浏览
linux tty(teletypewriter) 命令详解
功能说明:显示终端机连接标准输入设备的文件名称。
语  法:tty [-s][--help][--version]
补充说明:在Linux操作系统中,所有外围设备都有其名称与代号,这些名称代号以特殊文件的类型存放于/dev目录下。你可以执行tty指令查询目前使用的终端机的文件名称。
参  数:
-s或--silent或--quiet 不显示任何信息,只回传状态代码。
--help 在线帮助。
--version 显示版本信息。

/* 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 .  */   //3种状态
#include
#include
#include
#include
#include "system.h"
#include "error.h"
#include "quote.h"
/* Exit statuses.  */
enum
  {
    TTY_FAILURE = 2,
    TTY_WRITE_ERROR = 3
  };        //退出的状态
/* The official name of this program (e.g., no `g' prefix).  */
#define PROGRAM_NAME "tty"
#define AUTHORS "David MacKenzie"
/* The name under which this program was run. */
char *program_name;      //所跑的程序名
/* If true, return an exit status but produce no output. */
static bool silent;       //gcc 也支持bool类型?
static struct option const longopts[] =
{
  {"silent", no_argument, NULL, 's'},
  {"quiet", no_argument, NULL, 's'},
  {GETOPT_HELP_OPTION_DECL},
  {GETOPT_VERSION_OPTION_DECL},
  {NULL, 0, NULL, 0}
};      //注意这种结构,以后会常常遇到
/*slient,quiet是指不显示任何信息,后面的两项是指获取帮助和版本信息*/
void
usage (int status)     /*对程序所带的参数的一个判断,以后的程序有类似的*/
{
  if (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);
      fputs (HELP_OPTION_DESCRIPTION, stdout);
      fputs (VERSION_OPTION_DESCRIPTION, stdout);
      printf (_("\nReport bugs to .\n"), PACKAGE_BUGREPORT);
    }
  exit (status);
}
int
main (int argc, char **argv)
{
  char *tty;
  int optc;
  initialize_main (&argc, &argv);/*initialize_main?*/
  program_name = argv[0];
  setlocale (LC_ALL, "");
/*setlocale  配置地域化信息详细请见
http://www.cycoo.net/study/php/function.php-setlocale.htm
    */
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);
  initialize_exit_failure (TTY_WRITE_ERROR);
  atexit (close_stdout);
  silent = false;
  while ((optc = getopt_long (argc, argv, "s", longopts, NULL)) != -1)
    {/*getopt_long会分析一个选项,然后返回一个短项  这里的想法是分析argv中的选项*/
      switch (optc)
{
case 's':
   silent = true;
   break;
case_GETOPT_HELP_CHAR;    //返回帮助信息
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);  //返回版本信息
default:
   usage (TTY_FAILURE);    //失败
}
    }
  if (optind   tty = ttyname (STDIN_FILENO);   /*由于ttyname内容太多,这里没有进入其中去看*/
  if (!silent)
    {
      if (tty)
puts (tty);
      else
puts (_("not a tty"));
    }
  exit (isatty (STDIN_FILENO) ? EXIT_SUCCESS : EXIT_FAIL);
}
想法:
1、以后每天要坚持看一C
2、掌握一些基本的输入输出格式,如puts(_(""))等
3、学会对getopt_long的使用,即对struct option结构要熟悉
4、对于每一个方面都要考虑到,如帮助信息,版本信息,失败信息,成功状态等


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/107222/showart_2109813.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP