xref: /illumos-gate/usr/src/cmd/rmdir/rmdir.c (revision 9782b1c323d5daef57c9b4983fedfedd4b3be96f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9782b1c3SJoep Vesseur  * Common Development and Distribution License (the "License").
6*9782b1c3SJoep Vesseur  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*9782b1c3SJoep Vesseur  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Rmdir(1) removes directory.
327c478bd9Sstevel@tonic-gate  * If -p option is used, rmdir(1) tries to remove the directory
337c478bd9Sstevel@tonic-gate  * and it's parent directories.  It exits with code 0 if the WHOLE
347c478bd9Sstevel@tonic-gate  * given path is removed and 2 if part of path remains.
357c478bd9Sstevel@tonic-gate  * Results are printed except when -s is used.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <libgen.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <locale.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 
474fce32e1Smuffin int
main(int argc,char ** argv)487c478bd9Sstevel@tonic-gate main(int argc, char **argv)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	char	*prog;
527c478bd9Sstevel@tonic-gate 	int c, pflag, sflag, errflg, rc;
537c478bd9Sstevel@tonic-gate 	char *ptr, *remain, *msg, *path;
547c478bd9Sstevel@tonic-gate 	unsigned int pathlen;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	prog = argv[0];
577c478bd9Sstevel@tonic-gate 	pflag = sflag = 0;
587c478bd9Sstevel@tonic-gate 	errflg = 0;
597c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
607c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
617c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it wasn't */
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ps")) != EOF)
667c478bd9Sstevel@tonic-gate 		switch (c) {
677c478bd9Sstevel@tonic-gate 			case 'p':
687c478bd9Sstevel@tonic-gate 				pflag++;
697c478bd9Sstevel@tonic-gate 				break;
707c478bd9Sstevel@tonic-gate 			case 's':
717c478bd9Sstevel@tonic-gate 				sflag++;
727c478bd9Sstevel@tonic-gate 				break;
737c478bd9Sstevel@tonic-gate 			case '?':
747c478bd9Sstevel@tonic-gate 				errflg++;
757c478bd9Sstevel@tonic-gate 				break;
767c478bd9Sstevel@tonic-gate 		}
777c478bd9Sstevel@tonic-gate 	if (argc < 2 || errflg) {
787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Usage: %s [-ps] dirname ...\n"),
797c478bd9Sstevel@tonic-gate 		    prog);
807c478bd9Sstevel@tonic-gate 		exit(2);
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 	errno = 0;
837c478bd9Sstevel@tonic-gate 	argc -= optind;
847c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
857c478bd9Sstevel@tonic-gate 	while (argc--) {
867c478bd9Sstevel@tonic-gate 		ptr = *argv++;
877c478bd9Sstevel@tonic-gate 		/*
887c478bd9Sstevel@tonic-gate 		 * -p option. Remove directory and parents.
897c478bd9Sstevel@tonic-gate 		 * Prints results of removing
907c478bd9Sstevel@tonic-gate 		 */
917c478bd9Sstevel@tonic-gate 		if (pflag) {
927c478bd9Sstevel@tonic-gate 			pathlen = (unsigned)strlen(ptr);
937c478bd9Sstevel@tonic-gate 			if ((path = (char *)malloc(pathlen + 4)) == NULL ||
947c478bd9Sstevel@tonic-gate 			    (remain = (char *)malloc(pathlen + 4)) == NULL) {
957c478bd9Sstevel@tonic-gate 				perror(prog);
967c478bd9Sstevel@tonic-gate 				exit(2);
977c478bd9Sstevel@tonic-gate 			}
987c478bd9Sstevel@tonic-gate 			(void) strcpy(path, ptr);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 			/*
1017c478bd9Sstevel@tonic-gate 			 * rmdirp removes directory and parents
1027c478bd9Sstevel@tonic-gate 			 * rc != 0 implies only part of path removed
1037c478bd9Sstevel@tonic-gate 			 */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 			if (((rc = rmdirp(path, remain)) != 0) && !sflag) {
1067c478bd9Sstevel@tonic-gate 				switch (rc) {
1077c478bd9Sstevel@tonic-gate 				case -1:
1087c478bd9Sstevel@tonic-gate 					if (errno == EEXIST)
1097c478bd9Sstevel@tonic-gate 						msg = gettext(
1107c478bd9Sstevel@tonic-gate 						    "Directory not empty");
1117c478bd9Sstevel@tonic-gate 					else
1127c478bd9Sstevel@tonic-gate 						msg = strerror(errno);
1137c478bd9Sstevel@tonic-gate 					break;
1147c478bd9Sstevel@tonic-gate 				case -2:
1157c478bd9Sstevel@tonic-gate 					errno = EINVAL;
1167c478bd9Sstevel@tonic-gate 					msg = gettext("Can not remove . or ..");
1177c478bd9Sstevel@tonic-gate 					break;
1187c478bd9Sstevel@tonic-gate 				case -3:
1197c478bd9Sstevel@tonic-gate 					errno = EINVAL;
1207c478bd9Sstevel@tonic-gate 					msg = gettext(
1217c478bd9Sstevel@tonic-gate 					    "Can not remove current directory");
1227c478bd9Sstevel@tonic-gate 					break;
1237c478bd9Sstevel@tonic-gate 				}
1247c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: directory"
1257c478bd9Sstevel@tonic-gate 				    " \"%s\": %s not removed; %s\n"),
1267c478bd9Sstevel@tonic-gate 				    prog, ptr, remain, msg);
1277c478bd9Sstevel@tonic-gate 			}
1287c478bd9Sstevel@tonic-gate 			free(path);
1297c478bd9Sstevel@tonic-gate 			free(remain);
1307c478bd9Sstevel@tonic-gate 			continue;
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		/* No -p option. Remove only one directory */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		if (rmdir(ptr) == -1) {
1367c478bd9Sstevel@tonic-gate 			switch (errno) {
1377c478bd9Sstevel@tonic-gate 			case EEXIST:
1387c478bd9Sstevel@tonic-gate 				msg = gettext("Directory not empty");
1397c478bd9Sstevel@tonic-gate 				break;
1407c478bd9Sstevel@tonic-gate 			case ENOTDIR:
1417c478bd9Sstevel@tonic-gate 				msg = gettext("Path component not a directory");
1427c478bd9Sstevel@tonic-gate 				break;
1437c478bd9Sstevel@tonic-gate 			case ENOENT:
1447c478bd9Sstevel@tonic-gate 				msg = gettext("Directory does not exist");
1457c478bd9Sstevel@tonic-gate 				break;
1467c478bd9Sstevel@tonic-gate 			case EACCES:
1477c478bd9Sstevel@tonic-gate 				msg = gettext(
1487c478bd9Sstevel@tonic-gate 				    "Search or write permission needed");
1497c478bd9Sstevel@tonic-gate 				break;
1507c478bd9Sstevel@tonic-gate 			case EBUSY:
1517c478bd9Sstevel@tonic-gate 				msg = gettext(
1527c478bd9Sstevel@tonic-gate 				    "Directory is a mount point or in use");
1537c478bd9Sstevel@tonic-gate 				break;
1547c478bd9Sstevel@tonic-gate 			case EROFS:
1557c478bd9Sstevel@tonic-gate 				msg = gettext("Read-only file system");
1567c478bd9Sstevel@tonic-gate 				break;
1577c478bd9Sstevel@tonic-gate 			case EIO:
1587c478bd9Sstevel@tonic-gate 				msg = gettext(
1597c478bd9Sstevel@tonic-gate 				    "I/O error accessing file system");
1607c478bd9Sstevel@tonic-gate 				break;
1617c478bd9Sstevel@tonic-gate 			case EINVAL:
1627c478bd9Sstevel@tonic-gate 				msg = gettext(
1637c478bd9Sstevel@tonic-gate 				    "Can't remove current directory or ..");
1647c478bd9Sstevel@tonic-gate 				break;
1657c478bd9Sstevel@tonic-gate 			case EFAULT:
1667c478bd9Sstevel@tonic-gate 			default:
1677c478bd9Sstevel@tonic-gate 				msg = strerror(errno);
1687c478bd9Sstevel@tonic-gate 				break;
1697c478bd9Sstevel@tonic-gate 			}
1707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1717c478bd9Sstevel@tonic-gate 			    gettext("%s: directory \"%s\": %s\n"),
1727c478bd9Sstevel@tonic-gate 			    prog, ptr, msg);
1737c478bd9Sstevel@tonic-gate 			continue;
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 	}
1764fce32e1Smuffin 	return (errno ? 2 : 0);
1777c478bd9Sstevel@tonic-gate }
178