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