home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / fileutil.lha / fileutils-3.3 / src / rmdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-02  |  2.5 KB  |  120 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.  
  30. void remove_parents ();
  31. void error ();
  32. void strip_trailing_slashes ();
  33. void usage ();
  34.  
  35. /* If nonzero, remove empty parent directories. */
  36. int empty_paths;
  37.  
  38. /* The name this program was run with. */
  39. char *program_name;
  40.  
  41. struct option longopts[] =
  42. {
  43.   {"path", 0, &empty_paths, 1},
  44.   {NULL, 0, NULL, 0}
  45. };
  46.  
  47. void
  48. main (argc, argv)
  49.      int argc;
  50.      char **argv;
  51. {
  52.   int errors = 0;
  53.   int optc;
  54.  
  55.   program_name = argv[0];
  56.   empty_paths = 0;
  57.  
  58.   while ((optc = getopt_long (argc, argv, "p", longopts, (int *) 0)) != EOF)
  59.     {
  60.       switch (optc)
  61.     {
  62.     case 0:            /* Long option. */
  63.       break;
  64.     case 'p':
  65.       empty_paths = 1;
  66.       break;
  67.     default:
  68.       usage ();
  69.     }
  70.     }
  71.  
  72.   if (optind == argc)
  73.     usage ();
  74.   
  75.   for (; optind < argc; ++optind)
  76.     {
  77.       strip_trailing_slashes (argv[optind]);
  78.       if (rmdir (argv[optind]) != 0)
  79.     {
  80.       error (0, errno, "%s", argv[optind]);
  81.       errors = 1;
  82.     }
  83.       else if (empty_paths)
  84.     remove_parents (argv[optind]);
  85.     }
  86.  
  87.   exit (errors);
  88. }
  89.  
  90. /* Remove any empty parent directories of `path'.
  91.    Replaces '/' characters in `path' with NULs. */
  92.  
  93. void
  94. remove_parents (path)
  95.      char *path;
  96. {
  97.   char *slash;
  98.  
  99.   do
  100.     {
  101.       slash = rindex (path, '/');
  102.       if (slash == NULL)
  103.     break;
  104.       /* Remove any characters after the slash, skipping any extra
  105.      slashes in a row. */
  106.       while (slash > path && *slash == '/')
  107.     --slash;
  108.       slash[1] = 0;
  109.     }
  110.   while (rmdir (path) == 0);
  111. }
  112.  
  113. void
  114. usage ()
  115. {
  116.   fprintf (stderr, "Usage: %s [-p] [--path] dir...\n",
  117.        program_name);
  118.   exit (1);
  119. }
  120.