rmdir.c revision 129285
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1992, 1993, 1994 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * Redistribution and use in source and binary forms, with or without 61556Srgrimes * modification, are permitted provided that the following conditions 71556Srgrimes * are met: 81556Srgrimes * 1. Redistributions of source code must retain the above copyright 91556Srgrimes * notice, this list of conditions and the following disclaimer. 101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111556Srgrimes * notice, this list of conditions and the following disclaimer in the 121556Srgrimes * documentation and/or other materials provided with the distribution. 131556Srgrimes * 4. Neither the name of the University nor the names of its contributors 141556Srgrimes * may be used to endorse or promote products derived from this software 151556Srgrimes * without specific prior written permission. 161556Srgrimes * 171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271556Srgrimes * SUCH DAMAGE. 281556Srgrimes */ 291556Srgrimes 30114433Sobrien#if 0 311556Srgrimes#ifndef lint 3220422Sstevestatic char const copyright[] = 331556Srgrimes"@(#) Copyright (c) 1992, 1993, 1994\n\ 341556Srgrimes The Regents of the University of California. All rights reserved.\n"; 351556Srgrimes#endif /* not lint */ 361556Srgrimes 371556Srgrimes#ifndef lint 3836149Scharnierstatic char sccsid[] = "@(#)rmdir.c 8.3 (Berkeley) 4/2/94"; 39114433Sobrien#endif /* not lint */ 4036149Scharnier#endif 4199110Sobrien#include <sys/cdefs.h> 4299110Sobrien__FBSDID("$FreeBSD: head/bin/rmdir/rmdir.c 129285 2004-05-16 20:41:11Z ru $"); 431556Srgrimes 441556Srgrimes#include <err.h> 451556Srgrimes#include <errno.h> 461556Srgrimes#include <stdio.h> 471556Srgrimes#include <stdlib.h> 481556Srgrimes#include <string.h> 491556Srgrimes#include <unistd.h> 501556Srgrimes 51127257Sdesstatic int rm_path(char *); 52127257Sdesstatic void usage(void); 531556Srgrimes 54127257Sdesstatic int pflag; 55127257Sdesstatic int vflag; 56127257Sdes 571556Srgrimesint 5890110Simpmain(int argc, char *argv[]) 591556Srgrimes{ 601556Srgrimes int ch, errors; 611556Srgrimes 62127257Sdes while ((ch = getopt(argc, argv, "pv")) != -1) 631556Srgrimes switch(ch) { 6420422Ssteve case 'p': 6520422Ssteve pflag = 1; 6620422Ssteve break; 67127257Sdes case 'v': 68127257Sdes vflag = 1; 69127257Sdes break; 701556Srgrimes case '?': 711556Srgrimes default: 721556Srgrimes usage(); 731556Srgrimes } 741556Srgrimes argc -= optind; 751556Srgrimes argv += optind; 761556Srgrimes 771556Srgrimes if (argc == 0) 781556Srgrimes usage(); 791556Srgrimes 8020422Ssteve for (errors = 0; *argv; argv++) { 81127257Sdes if (pflag) { 8220422Ssteve errors |= rm_path(*argv); 83127257Sdes } else { 84127257Sdes if (rmdir(*argv) < 0) { 85127257Sdes warn("%s", *argv); 86127257Sdes errors = 1; 87127257Sdes } 88127257Sdes if (vflag) 89127257Sdes printf("%s\n", *argv); 90127257Sdes } 9120422Ssteve } 9220422Ssteve 931556Srgrimes exit(errors); 941556Srgrimes} 951556Srgrimes 96127257Sdesstatic int 9790110Simprm_path(char *path) 9820422Ssteve{ 9920422Ssteve char *p; 10020422Ssteve 10162213Snbm p = path + strlen(path); 10262213Snbm while (--p > path && *p == '/') 10362213Snbm ; 10462213Snbm *++p = '\0'; 10520422Ssteve while ((p = strrchr(path, '/')) != NULL) { 10620422Ssteve /* Delete trailing slashes. */ 10720422Ssteve while (--p > path && *p == '/') 10820422Ssteve ; 10920422Ssteve *++p = '\0'; 11020422Ssteve 11120422Ssteve if (rmdir(path) < 0) { 11220422Ssteve warn("%s", path); 11320422Ssteve return (1); 11420422Ssteve } 115127257Sdes if (vflag) 116127257Sdes printf("%s\n", path); 11720422Ssteve } 11820422Ssteve 11920422Ssteve return (0); 12020422Ssteve} 12120422Ssteve 122127257Sdesstatic void 12390110Simpusage(void) 1241556Srgrimes{ 1251556Srgrimes 126129285Sru (void)fprintf(stderr, "usage: rmdir [-pv] directory ...\n"); 1271556Srgrimes exit(1); 1281556Srgrimes} 129