xref: /onnv-gate/usr/src/cmd/rmdir/rmdir.c (revision 213:552475a8865e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 1995 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * Rmdir(1) removes directory.
350Sstevel@tonic-gate  * If -p option is used, rmdir(1) tries to remove the directory
360Sstevel@tonic-gate  * and it's parent directories.  It exits with code 0 if the WHOLE
370Sstevel@tonic-gate  * given path is removed and 2 if part of path remains.
380Sstevel@tonic-gate  * Results are printed except when -s is used.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <stdio.h>
420Sstevel@tonic-gate #include <libgen.h>
430Sstevel@tonic-gate #include <errno.h>
440Sstevel@tonic-gate #include <string.h>
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include <stdlib.h>
470Sstevel@tonic-gate #include <locale.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 
50*213Smuffin int
510Sstevel@tonic-gate main(int argc, char **argv)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	char	*prog;
550Sstevel@tonic-gate 	int c, pflag, sflag, errflg, rc;
560Sstevel@tonic-gate 	char *ptr, *remain, *msg, *path;
570Sstevel@tonic-gate 	unsigned int pathlen;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	prog = argv[0];
600Sstevel@tonic-gate 	pflag = sflag = 0;
610Sstevel@tonic-gate 	errflg = 0;
620Sstevel@tonic-gate 	/* set effective uid, euid, to be same as real	*/
630Sstevel@tonic-gate 	/* uid, ruid.  Rmdir(2) checks search & write	*/
640Sstevel@tonic-gate 	/* permissions for euid, but for compatibility	*/
650Sstevel@tonic-gate 	/* the check must be done using ruid.		*/
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
680Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
690Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it wasn't */
700Sstevel@tonic-gate #endif
710Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (setuid(getuid()) == -1) {
740Sstevel@tonic-gate 		char	buf[80];
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 		(void) sprintf(buf, gettext("%s: setuid(2) failed"), prog);
770Sstevel@tonic-gate 		perror(buf);
780Sstevel@tonic-gate 		exit(1);
790Sstevel@tonic-gate 	}
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ps")) != EOF)
820Sstevel@tonic-gate 		switch (c) {
830Sstevel@tonic-gate 			case 'p':
840Sstevel@tonic-gate 				pflag++;
850Sstevel@tonic-gate 				break;
860Sstevel@tonic-gate 			case 's':
870Sstevel@tonic-gate 				sflag++;
880Sstevel@tonic-gate 				break;
890Sstevel@tonic-gate 			case '?':
900Sstevel@tonic-gate 				errflg++;
910Sstevel@tonic-gate 				break;
920Sstevel@tonic-gate 		}
930Sstevel@tonic-gate 	if (argc < 2 || errflg) {
940Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Usage: %s [-ps] dirname ...\n"),
950Sstevel@tonic-gate 		    prog);
960Sstevel@tonic-gate 		exit(2);
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 	errno = 0;
990Sstevel@tonic-gate 	argc -= optind;
1000Sstevel@tonic-gate 	argv = &argv[optind];
1010Sstevel@tonic-gate 	while (argc--) {
1020Sstevel@tonic-gate 		ptr = *argv++;
1030Sstevel@tonic-gate 		/*
1040Sstevel@tonic-gate 		 * -p option. Remove directory and parents.
1050Sstevel@tonic-gate 		 * Prints results of removing
1060Sstevel@tonic-gate 		 */
1070Sstevel@tonic-gate 		if (pflag) {
1080Sstevel@tonic-gate 			pathlen = (unsigned)strlen(ptr);
1090Sstevel@tonic-gate 			if ((path = (char *)malloc(pathlen + 4)) == NULL ||
1100Sstevel@tonic-gate 			    (remain = (char *)malloc(pathlen + 4)) == NULL) {
1110Sstevel@tonic-gate 				perror(prog);
1120Sstevel@tonic-gate 				exit(2);
1130Sstevel@tonic-gate 			}
1140Sstevel@tonic-gate 			(void) strcpy(path, ptr);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 			/*
1170Sstevel@tonic-gate 			 * rmdirp removes directory and parents
1180Sstevel@tonic-gate 			 * rc != 0 implies only part of path removed
1190Sstevel@tonic-gate 			 */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 			if (((rc = rmdirp(path, remain)) != 0) && !sflag) {
1220Sstevel@tonic-gate 				switch (rc) {
1230Sstevel@tonic-gate 				case -1:
1240Sstevel@tonic-gate 					if (errno == EEXIST)
1250Sstevel@tonic-gate 						msg = gettext(
1260Sstevel@tonic-gate 						    "Directory not empty");
1270Sstevel@tonic-gate 					else
1280Sstevel@tonic-gate 						msg = strerror(errno);
1290Sstevel@tonic-gate 					break;
1300Sstevel@tonic-gate 				case -2:
1310Sstevel@tonic-gate 					errno = EINVAL;
1320Sstevel@tonic-gate 					msg = gettext("Can not remove . or ..");
1330Sstevel@tonic-gate 					break;
1340Sstevel@tonic-gate 				case -3:
1350Sstevel@tonic-gate 					errno = EINVAL;
1360Sstevel@tonic-gate 					msg = gettext(
1370Sstevel@tonic-gate 					    "Can not remove current directory");
1380Sstevel@tonic-gate 					break;
1390Sstevel@tonic-gate 				}
1400Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: directory"
1410Sstevel@tonic-gate 				    " \"%s\": %s not removed; %s\n"),
1420Sstevel@tonic-gate 				    prog, ptr, remain, msg);
1430Sstevel@tonic-gate 			}
1440Sstevel@tonic-gate 			free(path);
1450Sstevel@tonic-gate 			free(remain);
1460Sstevel@tonic-gate 			continue;
1470Sstevel@tonic-gate 		}
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 		/* No -p option. Remove only one directory */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 		if (rmdir(ptr) == -1) {
1520Sstevel@tonic-gate 			switch (errno) {
1530Sstevel@tonic-gate 			case EEXIST:
1540Sstevel@tonic-gate 				msg = gettext("Directory not empty");
1550Sstevel@tonic-gate 				break;
1560Sstevel@tonic-gate 			case ENOTDIR:
1570Sstevel@tonic-gate 				msg = gettext("Path component not a directory");
1580Sstevel@tonic-gate 				break;
1590Sstevel@tonic-gate 			case ENOENT:
1600Sstevel@tonic-gate 				msg = gettext("Directory does not exist");
1610Sstevel@tonic-gate 				break;
1620Sstevel@tonic-gate 			case EACCES:
1630Sstevel@tonic-gate 				msg = gettext(
1640Sstevel@tonic-gate 				    "Search or write permission needed");
1650Sstevel@tonic-gate 				break;
1660Sstevel@tonic-gate 			case EBUSY:
1670Sstevel@tonic-gate 				msg = gettext(
1680Sstevel@tonic-gate 				    "Directory is a mount point or in use");
1690Sstevel@tonic-gate 				break;
1700Sstevel@tonic-gate 			case EROFS:
1710Sstevel@tonic-gate 				msg = gettext("Read-only file system");
1720Sstevel@tonic-gate 				break;
1730Sstevel@tonic-gate 			case EIO:
1740Sstevel@tonic-gate 				msg = gettext(
1750Sstevel@tonic-gate 				    "I/O error accessing file system");
1760Sstevel@tonic-gate 				break;
1770Sstevel@tonic-gate 			case EINVAL:
1780Sstevel@tonic-gate 				msg = gettext(
1790Sstevel@tonic-gate 				    "Can't remove current directory or ..");
1800Sstevel@tonic-gate 				break;
1810Sstevel@tonic-gate 			case EFAULT:
1820Sstevel@tonic-gate 			default:
1830Sstevel@tonic-gate 				msg = strerror(errno);
1840Sstevel@tonic-gate 				break;
1850Sstevel@tonic-gate 			}
1860Sstevel@tonic-gate 			(void) fprintf(stderr,
1870Sstevel@tonic-gate 			    gettext("%s: directory \"%s\": %s\n"),
1880Sstevel@tonic-gate 			    prog, ptr, msg);
1890Sstevel@tonic-gate 			continue;
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 	}
192*213Smuffin 	return (errno ? 2 : 0);
1930Sstevel@tonic-gate }
194