(memcasecmp): Declare local I to be unsigned to avoid warning from gcc -Wall.
[coreutils.git] / src / rmdir.c
blob2902b27348b712ce085f01821d32ed3b3a1e84f7
1 /* rmdir -- remove directories
2 Copyright (C) 1990, 1991, 1995 Free Software Foundation, Inc.
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.
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.
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. */
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.
23 David MacKenzie <djm@ai.mit.edu> */
25 #include <config.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <sys/types.h>
30 #include "system.h"
31 #include "version.h"
32 #include "error.h"
34 void strip_trailing_slashes ();
36 /* The name this program was run with. */
37 char *program_name;
39 /* If nonzero, remove empty parent directories. */
40 static int empty_paths;
42 /* If nonzero, display usage information and exit. */
43 static int show_help;
45 /* If nonzero, print the version on standard output and exit. */
46 static int show_version;
48 static struct option const longopts[] =
50 {"path", no_argument, &empty_paths, 1},
51 {"parents", no_argument, &empty_paths, 1},
52 {"help", no_argument, &show_help, 1},
53 {"version", no_argument, &show_version, 1},
54 {NULL, 0, NULL, 0}
57 /* Remove any empty parent directories of `path'.
58 Replaces '/' characters in `path' with NULs. */
60 static void
61 remove_parents (char *path)
63 char *slash;
67 slash = strrchr (path, '/');
68 if (slash == NULL)
69 break;
70 /* Remove any characters after the slash, skipping any extra
71 slashes in a row. */
72 while (slash > path && *slash == '/')
73 --slash;
74 slash[1] = 0;
76 while (rmdir (path) == 0);
79 static void
80 usage (int status)
82 if (status != 0)
83 fprintf (stderr, _("Try `%s --help' for more information.\n"),
84 program_name);
85 else
87 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
88 printf (_("\
89 Remove the DIRECTORY(ies), if they are empty.\n\
90 \n\
91 -p, --parents remove explicit parent directories if being emptied\n\
92 --help display this help and exit\n\
93 --version output version information and exit\n"));
95 exit (status);
98 void
99 main (int argc, char **argv)
101 int errors = 0;
102 int optc;
104 program_name = argv[0];
105 empty_paths = 0;
107 while ((optc = getopt_long (argc, argv, "p", longopts, (int *) 0)) != EOF)
109 switch (optc)
111 case 0: /* Long option. */
112 break;
113 case 'p':
114 empty_paths = 1;
115 break;
116 default:
117 usage (1);
121 if (show_version)
123 printf ("rmdir - %s\n", version_string);
124 exit (0);
127 if (show_help)
128 usage (0);
130 if (optind == argc)
132 error (0, 0, _("too few arguments"));
133 usage (1);
136 for (; optind < argc; ++optind)
138 /* Stripping slashes is harmless for rmdir;
139 if the arg is not a directory, it will fail with ENOTDIR. */
140 strip_trailing_slashes (argv[optind]);
141 if (rmdir (argv[optind]) != 0)
143 error (0, errno, "%s", argv[optind]);
144 errors = 1;
146 else if (empty_paths)
147 remove_parents (argv[optind]);
150 exit (errors);