4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
31 * Rmdir(1) removes directory.
32 * If -p option is used, rmdir(1) tries to remove the directory
33 * and it's parent directories. It exits with code 0 if the WHOLE
34 * given path is removed and 2 if part of path remains.
35 * Results are printed except when -s is used.
48 main(int argc
, char **argv
)
52 int c
, pflag
, sflag
, errflg
, rc
;
53 char *ptr
, *remain
, *msg
, *path
;
59 (void) setlocale(LC_ALL
, "");
60 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
61 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */
63 (void) textdomain(TEXT_DOMAIN
);
65 while ((c
= getopt(argc
, argv
, "ps")) != EOF
)
77 if (argc
< 2 || errflg
) {
78 (void) fprintf(stderr
, gettext("Usage: %s [-ps] dirname ...\n"),
88 * -p option. Remove directory and parents.
89 * Prints results of removing
92 pathlen
= (unsigned)strlen(ptr
);
93 if ((path
= (char *)malloc(pathlen
+ 4)) == NULL
||
94 (remain
= (char *)malloc(pathlen
+ 4)) == NULL
) {
98 (void) strcpy(path
, ptr
);
101 * rmdirp removes directory and parents
102 * rc != 0 implies only part of path removed
105 if (((rc
= rmdirp(path
, remain
)) != 0) && !sflag
) {
110 "Directory not empty");
112 msg
= strerror(errno
);
116 msg
= gettext("Can not remove . or ..");
121 "Can not remove current directory");
124 (void) fprintf(stderr
, gettext("%s: directory"
125 " \"%s\": %s not removed; %s\n"),
126 prog
, ptr
, remain
, msg
);
133 /* No -p option. Remove only one directory */
135 if (rmdir(ptr
) == -1) {
138 msg
= gettext("Directory not empty");
141 msg
= gettext("Path component not a directory");
144 msg
= gettext("Directory does not exist");
148 "Search or write permission needed");
152 "Directory is a mount point or in use");
155 msg
= gettext("Read-only file system");
159 "I/O error accessing file system");
163 "Can't remove current directory or ..");
167 msg
= strerror(errno
);
170 (void) fprintf(stderr
,
171 gettext("%s: directory \"%s\": %s\n"),
176 return (errno
? 2 : 0);