xref: /titanic_53/usr/src/cmd/rmdir/rmdir.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1995 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  * Rmdir(1) removes directory.
35*7c478bd9Sstevel@tonic-gate  * If -p option is used, rmdir(1) tries to remove the directory
36*7c478bd9Sstevel@tonic-gate  * and it's parent directories.  It exits with code 0 if the WHOLE
37*7c478bd9Sstevel@tonic-gate  * given path is removed and 2 if part of path remains.
38*7c478bd9Sstevel@tonic-gate  * Results are printed except when -s is used.
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include <stdio.h>
42*7c478bd9Sstevel@tonic-gate #include <libgen.h>
43*7c478bd9Sstevel@tonic-gate #include <errno.h>
44*7c478bd9Sstevel@tonic-gate #include <string.h>
45*7c478bd9Sstevel@tonic-gate #include <unistd.h>
46*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
47*7c478bd9Sstevel@tonic-gate #include <locale.h>
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate void
51*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	char	*prog;
55*7c478bd9Sstevel@tonic-gate 	int c, pflag, sflag, errflg, rc;
56*7c478bd9Sstevel@tonic-gate 	char *ptr, *remain, *msg, *path;
57*7c478bd9Sstevel@tonic-gate 	unsigned int pathlen;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	prog = argv[0];
60*7c478bd9Sstevel@tonic-gate 	pflag = sflag = 0;
61*7c478bd9Sstevel@tonic-gate 	errflg = 0;
62*7c478bd9Sstevel@tonic-gate 	/* set effective uid, euid, to be same as real	*/
63*7c478bd9Sstevel@tonic-gate 	/* uid, ruid.  Rmdir(2) checks search & write	*/
64*7c478bd9Sstevel@tonic-gate 	/* permissions for euid, but for compatibility	*/
65*7c478bd9Sstevel@tonic-gate 	/* the check must be done using ruid.		*/
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
68*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
69*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it wasn't */
70*7c478bd9Sstevel@tonic-gate #endif
71*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	if (setuid(getuid()) == -1) {
74*7c478bd9Sstevel@tonic-gate 		char	buf[80];
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, gettext("%s: setuid(2) failed"), prog);
77*7c478bd9Sstevel@tonic-gate 		perror(buf);
78*7c478bd9Sstevel@tonic-gate 		exit(1);
79*7c478bd9Sstevel@tonic-gate 	}
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ps")) != EOF)
82*7c478bd9Sstevel@tonic-gate 		switch (c) {
83*7c478bd9Sstevel@tonic-gate 			case 'p':
84*7c478bd9Sstevel@tonic-gate 				pflag++;
85*7c478bd9Sstevel@tonic-gate 				break;
86*7c478bd9Sstevel@tonic-gate 			case 's':
87*7c478bd9Sstevel@tonic-gate 				sflag++;
88*7c478bd9Sstevel@tonic-gate 				break;
89*7c478bd9Sstevel@tonic-gate 			case '?':
90*7c478bd9Sstevel@tonic-gate 				errflg++;
91*7c478bd9Sstevel@tonic-gate 				break;
92*7c478bd9Sstevel@tonic-gate 		}
93*7c478bd9Sstevel@tonic-gate 	if (argc < 2 || errflg) {
94*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Usage: %s [-ps] dirname ...\n"),
95*7c478bd9Sstevel@tonic-gate 		    prog);
96*7c478bd9Sstevel@tonic-gate 		exit(2);
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 	errno = 0;
99*7c478bd9Sstevel@tonic-gate 	argc -= optind;
100*7c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
101*7c478bd9Sstevel@tonic-gate 	while (argc--) {
102*7c478bd9Sstevel@tonic-gate 		ptr = *argv++;
103*7c478bd9Sstevel@tonic-gate 		/*
104*7c478bd9Sstevel@tonic-gate 		 * -p option. Remove directory and parents.
105*7c478bd9Sstevel@tonic-gate 		 * Prints results of removing
106*7c478bd9Sstevel@tonic-gate 		 */
107*7c478bd9Sstevel@tonic-gate 		if (pflag) {
108*7c478bd9Sstevel@tonic-gate 			pathlen = (unsigned)strlen(ptr);
109*7c478bd9Sstevel@tonic-gate 			if ((path = (char *)malloc(pathlen + 4)) == NULL ||
110*7c478bd9Sstevel@tonic-gate 			    (remain = (char *)malloc(pathlen + 4)) == NULL) {
111*7c478bd9Sstevel@tonic-gate 				perror(prog);
112*7c478bd9Sstevel@tonic-gate 				exit(2);
113*7c478bd9Sstevel@tonic-gate 			}
114*7c478bd9Sstevel@tonic-gate 			(void) strcpy(path, ptr);
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 			/*
117*7c478bd9Sstevel@tonic-gate 			 * rmdirp removes directory and parents
118*7c478bd9Sstevel@tonic-gate 			 * rc != 0 implies only part of path removed
119*7c478bd9Sstevel@tonic-gate 			 */
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 			if (((rc = rmdirp(path, remain)) != 0) && !sflag) {
122*7c478bd9Sstevel@tonic-gate 				switch (rc) {
123*7c478bd9Sstevel@tonic-gate 				case -1:
124*7c478bd9Sstevel@tonic-gate 					if (errno == EEXIST)
125*7c478bd9Sstevel@tonic-gate 						msg = gettext(
126*7c478bd9Sstevel@tonic-gate 						    "Directory not empty");
127*7c478bd9Sstevel@tonic-gate 					else
128*7c478bd9Sstevel@tonic-gate 						msg = strerror(errno);
129*7c478bd9Sstevel@tonic-gate 					break;
130*7c478bd9Sstevel@tonic-gate 				case -2:
131*7c478bd9Sstevel@tonic-gate 					errno = EINVAL;
132*7c478bd9Sstevel@tonic-gate 					msg = gettext("Can not remove . or ..");
133*7c478bd9Sstevel@tonic-gate 					break;
134*7c478bd9Sstevel@tonic-gate 				case -3:
135*7c478bd9Sstevel@tonic-gate 					errno = EINVAL;
136*7c478bd9Sstevel@tonic-gate 					msg = gettext(
137*7c478bd9Sstevel@tonic-gate 					    "Can not remove current directory");
138*7c478bd9Sstevel@tonic-gate 					break;
139*7c478bd9Sstevel@tonic-gate 				}
140*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: directory"
141*7c478bd9Sstevel@tonic-gate 				    " \"%s\": %s not removed; %s\n"),
142*7c478bd9Sstevel@tonic-gate 				    prog, ptr, remain, msg);
143*7c478bd9Sstevel@tonic-gate 			}
144*7c478bd9Sstevel@tonic-gate 			free(path);
145*7c478bd9Sstevel@tonic-gate 			free(remain);
146*7c478bd9Sstevel@tonic-gate 			continue;
147*7c478bd9Sstevel@tonic-gate 		}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 		/* No -p option. Remove only one directory */
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 		if (rmdir(ptr) == -1) {
152*7c478bd9Sstevel@tonic-gate 			switch (errno) {
153*7c478bd9Sstevel@tonic-gate 			case EEXIST:
154*7c478bd9Sstevel@tonic-gate 				msg = gettext("Directory not empty");
155*7c478bd9Sstevel@tonic-gate 				break;
156*7c478bd9Sstevel@tonic-gate 			case ENOTDIR:
157*7c478bd9Sstevel@tonic-gate 				msg = gettext("Path component not a directory");
158*7c478bd9Sstevel@tonic-gate 				break;
159*7c478bd9Sstevel@tonic-gate 			case ENOENT:
160*7c478bd9Sstevel@tonic-gate 				msg = gettext("Directory does not exist");
161*7c478bd9Sstevel@tonic-gate 				break;
162*7c478bd9Sstevel@tonic-gate 			case EACCES:
163*7c478bd9Sstevel@tonic-gate 				msg = gettext(
164*7c478bd9Sstevel@tonic-gate 				    "Search or write permission needed");
165*7c478bd9Sstevel@tonic-gate 				break;
166*7c478bd9Sstevel@tonic-gate 			case EBUSY:
167*7c478bd9Sstevel@tonic-gate 				msg = gettext(
168*7c478bd9Sstevel@tonic-gate 				    "Directory is a mount point or in use");
169*7c478bd9Sstevel@tonic-gate 				break;
170*7c478bd9Sstevel@tonic-gate 			case EROFS:
171*7c478bd9Sstevel@tonic-gate 				msg = gettext("Read-only file system");
172*7c478bd9Sstevel@tonic-gate 				break;
173*7c478bd9Sstevel@tonic-gate 			case EIO:
174*7c478bd9Sstevel@tonic-gate 				msg = gettext(
175*7c478bd9Sstevel@tonic-gate 				    "I/O error accessing file system");
176*7c478bd9Sstevel@tonic-gate 				break;
177*7c478bd9Sstevel@tonic-gate 			case EINVAL:
178*7c478bd9Sstevel@tonic-gate 				msg = gettext(
179*7c478bd9Sstevel@tonic-gate 				    "Can't remove current directory or ..");
180*7c478bd9Sstevel@tonic-gate 				break;
181*7c478bd9Sstevel@tonic-gate 			case EFAULT:
182*7c478bd9Sstevel@tonic-gate 			default:
183*7c478bd9Sstevel@tonic-gate 				msg = strerror(errno);
184*7c478bd9Sstevel@tonic-gate 				break;
185*7c478bd9Sstevel@tonic-gate 			}
186*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
187*7c478bd9Sstevel@tonic-gate 			    gettext("%s: directory \"%s\": %s\n"),
188*7c478bd9Sstevel@tonic-gate 			    prog, ptr, msg);
189*7c478bd9Sstevel@tonic-gate 			continue;
190*7c478bd9Sstevel@tonic-gate 		}
191*7c478bd9Sstevel@tonic-gate 	}
192*7c478bd9Sstevel@tonic-gate 	exit(errno ? 2 : 0);
193*7c478bd9Sstevel@tonic-gate }
194