9162 libpp: this statement may fall through
[unleashed.git] / usr / src / cmd / rmdir / rmdir.c
blob4ab41258ffe59cff6a02492f61fcdb5fc7405058
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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.
38 #include <stdio.h>
39 #include <libgen.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <locale.h>
47 int
48 main(int argc, char **argv)
51 char *prog;
52 int c, pflag, sflag, errflg, rc;
53 char *ptr, *remain, *msg, *path;
54 unsigned int pathlen;
56 prog = argv[0];
57 pflag = sflag = 0;
58 errflg = 0;
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 */
62 #endif
63 (void) textdomain(TEXT_DOMAIN);
65 while ((c = getopt(argc, argv, "ps")) != EOF)
66 switch (c) {
67 case 'p':
68 pflag++;
69 break;
70 case 's':
71 sflag++;
72 break;
73 case '?':
74 errflg++;
75 break;
77 if (argc < 2 || errflg) {
78 (void) fprintf(stderr, gettext("Usage: %s [-ps] dirname ...\n"),
79 prog);
80 exit(2);
82 errno = 0;
83 argc -= optind;
84 argv = &argv[optind];
85 while (argc--) {
86 ptr = *argv++;
88 * -p option. Remove directory and parents.
89 * Prints results of removing
91 if (pflag) {
92 pathlen = (unsigned)strlen(ptr);
93 if ((path = (char *)malloc(pathlen + 4)) == NULL ||
94 (remain = (char *)malloc(pathlen + 4)) == NULL) {
95 perror(prog);
96 exit(2);
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) {
106 switch (rc) {
107 case -1:
108 if (errno == EEXIST)
109 msg = gettext(
110 "Directory not empty");
111 else
112 msg = strerror(errno);
113 break;
114 case -2:
115 errno = EINVAL;
116 msg = gettext("Can not remove . or ..");
117 break;
118 case -3:
119 errno = EINVAL;
120 msg = gettext(
121 "Can not remove current directory");
122 break;
124 (void) fprintf(stderr, gettext("%s: directory"
125 " \"%s\": %s not removed; %s\n"),
126 prog, ptr, remain, msg);
128 free(path);
129 free(remain);
130 continue;
133 /* No -p option. Remove only one directory */
135 if (rmdir(ptr) == -1) {
136 switch (errno) {
137 case EEXIST:
138 msg = gettext("Directory not empty");
139 break;
140 case ENOTDIR:
141 msg = gettext("Path component not a directory");
142 break;
143 case ENOENT:
144 msg = gettext("Directory does not exist");
145 break;
146 case EACCES:
147 msg = gettext(
148 "Search or write permission needed");
149 break;
150 case EBUSY:
151 msg = gettext(
152 "Directory is a mount point or in use");
153 break;
154 case EROFS:
155 msg = gettext("Read-only file system");
156 break;
157 case EIO:
158 msg = gettext(
159 "I/O error accessing file system");
160 break;
161 case EINVAL:
162 msg = gettext(
163 "Can't remove current directory or ..");
164 break;
165 case EFAULT:
166 default:
167 msg = strerror(errno);
168 break;
170 (void) fprintf(stderr,
171 gettext("%s: directory \"%s\": %s\n"),
172 prog, ptr, msg);
173 continue;
176 return (errno ? 2 : 0);