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

2012年(12)

2011年(24)

2010年(24)

2009年(75)

2008年(42)

我的朋友

分类: C/C++

2009-10-31 21:07:39

/* dirname -- strip suffix from file name//去除文件名的后缀

   Copyright (C) 1990-1997, 1999-2002, 2004, 2005, 2006 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.  */

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

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

#define AUTHORS "David MacKenzie", "Jim Meyering"//定义宏AUTHORS,这个用法第一次看到,因为两个字符串中间有空格,其实还是不建议这样使用的。

/* The name this program was run with. */
char *program_name; //声明全局变量,program_name是一个指向char类型的指针变量

void
usage (int status)//普通的usage函数定义,函数接受一个整型形参。
{
  if (status != EXIT_SUCCESS)//如果传入的形参status不等于EXIT_SUCCESS
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
         program_name);//则向标准错误打印一个字符串,提示用户使用--help来获取帮助信息
  else//否则,如果status等于EXIT_SUCCESS,则打印下面的语句块里的字符串
    {
      printf (_("\
Usage: %s NAME\n\
  or:  %s OPTION\n\
"),
          program_name, program_name);
      fputs (_("\
Print NAME with its trailing /component removed; if NAME contains no /'s,\n\
output `.' (meaning the current directory).\n\
\n\
"), stdout);//向标准输出,打印去除了最后的/的目录的名字,如果名字里面没有/则打印一个点,代表的意思是当前目录
      fputs (HELP_OPTION_DESCRIPTION, stdout);
      fputs (VERSION_OPTION_DESCRIPTION, stdout);
      printf (_("\
\n\
Examples:\n\
  %s /usr/bin/sort  Output \"/usr/bin\".\n\
  %s stdio.h        Output \".\".\n\
"),
          program_name, program_name);//这里还举了个使用的例子
      printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
    }
  exit (status);//usage函数的退出状态通常是传入的形参status的值
}

int
main (int argc, char **argv)//标准的main函数定义
{
  static char const dot = '.';//初始化一个静态的字符常量dot,其值为一个点
  char const *result;//声明一个字符指针常量result
  size_t len;//定义一个size_t类型的变量len

  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 (EXIT_FAILURE);//则调用usage函数打印帮助

  if (argc < optind + 1)//如果调用getopt_long后,argc的值小于optind+1的值,则调用error函数并打印帮助信息
    {
      error (0, 0, _("missing operand"));
      usage (EXIT_FAILURE);
    }

  if (optind + 1 < argc)//如果optind+1的值小于argc的值,也打印帮助信息
    {
      error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
      usage (EXIT_FAILURE);
    }

  result = argv[optind];//如果执行到这里,说明上面的代码都没有发生错误,则将命令行选项里的最后一个字符串赋值给result
  /*
这里有必要多说一下result的类型,其定义为char const *result,意思是,result是一个指向字符常量的指针,result这个指针的值可以改变,但是它指向的字符串不能改变。
  */
  len = dir_len (result);//dir_len是在Remove.c里面用define定义的,具体的实现是在lib文件夹下的dirname.c中实现的

  if (! len)//如果得到的len是0的话
    {
      result = ˙//将result指针指向dot
      len = 1;//将len的值设置为1
    }

  fwrite (result, 1, len, stdout);//根据上面得到的结果,调用fwrite库函数,这句话的意思是,向标准输出写result指针指向的字符串,一次写1个字符,总共写的长度为len,注意如果dir_len是0的话,输出是个'.'
  putchar ('\n');//再输出个回车

  exit (EXIT_SUCCESS);//程序最后返回EXIT_SUCCESS
}


ok,用一下:
[root@bjxdurs235 c]# dirname a.out
.
[root@bjxdurs235 c]# dirname /bin/sh
/bin
阅读(894) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~