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