1 /* rmdir -- remove directories
2 Copyright (C) 90, 91, 1995-2001 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>
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "rmdir"
37 #define AUTHORS "David MacKenzie"
47 /* The name this program was run with. */
50 /* If nonzero, remove empty parent directories. */
51 static int empty_paths
;
53 /* If nonzero, don't treat failure to remove a nonempty directory
55 static int ignore_fail_on_non_empty
;
57 /* If nonzero, output a diagnostic for every directory processed. */
60 /* For long options that have no equivalent short option, use a
61 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
64 IGNORE_FAIL_ON_NON_EMPTY_OPTION
= CHAR_MAX
+ 1
67 static struct option
const longopts
[] =
69 /* Don't name this `--force' because it's not close enough in meaning
70 to e.g. rm's -f option. */
71 {"ignore-fail-on-non-empty", no_argument
, NULL
,
72 IGNORE_FAIL_ON_NON_EMPTY_OPTION
},
74 {"path", no_argument
, NULL
, 'p'},
75 {"parents", no_argument
, NULL
, 'p'},
76 {"verbose", no_argument
, NULL
, 'v'},
77 {GETOPT_HELP_OPTION_DECL
},
78 {GETOPT_VERSION_OPTION_DECL
},
82 /* Return nonzero if ERROR_NUMBER is one of the values associated
83 with a failed rmdir due to non-empty target directory. */
86 errno_rmdir_non_empty (int error_number
)
88 return (error_number
== RMDIR_ERRNO_NOT_EMPTY
);
91 /* Remove any empty parent directories of PATH.
92 If PATH contains slash characters, at least one of them
93 (beginning with the rightmost) is replaced with a NUL byte. */
96 remove_parents (char *path
)
103 slash
= strrchr (path
, '/');
106 /* Remove any characters after the slash, skipping any extra
108 while (slash
> path
&& *slash
== '/')
112 /* Give a diagnostic for each attempted removal if --verbose. */
114 error (0, 0, _("removing directory, %s"), path
);
120 /* Stop quietly if --ignore-fail-on-non-empty. */
121 if (ignore_fail_on_non_empty
122 && errno_rmdir_non_empty (errno
))
128 error (0, errno
, "%s", quote (path
));
140 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
144 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name
);
146 Remove the DIRECTORY(ies), if they are empty.\n\
148 --ignore-fail-on-non-empty\n\
149 ignore each failure that is solely because a directory\n\
153 -p, --parents remove DIRECTORY, then try to remove each directory\n\
154 component of that path name. E.g., `rmdir -p a/b/c' is\n\
155 similar to `rmdir a/b/c a/b a'.\n\
156 -v, --verbose output a diagnostic for every directory processed\n\
158 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
159 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
160 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
166 main (int argc
, char **argv
)
171 program_name
= argv
[0];
172 setlocale (LC_ALL
, "");
173 bindtextdomain (PACKAGE
, LOCALEDIR
);
174 textdomain (PACKAGE
);
176 atexit (close_stdout
);
180 while ((optc
= getopt_long (argc
, argv
, "pv", longopts
, NULL
)) != -1)
184 case 0: /* Long option. */
189 case IGNORE_FAIL_ON_NON_EMPTY_OPTION
:
190 ignore_fail_on_non_empty
= 1;
195 case_GETOPT_HELP_CHAR
;
196 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
204 error (0, 0, _("too few arguments"));
208 for (; optind
< argc
; ++optind
)
211 char *dir
= argv
[optind
];
213 /* Give a diagnostic for each attempted removal if --verbose. */
215 error (0, 0, _("removing directory, %s"), dir
);
221 if (ignore_fail_on_non_empty
222 && errno_rmdir_non_empty (errno
))
225 error (0, errno
, "%s", quote (dir
));
228 else if (empty_paths
)
230 errors
+= remove_parents (dir
);
234 exit (errors
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);