1 /* rmdir -- remove directories
2 Copyright (C) 90, 91, 95, 1996 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>
33 void strip_trailing_slashes ();
35 /* The name this program was run with. */
38 /* If nonzero, remove empty parent directories. */
39 static int empty_paths
;
41 /* If nonzero, display usage information and exit. */
44 /* If nonzero, print the version on standard output and exit. */
45 static int show_version
;
47 static struct option
const longopts
[] =
49 {"path", no_argument
, &empty_paths
, 1},
50 {"parents", no_argument
, &empty_paths
, 1},
51 {"help", no_argument
, &show_help
, 1},
52 {"version", no_argument
, &show_version
, 1},
56 /* Remove any empty parent directories of `path'.
57 Replaces '/' characters in `path' with NULs. */
60 remove_parents (char *path
)
66 slash
= strrchr (path
, '/');
69 /* Remove any characters after the slash, skipping any extra
71 while (slash
> path
&& *slash
== '/')
75 while (rmdir (path
) == 0);
82 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
86 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name
);
88 Remove the DIRECTORY(ies), if they are empty.\n\
90 -p, --parents remove explicit parent directories if being emptied\n\
91 --help display this help and exit\n\
92 --version output version information and exit\n"));
98 main (int argc
, char **argv
)
103 program_name
= argv
[0];
104 setlocale (LC_ALL
, "");
105 bindtextdomain (PACKAGE
, LOCALEDIR
);
106 textdomain (PACKAGE
);
110 while ((optc
= getopt_long (argc
, argv
, "p", longopts
, (int *) 0)) != EOF
)
114 case 0: /* Long option. */
126 printf ("rmdir - %s\n", PACKAGE_VERSION
);
135 error (0, 0, _("too few arguments"));
139 for (; optind
< argc
; ++optind
)
141 /* Stripping slashes is harmless for rmdir;
142 if the arg is not a directory, it will fail with ENOTDIR. */
143 strip_trailing_slashes (argv
[optind
]);
144 if (rmdir (argv
[optind
]) != 0)
146 error (0, errno
, "%s", argv
[optind
]);
149 else if (empty_paths
)
150 remove_parents (argv
[optind
]);