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