home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fileutils-3.6 / src / rmdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  3.1 KB  |  141 lines

  1. /* rmdir -- remove directories
  2.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Options:
  19.    -p, --path        Remove any parent dirs that are explicitly mentioned
  20.             in an argument, if they become empty after the
  21.             argument file is removed.
  22.  
  23.    David MacKenzie <djm@ai.mit.edu>  */
  24.  
  25. #include <stdio.h>
  26. #include <getopt.h>
  27. #include <sys/types.h>
  28. #include "system.h"
  29. #include "version.h"
  30.  
  31. void error ();
  32. void strip_trailing_slashes ();
  33.  
  34. static void remove_parents ();
  35. static void usage ();
  36.  
  37. /* The name this program was run with. */
  38. char *program_name;
  39.  
  40. /* If nonzero, remove empty parent directories. */
  41. static int empty_paths;
  42.  
  43. /* If non-zero, display usage information and exit.  */
  44. static int flag_help;
  45.  
  46. /* If non-zero, print the version on standard error.  */
  47. static int flag_version;
  48.  
  49. static struct option const longopts[] =
  50. {
  51.   {"path", no_argument, &empty_paths, 1},
  52.   {"help", no_argument, &flag_help, 1},
  53.   {"version", no_argument, &flag_version, 1},
  54.   {NULL, 0, NULL, 0}
  55. };
  56.  
  57. void
  58. main (argc, argv)
  59.      int argc;
  60.      char **argv;
  61. {
  62.   int errors = 0;
  63.   int optc;
  64.  
  65.   program_name = argv[0];
  66.   empty_paths = 0;
  67.  
  68.   while ((optc = getopt_long (argc, argv, "p", longopts, (int *) 0)) != EOF)
  69.     {
  70.       switch (optc)
  71.     {
  72.     case 0:            /* Long option. */
  73.       break;
  74.     case 'p':
  75.       empty_paths = 1;
  76.       break;
  77.     default:
  78.       usage ();
  79.     }
  80.     }
  81.  
  82.   if (flag_version)
  83.     {
  84.       fprintf (stderr, "%s\n", version_string);
  85.       exit (0);
  86.     }
  87.  
  88.   if (flag_help)
  89.     usage ();
  90.  
  91.   if (optind == argc)
  92.     usage ();
  93.  
  94.   for (; optind < argc; ++optind)
  95.     {
  96.       /* Stripping slashes is harmless for rmdir;
  97.      if the arg is not a directory, it will fail with ENOTDIR.  */
  98.       strip_trailing_slashes (argv[optind]);
  99.       if (rmdir (argv[optind]) != 0)
  100.     {
  101.       error (0, errno, "%s", argv[optind]);
  102.       errors = 1;
  103.     }
  104.       else if (empty_paths)
  105.     remove_parents (argv[optind]);
  106.     }
  107.  
  108.   exit (errors);
  109. }
  110.  
  111. /* Remove any empty parent directories of `path'.
  112.    Replaces '/' characters in `path' with NULs. */
  113.  
  114. static void
  115. remove_parents (path)
  116.      char *path;
  117. {
  118.   char *slash;
  119.  
  120.   do
  121.     {
  122.       slash = rindex (path, '/');
  123.       if (slash == NULL)
  124.     break;
  125.       /* Remove any characters after the slash, skipping any extra
  126.      slashes in a row. */
  127.       while (slash > path && *slash == '/')
  128.     --slash;
  129.       slash[1] = 0;
  130.     }
  131.   while (rmdir (path) == 0);
  132. }
  133.  
  134. static void
  135. usage ()
  136. {
  137.   fprintf (stderr, "Usage: %s [-p] [--path] [--help] [--version] dir...\n",
  138.        program_name);
  139.   exit (1);
  140. }
  141.