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

2012年(12)

2011年(24)

2010年(24)

2009年(75)

2008年(42)

我的朋友

分类: C/C++

2009-12-20 21:50:03

    刚刚看完了rmdir.c,当然这个程序调用了系统调用rmdir,这是我刚刚看的,其实coreutil的很多程序都调用了系统调用,将来看起来肯定更顺利了。
    具体这个程序来说,在删除上级目录的时候做了比较多的判断,我没有完全去跟踪,都是一些字符串的操作。
/* rmdir -- remove directories

   Copyright (C) 90, 91, 1995-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.  */

/* Options:
   -p, --parent        Remove any parent dirs that are explicitly mentioned
            in an argument, if they become empty after the
            argument file is removed.

   David MacKenzie   */
//包含头文件
#include
#include
#include
#include

#include "system.h"
#include "error.h"
#include "quotearg.h"

/* The official name of this program (e.g., no `g' prefix).  */
#define PROGRAM_NAME "rmdir"//定义宏

#define AUTHORS "David MacKenzie"//定义宏

/* The name this program was run with. */
char *program_name;//声明全局字符指针变量,只分配一个指针的空间而已

/* If true, remove empty parent directories.  */
static bool remove_empty_parents;//暂且不管它的作用,语法的意思是,声明一个全局布尔变量,其值保存在一个静态区,传说中的全局变量^_^

/* If true, don't treat failure to remove a nonempty directory
   as an error.  */
static bool ignore_fail_on_non_empty;//暂且不管它的作用,语法的意思是,声明一个全局布尔变量,其值保存在一个静态区,传说中的全局变量^_^

/* If true, output a diagnostic for every directory processed.  */
static bool verbose;//暂且不管它的作用,语法的意思是,声明一个全局布尔变量,其值保存在一个静态区,传说中的全局变量^_^

/* For long options that have no equivalent short option, use a
   non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
enum
{
  IGNORE_FAIL_ON_NON_EMPTY_OPTION = CHAR_MAX + 1
};//初始化一个枚举类型的变量

static struct option const longopts[] =
{
  /* Don't name this `--force' because it's not close enough in meaning
     to e.g. rm's -f option.  */
  {"ignore-fail-on-non-empty", no_argument, NULL,
   IGNORE_FAIL_ON_NON_EMPTY_OPTION},

  {"path", no_argument, NULL, 'p'},  /* Deprecated.  */
  {"parents", no_argument, NULL, 'p'},
  {"verbose", no_argument, NULL, 'v'},
  {GETOPT_HELP_OPTION_DECL},
  {GETOPT_VERSION_OPTION_DECL},
  {NULL, 0, NULL, 0}
};//初始化一个option类型的结构体数组,还是用来处理命令行选项

/* Return true if ERROR_NUMBER is one of the values associated
   with a failed rmdir due to non-empty target directory.  */

static bool
errno_rmdir_non_empty (int error_number)
{
  return (error_number == RMDIR_ERRNO_NOT_EMPTY);
}//这个函数让人惊叹其简洁^_^ ,作用是将形参和RMDIR_ERRNO_NOT_EMPTY进行比较,返回比较后的结果,返回值是个布尔类型值

/* Remove any empty parent directories of DIR.
   If DIR contains slash characters, at least one of them
   (beginning with the rightmost) is replaced with a NUL byte.
   Return true if successful.  */

static bool
remove_parents (char *dir)//函数定义部分,接受一个字符指针变量的形参
{
  char *slash;//声明一个字符指针类型的局部变量
  bool ok = true;//初始化局部布尔类型变量ok为真

  strip_trailing_slashes (dir);
  while (1)
    {
      slash = strrchr (dir, '/');//slash指向dir这个字符串中的最后一个/,如果dir中没有/,则退出while循环
      if (slash == NULL)
    break;
      /* Remove any characters after the slash, skipping any extra
     slashes in a row. */
      while (slash > dir && *slash == '/')
    --slash;
      slash[1] = 0;

      /* Give a diagnostic for each attempted removal if --verbose.  */
      if (verbose)
    error (0, 0, _("removing directory, %s"), dir);

      ok = (rmdir (dir) == 0);//调用rmdir删除后是否成功的状态保存在ok里

      if (!ok)//如果rmdir不成功,则判断是否忽略
    {
      /* Stop quietly if --ignore-fail-on-non-empty. */
      if (ignore_fail_on_non_empty
          && errno_rmdir_non_empty (errno))
        {
          ok = true;
        }
      else
        {
          error (0, errno, "%s", quotearg_colon (dir));
        }
      break;
    }
    }
  return ok;
}

void
usage (int status)//帮助函数
{
  if (status != EXIT_SUCCESS)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
         program_name);
  else
    {
      printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
      fputs (_("\
Remove the DIRECTORY(ies), if they are empty.\n\
\n\
      --ignore-fail-on-non-empty\n\
                  ignore each failure that is solely because a directory\n\
                  is non-empty\n\
"), stdout);
      fputs (_("\
  -p, --parents   Remove DIRECTORY and its ancestors.  E.g., `rmdir -p a/b/c' is\n\
                  similar to `rmdir a/b/c a/b a'.\n\
  -v, --verbose   output a diagnostic for every directory processed\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函数里的变量
  bool ok = true;
  int optc;

  initialize_main (&argc, &argv);
  program_name = argv[0];
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  atexit (close_stdout);

  remove_empty_parents = false;//初始化全局变量

  while ((optc = getopt_long (argc, argv, "pv", longopts, NULL)) != -1)
    {//解析命令行选项,根据选项,设置全局变量的值
      switch (optc)
    {
    case 'p':
      remove_empty_parents = true;
      break;
    case IGNORE_FAIL_ON_NON_EMPTY_OPTION:
      ignore_fail_on_non_empty = true;
      break;
    case 'v':
      verbose = true;
      break;
    case_GETOPT_HELP_CHAR;
    case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
    default:
      usage (EXIT_FAILURE);
    }
    }

  if (optind == argc)//此程序需要命令行参数
    {
      error (0, 0, _("missing operand"));
      usage (EXIT_FAILURE);
    }

  for (; optind < argc; ++optind)
    {//在这个for循环里,处理每一个命令行参数,也就是删除每一个列出来的目录
      char *dir = argv[optind];

      /* Give a diagnostic for each attempted removal if --verbose.  */
      if (verbose)
    error (0, 0, _("removing directory, %s"), dir);

      if (rmdir (dir) != 0)//如果调用rmdir删除dir不成功,则执行下面的代码,判断是否忽略非空的目录,采取措施
    {
      if (ignore_fail_on_non_empty
          && errno_rmdir_non_empty (errno))
        continue;//如果满足上面的条件,直接跳到下一个for循环,否则打印错误,并将ok的值设置为假

      error (0, errno, "%s", quotearg_colon (dir));
      ok = false;
    }
      else if (remove_empty_parents)//否则,如果成功删除了dir,并且remove_empty_parents的值为真,则执行:
    {
      ok &= remove_parents (dir);//先调用remove_parents删除dir,然后用ok的值和函数的返回值进行并
    }
    }

  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);//main函数退出
}

阅读(2263) | 评论(0) | 转发(0) |
0

上一篇:顺义的温泉

下一篇:ie对文件类型的错认

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