Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32994
  • 博文数量: 14
  • 博客积分: 255
  • 博客等级: 二等列兵
  • 技术积分: 115
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-30 13:47
文章分类
文章存档

2011年(14)

我的朋友

分类: LINUX

2011-10-11 13:35:38

命令: basename
解释: 显示文件名
实现:
  1. /* basename -- strip directory and suffix from file names
  2.    Copyright (C) 1990-1997, 1999-2010 Free Software Foundation, Inc.

  3.    This program is free software: you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation, either version 3 of the License, or
  6.    (at your option) any later version.

  7.    This program is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10.    GNU General Public License for more details.

  11.    You should have received a copy of the GNU General Public License
  12.    along with this program. If not, see <http://www.gnu.org/licenses/>. */

  13. /* Usage: basename name [suffix]
  14.    NAME is a file name; SUFFIX is a suffix to strip from it.

  15.    basename /usr/foo/lossage/functions.l
  16.    => functions.l
  17.    basename /usr/foo/lossage/functions.l .l
  18.    => functions
  19.    basename functions.lisp p
  20.    => functions.lis */

  21. #include <config.h>
  22. #include <getopt.h>
  23. #include <stdio.h>
  24. #include <sys/types.h>

  25. #include "system.h"
  26. #include "long-options.h"
  27. #include "error.h"
  28. #include "quote.h"

  29. /* The official name of this program (e.g., no `g' prefix). */
  30. #define PROGRAM_NAME "basename"

  31. #define AUTHORS proper_name ("David MacKenzie")

  32. void
  33. usage (int status)
  34. {
  35.   if (status != EXIT_SUCCESS)
  36.     fprintf (stderr, _("Try `%s --help' for more information.\n"),
  37.              program_name);
  38.   else
  39.     {
  40.       printf (_("\
  41. Usage: %s NAME [SUFFIX]\n\
  42.   or: %s OPTION\n\
  43. "),
  44.               program_name, program_name);
  45.       fputs (_("\
  46. Print NAME with any leading directory components removed.\n\
  47. If specified, also remove a trailing SUFFIX.\n\
  48. \n\
  49. "), stdout);
  50.       fputs (HELP_OPTION_DESCRIPTION, stdout);
  51.       fputs (VERSION_OPTION_DESCRIPTION, stdout);
  52.       printf (_("\
  53. \n\
  54. Examples:\n\
  55.   %s /usr/bin/sort Output \"sort\".\n\
  56.   %s include/stdio.h .h Output \"stdio\".\n\
  57. "),
  58.               program_name, program_name);
  59.       emit_ancillary_info ();
  60.     }
  61.   exit (status);
  62. }

  63. /* Remove SUFFIX from the end of NAME if it is there, unless NAME
  64.    consists entirely of SUFFIX. */

  65. static void
  66. remove_suffix (char *name, const char *suffix)
  67. {
  68.   char *np;
  69.   const char *sp;

  70.   np = name + strlen (name);
  71.   sp = suffix + strlen (suffix);

  72.   while (np > name && sp > suffix)
  73.     if (*--np != *--sp)
  74.       return;
  75.   if (np > name)
  76.     *np = '\0';
  77. }

  78. int
  79. main (int argc, char **argv)
  80. {
  81.   char *name;

  82.   initialize_main (&argc, &argv);
  83.   set_program_name (argv[0]);
  84.   setlocale (LC_ALL, "");
  85.   bindtextdomain (PACKAGE, LOCALEDIR);
  86.   textdomain (PACKAGE);

  87.   atexit (close_stdout);

  88.   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
  89.                       usage, AUTHORS, (char const *) NULL);
  90.   if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
  91.     usage (EXIT_FAILURE);

  92.   if (argc < optind + 1)
  93.     {
  94.       error (0, 0, _("missing operand"));
  95.       usage (EXIT_FAILURE);
  96.     }

  97.   if (optind + 2 < argc)
  98.     {
  99.       error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
  100.       usage (EXIT_FAILURE);
  101.     }

  102.   name = base_name (argv[optind]);
  103.   strip_trailing_slashes (name);

  104.   /* Per POSIX, `basename // /' must return `//' on platforms with
  105.      distinct //. On platforms with drive letters, this generalizes
  106.      to making `basename c: :' return `c:'. This rule is captured by
  107.      skipping suffix stripping if base_name returned an absolute path
  108.      or a drive letter (only possible if name is a file-system
  109.      root). */
  110.   if (argc == optind + 2 && IS_RELATIVE_FILE_NAME (name)
  111.       && ! FILE_SYSTEM_PREFIX_LEN (name))
  112.     remove_suffix (name, argv[optind + 1]);

  113.   puts (name);
  114.   free (name);

  115.   exit (EXIT_SUCCESS);
  116. }
阅读(1846) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~