rmdir.c revision 99110
11558Srgrimes/*- 21558Srgrimes * Copyright (c) 1992, 1993, 1994 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 3538040Scharnierstatic char const copyright[] = 361558Srgrimes"@(#) Copyright (c) 1992, 1993, 1994\n\ 371558Srgrimes The Regents of the University of California. All rights reserved.\n"; 381558Srgrimes#endif /* not lint */ 391558Srgrimes 401558Srgrimes#ifndef lint 4138040Scharnier#if 0 421558Srgrimesstatic char sccsid[] = "@(#)rmdir.c 8.3 (Berkeley) 4/2/94"; 4338040Scharnier#endif 4438040Scharnier#endif /* not lint */ 4550476Speter#include <sys/cdefs.h> 461558Srgrimes__FBSDID("$FreeBSD: head/bin/rmdir/rmdir.c 99110 2002-06-30 05:15:05Z obrien $"); 471558Srgrimes 481558Srgrimes#include <err.h> 49102231Strhodes#include <errno.h> 501558Srgrimes#include <stdio.h> 511558Srgrimes#include <stdlib.h> 5242873Sluoqi#include <string.h> 5396478Sphk#include <unistd.h> 541558Srgrimes 551558Srgrimesint rm_path(char *); 5698542Smckusickvoid usage(void); 5798542Smckusick 581558Srgrimesint 591558Srgrimesmain(int argc, char *argv[]) 601558Srgrimes{ 611558Srgrimes int ch, errors; 621558Srgrimes int pflag; 6338040Scharnier 641558Srgrimes pflag = 0; 651558Srgrimes while ((ch = getopt(argc, argv, "p")) != -1) 6658047Ssheldonh switch(ch) { 671558Srgrimes case 'p': 681558Srgrimes pflag = 1; 691558Srgrimes break; 701558Srgrimes case '?': 711558Srgrimes default: 721558Srgrimes usage(); 731558Srgrimes } 741558Srgrimes argc -= optind; 751558Srgrimes argv += optind; 761558Srgrimes 771558Srgrimes if (argc == 0) 781558Srgrimes usage(); 791558Srgrimes 801558Srgrimes for (errors = 0; *argv; argv++) { 8198542Smckusick if (rmdir(*argv) < 0) { 8298542Smckusick warn("%s", *argv); 8392883Simp errors = 1; 8492883Simp } else if (pflag) 8592883Simp errors |= rm_path(*argv); 8692883Simp } 871558Srgrimes 881558Srgrimes exit(errors); 891558Srgrimes} 901558Srgrimes 911558Srgrimesint 921558Srgrimesrm_path(char *path) 9379750Sdd{ 9479750Sdd char *p; 951558Srgrimes 96105120Srwatson p = path + strlen(path); 97105120Srwatson while (--p > path && *p == '/') 9875377Smckusick ; 99103005Sphk *++p = '\0'; 10075377Smckusick while ((p = strrchr(path, '/')) != NULL) { 101105120Srwatson /* Delete trailing slashes. */ 1021558Srgrimes while (--p > path && *p == '/') 10379750Sdd ; 10479750Sdd *++p = '\0'; 10542873Sluoqi 10642873Sluoqi if (rmdir(path) < 0) { 10769314Scharnier warn("%s", path); 1081558Srgrimes return (1); 10969314Scharnier } 11069314Scharnier } 11169314Scharnier 112105120Srwatson return (0); 11369314Scharnier} 114105120Srwatson 115105120Srwatsonvoid 116105120Srwatsonusage(void) 117105120Srwatson{ 118105120Srwatson 119105120Srwatson (void)fprintf(stderr, "usage: rmdir [-p] directory ...\n"); 120105120Srwatson exit(1); 121105120Srwatson} 122105120Srwatson