home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / gnu / fileutils-3.12-src.lha / fileutils-3.12 / src / rmdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-12  |  3.5 KB  |  156 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. main (argc, argv)
  60.      int argc;
  61.      char **argv;
  62. {
  63.   int errors = 0;
  64.   int optc;
  65.  
  66.   program_name = argv[0];
  67.   empty_paths = 0;
  68.  
  69.   while ((optc = getopt_long (argc, argv, "p", longopts, (int *) 0)) != EOF)
  70.     {
  71.       switch (optc)
  72.     {
  73.     case 0:            /* Long option. */
  74.       break;
  75.     case 'p':
  76.       empty_paths = 1;
  77.       break;
  78.     default:
  79.       usage (1);
  80.     }
  81.     }
  82.  
  83.   if (show_version)
  84.     {
  85.       printf ("%s\n", version_string);
  86.       exit (0);
  87.     }
  88.  
  89.   if (show_help)
  90.     usage (0);
  91.  
  92.   if (optind == argc)
  93.     {
  94.       error (0, 0, "too few arguments");
  95.       usage (1);
  96.     }
  97.  
  98.   for (; optind < argc; ++optind)
  99.     {
  100.       /* Stripping slashes is harmless for rmdir;
  101.      if the arg is not a directory, it will fail with ENOTDIR.  */
  102.       strip_trailing_slashes (argv[optind]);
  103.       if (rmdir (argv[optind]) != 0)
  104.     {
  105.       error (0, errno, "%s", argv[optind]);
  106.       errors = 1;
  107.     }
  108.       else if (empty_paths)
  109.     remove_parents (argv[optind]);
  110.     }
  111.  
  112.   exit (errors);
  113. }
  114.  
  115. /* Remove any empty parent directories of `path'.
  116.    Replaces '/' characters in `path' with NULs. */
  117.  
  118. static void
  119. remove_parents (path)
  120.      char *path;
  121. {
  122.   char *slash;
  123.  
  124.   do
  125.     {
  126.       slash = rindex (path, '/');
  127.       if (slash == NULL)
  128.     break;
  129.       /* Remove any characters after the slash, skipping any extra
  130.      slashes in a row. */
  131.       while (slash > path && *slash == '/')
  132.     --slash;
  133.       slash[1] = 0;
  134.     }
  135.   while (rmdir (path) == 0);
  136. }
  137.  
  138. static void
  139. usage (status)
  140.      int status;
  141. {
  142.   if (status != 0)
  143.     fprintf (stderr, "Try `%s --help' for more information.\n",
  144.          program_name);
  145.   else
  146.     {
  147.       printf ("Usage: %s [OPTION]... DIRECTORY...\n", program_name);
  148.       printf ("\
  149. \n\
  150.   -p, --parents   remove explicit parent directories if being emptied\n\
  151.       --help      display this help and exit\n\
  152.       --version   output version information and exit\n");
  153.     }
  154.   exit (status);
  155. }
  156.