home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / futi14as.zip / RMDIR.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  5KB  |  200 lines

  1. /* rmdir -- remove directories
  2.    Copyright (C) 1990 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 1, 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. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  19.    This port is also distributed under the terms of the
  20.    GNU General Public License as published by the
  21.    Free Software Foundation.
  22.  
  23.    Please note that this file is not identical to the
  24.    original GNU release, you should have received this
  25.    code as patch to the official release.  */
  26.  
  27. #ifdef MSDOS
  28. static char RCS_Id[] =
  29.   "$Header: e:/gnu/fileutil/RCS/rmdir.c 1.4.0.2 90/09/19 12:09:14 tho Exp $";
  30.  
  31. static char Program_Id[] = "rmdir";
  32. static char RCS_Revision[] = "$Revision: 1.4.0.2 $";
  33.  
  34. #define VERSION \
  35.   "GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
  36.   (sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__
  37.  
  38. #define COPYING \
  39.   "This is free software, distributed under the terms of the\n" \
  40.   "GNU General Public License.  For details, see the file COPYING.\n"
  41. #endif /* MSDOS */
  42.  
  43. /* Usage: rmdir [-p] [+path] dir...
  44.  
  45.    Options:
  46.    -p, +path        Remove any parent dirs that are explicitly mentioned
  47.             in an argument, if they become empty after the
  48.             argument file is removed.
  49.  
  50.    David MacKenzie <djm@ai.mit.edu>  */
  51.  
  52. #include <stdio.h>
  53. #include <getopt.h>
  54. #include <sys/types.h>
  55. #include "system.h"
  56.  
  57. #ifdef STDC_HEADERS
  58. #include <errno.h>
  59. #include <stdlib.h>
  60. #else
  61. extern int errno;
  62. #endif
  63.  
  64. #ifdef MSDOS
  65.  
  66. #include <direct.h>
  67. #include <gnulib.h>
  68.  
  69. extern void main (int argc, char * *argv);
  70. static void remove_parents (char *path);
  71. static void strip_trailing_slashes (char *path);
  72. static void usage (void);
  73.  
  74. #else /* not MSDOS */
  75.  
  76. void remove_parents ();
  77. void error ();
  78. void strip_trailing_slashes ();
  79. void usage ();
  80.  
  81. #endif /* not MSDOS */
  82.  
  83. /* If nonzero, remove empty parent directories. */
  84. int empty_paths;
  85.  
  86. /* The name this program was run with. */
  87. char *program_name;
  88.  
  89. struct option longopts[] =
  90. {
  91. #ifdef MSDOS
  92.   {"copying", 0, NULL, 30},
  93.   {"version", 0, NULL, 31},
  94. #endif
  95.   {"path", 0, &empty_paths, 1},
  96.   {NULL, 0, NULL, 0}
  97. };
  98.  
  99. void
  100. main (argc, argv)
  101.      int argc;
  102.      char **argv;
  103. {
  104.   int errors = 0;
  105.   int optc;
  106.   int ind;
  107.  
  108.   program_name = argv[0];
  109.   empty_paths = 0;
  110.  
  111.   while ((optc = getopt_long (argc, argv, "p", longopts, &ind)) != EOF)
  112.     {
  113.       switch (optc)
  114.     {
  115.     case 0:            /* Long option. */
  116.       break;
  117.     case 'p':
  118.       empty_paths = 1;
  119.       break;
  120. #ifdef MSDOS
  121.     case 30:
  122.       fprintf (stderr, COPYING);
  123.       exit (0);
  124.       break;
  125.     case 31:
  126.       fprintf (stderr, VERSION);
  127.       exit (0);
  128.       break;
  129. #endif
  130.     default:
  131.       usage ();
  132.     }
  133.     }
  134.  
  135.   if (optind == argc)
  136.     usage ();
  137.   
  138.   for (; optind < argc; ++optind)
  139.     {
  140.       strip_trailing_slashes (argv[optind]);
  141.       if (rmdir (argv[optind]) != 0)
  142.     {
  143.       error (0, errno, "%s", argv[optind]);
  144.       errors = 1;
  145.     }
  146.       else if (empty_paths)
  147.     remove_parents (argv[optind]);
  148.     }
  149.  
  150.   exit (errors);
  151. }
  152.  
  153. /* Remove any empty parent directories of `path'.
  154.    Replaces '/' characters in `path' with NULs. */
  155.  
  156. void
  157. remove_parents (path)
  158.      char *path;
  159. {
  160.   char *slash;
  161.  
  162.   do
  163.     {
  164.       slash = rindex (path, '/');
  165.       if (slash == NULL)
  166.     break;
  167.       /* Remove any characters after the slash, skipping any extra
  168.      slashes in a row. */
  169.       while (slash > path && *slash == '/')
  170.     --slash;
  171.       slash[1] = 0;
  172.     }
  173.   while (rmdir (path) == 0);
  174. }
  175.  
  176. /* Remove trailing slashes from PATH; they cause some system calls to fail. */
  177.  
  178. void
  179. strip_trailing_slashes (path)
  180.      char *path;
  181. {
  182.   int last;
  183.  
  184.   last = strlen (path) - 1;
  185.   while (last > 0 && path[last] == '/')
  186.     path[last--] = '\0';
  187. }
  188.  
  189. void
  190. usage ()
  191. {
  192. #ifdef MSDOS
  193.   fprintf (stderr, "Usage: %s [-p] [+path] [+copying] [+version] dir...\n",
  194. #else /* not MSDOS */
  195.   fprintf (stderr, "Usage: %s [-p] [+path] dir...\n",
  196. #endif /* not MSDOS */
  197.        program_name);
  198.   exit (1);
  199. }
  200.