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)
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. */
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> */
28 #include <sys/types.h>
34 void strip_trailing_slashes ();
36 static void remove_parents ();
39 /* The name this program was run with. */
42 /* If nonzero, remove empty parent directories. */
43 static int empty_paths;
45 /* If non-zero, display usage information and exit. */
48 /* If non-zero, print the version on standard output and exit. */
49 static int show_version;
51 static struct option const longopts[] =
53 {"path", no_argument, &empty_paths, 1},
54 {"parents", no_argument, &empty_paths, 1},
55 {"help", no_argument, &show_help, 1},
56 {"version", no_argument, &show_version, 1},
68 program_name = argv[0];
71 while ((optc = getopt_long (argc, argv, "p", longopts, (int *) 0)) != EOF)
75 case 0: /* Long option. */
87 printf ("%s\n", version_string);
96 error (0, 0, "too few arguments");
100 for (; optind < argc; ++optind)
102 /* Stripping slashes is harmless for rmdir;
103 if the arg is not a directory, it will fail with ENOTDIR. */
104 strip_trailing_slashes (argv[optind]);
105 if (rmdir (argv[optind]) != 0)
107 error (0, errno, "%s", argv[optind]);
110 else if (empty_paths)
111 remove_parents (argv[optind]);
117 /* Remove any empty parent directories of `path'.
118 Replaces '/' characters in `path' with NULs. */
121 remove_parents (path)
128 slash = strrchr (path, '/');
131 /* Remove any characters after the slash, skipping any extra
133 while (slash > path && *slash == '/')
137 while (rmdir (path) == 0);
145 fprintf (stderr, "Try `%s --help' for more information.\n",
149 printf ("Usage: %s [OPTION]... DIRECTORY...\n", program_name);
152 -p, --parents remove explicit parent directories if being emptied\n\
153 --help display this help and exit\n\
154 --version output version information and exit\n");