home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / a / bin / fileutil.12 / fileutil / fileutils-3.12 / src / rmdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-07  |  3.5 KB  |  157 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, --parent        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 <config.h>
  26. #include <stdio.h>
  27. #include <getopt.h>
  28. #include <sys/types.h>
  29. #include "system.h"
  30. #include "version.h"
  31.  
  32. void error ();
  33. void strip_trailing_slashes ();
  34.  
  35. static void remove_parents ();
  36. static void usage ();
  37.  
  38. /* The name this program was run with. */
  39. char *program_name;
  40.  
  41. /* If nonzero, remove empty parent directories. */
  42. static int empty_paths;
  43.  
  44. /* If non-zero, display usage information and exit.  */
  45. static int show_help;
  46.  
  47. /* If non-zero, print the version on standard output and exit.  */
  48. static int show_version;
  49.  
  50. static struct option const longopts[] =
  51. {
  52.   {"path", no_argument, &empty_paths, 1},
  53.   {"parents", no_argument, &empty_paths, 1},
  54.   {"help", no_argument, &show_help, 1},
  55.   {"version", no_argument, &show_version, 1},
  56.   {NULL, 0, NULL, 0}
  57. };
  58.  
  59. void
  60. main (argc, argv)
  61.      int argc;
  62.      char **argv;
  63. {
  64.   int errors = 0;
  65.   int optc;
  66.  
  67.   program_name = argv[0];
  68.   empty_paths = 0;
  69.  
  70.   while ((optc = getopt_long (argc, argv, "p", longopts, (int *) 0)) != EOF)
  71.     {
  72.       switch (optc)
  73.     {
  74.     case 0:            /* Long option. */
  75.       break;
  76.     case 'p':
  77.       empty_paths = 1;
  78.       break;
  79.     default:
  80.       usage (1);
  81.     }
  82.     }
  83.  
  84.   if (show_version)
  85.     {
  86.       printf ("%s\n", version_string);
  87.       exit (0);
  88.     }
  89.  
  90.   if (show_help)
  91.     usage (0);
  92.  
  93.   if (optind == argc)
  94.     {
  95.       error (0, 0, "too few arguments");
  96.       usage (1);
  97.     }
  98.  
  99.   for (; optind < argc; ++optind)
  100.     {
  101.       /* Stripping slashes is harmless for rmdir;
  102.      if the arg is not a directory, it will fail with ENOTDIR.  */
  103.       strip_trailing_slashes (argv[optind]);
  104.       if (rmdir (argv[optind]) != 0)
  105.     {
  106.       error (0, errno, "%s", argv[optind]);
  107.       errors = 1;
  108.     }
  109.       else if (empty_paths)
  110.     remove_parents (argv[optind]);
  111.     }
  112.  
  113.   exit (errors);
  114. }
  115.  
  116. /* Remove any empty parent directories of `path'.
  117.    Replaces '/' characters in `path' with NULs. */
  118.  
  119. static void
  120. remove_parents (path)
  121.      char *path;
  122. {
  123.   char *slash;
  124.  
  125.   do
  126.     {
  127.       slash = rindex (path, '/');
  128.       if (slash == NULL)
  129.     break;
  130.       /* Remove any characters after the slash, skipping any extra
  131.      slashes in a row. */
  132.       while (slash > path && *slash == '/')
  133.     --slash;
  134.       slash[1] = 0;
  135.     }
  136.   while (rmdir (path) == 0);
  137. }
  138.  
  139. static void
  140. usage (status)
  141.      int status;
  142. {
  143.   if (status != 0)
  144.     fprintf (stderr, "Try `%s --help' for more information.\n",
  145.          program_name);
  146.   else
  147.     {
  148.       printf ("Usage: %s [OPTION]... DIRECTORY...\n", program_name);
  149.       printf ("\
  150. \n\
  151.   -p, --parents   remove explicit parent directories if being emptied\n\
  152.       --help      display this help and exit\n\
  153.       --version   output version information and exit\n");
  154.     }
  155.   exit (status);
  156. }
  157.