rmdir.c revision 20422
1266087Sian/*- 2235348Smarius * Copyright (c) 1992, 1993, 1994 3235348Smarius * The Regents of the University of California. All rights reserved. 4235348Smarius * 5235348Smarius * Redistribution and use in source and binary forms, with or without 6235348Smarius * modification, are permitted provided that the following conditions 7235348Smarius * are met: 8235348Smarius * 1. Redistributions of source code must retain the above copyright 9235348Smarius * notice, this list of conditions and the following disclaimer. 10235348Smarius * 2. Redistributions in binary form must reproduce the above copyright 11235348Smarius * notice, this list of conditions and the following disclaimer in the 12235348Smarius * documentation and/or other materials provided with the distribution. 13235348Smarius * 3. All advertising materials mentioning features or use of this software 14235348Smarius * must display the following acknowledgement: 15235348Smarius * This product includes software developed by the University of 16235348Smarius * California, Berkeley and its contributors. 17235348Smarius * 4. Neither the name of the University nor the names of its contributors 18235348Smarius * may be used to endorse or promote products derived from this software 19235348Smarius * without specific prior written permission. 20238785Simp * 21235348Smarius * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22238785Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23235348Smarius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24235348Smarius * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25238785Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26235348Smarius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27235348Smarius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28266277Sian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29235348Smarius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30235348Smarius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31235348Smarius * SUCH DAMAGE. 32235348Smarius * 33235348Smarius * $Id: rmdir.c,v 1.2 1994/09/24 02:57:13 davidg Exp $ 34235348Smarius */ 35235348Smarius 36235348Smarius#ifndef lint 37235348Smariusstatic char const copyright[] = 38235348Smarius"@(#) Copyright (c) 1992, 1993, 1994\n\ 39235348Smarius The Regents of the University of California. All rights reserved.\n"; 40235348Smarius#endif /* not lint */ 41266087Sian 42235348Smarius#ifndef lint 43235348Smariusstatic char const sccsid[] = "@(#)rmdir.c 8.3 (Berkeley) 4/2/94"; 44235348Smarius#endif /* not lint */ 45235348Smarius 46266277Sian#include <err.h> 47235348Smarius#include <errno.h> 48235348Smarius#include <stdio.h> 49235348Smarius#include <stdlib.h> 50235348Smarius#include <string.h> 51266277Sian#include <unistd.h> 52266277Sian 53235348Smariusint rm_path __P((char *)); 54235348Smariusvoid usage __P((void)); 55235348Smarius 56235348Smariusint 57235348Smariusmain(argc, argv) 58235348Smarius int argc; 59235348Smarius char *argv[]; 60235348Smarius{ 61235348Smarius int ch, errors; 62235348Smarius int pflag; 63235348Smarius 64235348Smarius pflag = 0; 65235348Smarius while ((ch = getopt(argc, argv, "p")) != EOF) 66235348Smarius switch(ch) { 67235348Smarius case 'p': 68235348Smarius pflag = 1; 69235348Smarius break; 70235348Smarius case '?': 71235348Smarius default: 72235348Smarius usage(); 73235348Smarius } 74238823Simp argc -= optind; 75238823Simp argv += optind; 76238823Simp 77238823Simp if (argc == 0) 78238823Simp usage(); 79235348Smarius 80235348Smarius for (errors = 0; *argv; argv++) { 81238823Simp char *p; 82235348Smarius 83238823Simp /* Delete trailing slashes, per POSIX. */ 84238823Simp p = *argv + strlen(*argv); 85238823Simp while (--p > *argv && *p == '/') 86235348Smarius ; 87235348Smarius *++p = '\0'; 88235348Smarius 89235348Smarius if (rmdir(*argv) < 0) { 90235348Smarius warn("%s", *argv); 91235348Smarius errors = 1; 92235348Smarius } else if (pflag) 93235348Smarius errors |= rm_path(*argv); 94235348Smarius } 95235348Smarius 96235348Smarius exit(errors); 97235348Smarius} 98235348Smarius 99235348Smariusint 100235348Smariusrm_path(path) 101235348Smarius char *path; 102235348Smarius{ 103235348Smarius char *p; 104235348Smarius 105235348Smarius while ((p = strrchr(path, '/')) != NULL) { 106235348Smarius /* Delete trailing slashes. */ 107235348Smarius while (--p > path && *p == '/') 108235348Smarius ; 109235348Smarius *++p = '\0'; 110235348Smarius 111235348Smarius if (rmdir(path) < 0) { 112235348Smarius warn("%s", path); 113235348Smarius return (1); 114238785Simp } 115235348Smarius } 116235348Smarius 117235348Smarius return (0); 118235348Smarius} 119235348Smarius 120238785Simpvoid 121235348Smariususage() 122235348Smarius{ 123238823Simp 124235348Smarius (void)fprintf(stderr, "usage: rmdir [-p] directory ...\n"); 125238823Simp exit(1); 126238823Simp} 127235348Smarius