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 /* The name this program was run with. */
39 /* If nonzero, remove empty parent directories. */
40 static int empty_paths
;
42 /* If nonzero, display usage information and exit. */
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},
57 /* Remove any empty parent directories of `path'.
58 Replaces '/' characters in `path' with NULs. */
61 remove_parents (char *path
)
67 slash
= strrchr (path
, '/');
70 /* Remove any characters after the slash, skipping any extra
72 while (slash
> path
&& *slash
== '/')
76 while (rmdir (path
) == 0);
83 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
87 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name
);
89 Remove the DIRECTORY(ies), if they are empty.\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"));
99 main (int argc
, char **argv
)
104 program_name
= argv
[0];
107 while ((optc
= getopt_long (argc
, argv
, "p", longopts
, (int *) 0)) != EOF
)
111 case 0: /* Long option. */
123 printf ("rmdir - %s\n", version_string
);
132 error (0, 0, _("too few arguments"));
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
]);
146 else if (empty_paths
)
147 remove_parents (argv
[optind
]);