rmdir.c revision 140851
191100Sdes/*- 291100Sdes * Copyright (c) 1992, 1993, 1994 3115619Sdes * The Regents of the University of California. All rights reserved. 491100Sdes * 591100Sdes * Redistribution and use in source and binary forms, with or without 691100Sdes * modification, are permitted provided that the following conditions 799158Sdes * are met: 899158Sdes * 1. Redistributions of source code must retain the above copyright 999158Sdes * notice, this list of conditions and the following disclaimer. 1091100Sdes * 2. Redistributions in binary form must reproduce the above copyright 1191100Sdes * notice, this list of conditions and the following disclaimer in the 1291100Sdes * documentation and/or other materials provided with the distribution. 1391100Sdes * 4. Neither the name of the University nor the names of its contributors 1491100Sdes * may be used to endorse or promote products derived from this software 1591100Sdes * without specific prior written permission. 1691100Sdes * 1791100Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1891100Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1991100Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2091100Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2191100Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2291100Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2391100Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2491100Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2591100Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2691100Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2791100Sdes * SUCH DAMAGE. 2891100Sdes */ 2991100Sdes 3091100Sdes#if 0 3191100Sdes#ifndef lint 3291100Sdesstatic char const copyright[] = 3391100Sdes"@(#) Copyright (c) 1992, 1993, 1994\n\ 3491100Sdes The Regents of the University of California. All rights reserved.\n"; 35117610Sdes#endif /* not lint */ 3691100Sdes 3791100Sdes#ifndef lint 3891100Sdesstatic char sccsid[] = "@(#)rmdir.c 8.3 (Berkeley) 4/2/94"; 39117610Sdes#endif /* not lint */ 4091100Sdes#endif 4199158Sdes#include <sys/cdefs.h> 42117610Sdes__FBSDID("$FreeBSD: head/bin/rmdir/rmdir.c 140851 2005-01-26 06:51:28Z ssouhlal $"); 4391100Sdes 4491100Sdes#include <err.h> 4599158Sdes#include <stdio.h> 46115619Sdes#include <stdlib.h> 4799158Sdes#include <string.h> 4899158Sdes#include <unistd.h> 4999158Sdes 5099158Sdesstatic int rm_path(char *); 5199158Sdesstatic void usage(void); 5299158Sdes 5399158Sdesstatic int pflag; 5499158Sdesstatic int vflag; 5599158Sdes 5699158Sdesint 5799158Sdesmain(int argc, char *argv[]) 5899158Sdes{ 5999158Sdes int ch, errors; 6099158Sdes 6199158Sdes while ((ch = getopt(argc, argv, "pv")) != -1) 6299158Sdes switch(ch) { 6399158Sdes case 'p': 6499158Sdes pflag = 1; 6599158Sdes break; 6699158Sdes case 'v': 6799158Sdes vflag = 1; 6899158Sdes break; 6999158Sdes case '?': 7099158Sdes default: 7199158Sdes usage(); 7299158Sdes } 7399158Sdes argc -= optind; 7499158Sdes argv += optind; 7599158Sdes 7699158Sdes if (argc == 0) 7799158Sdes usage(); 7899158Sdes 7999158Sdes for (errors = 0; *argv; argv++) { 8099158Sdes if (rmdir(*argv) < 0) { 8191100Sdes warn("%s", *argv); 8291100Sdes errors = 1; 8391100Sdes } else { 8491100Sdes if (vflag) 8591100Sdes printf("%s\n", *argv); 8691100Sdes if (pflag) 8791100Sdes errors |= rm_path(*argv); 8891100Sdes } 8991100Sdes } 9091100Sdes 9191100Sdes exit(errors); 9291100Sdes} 9391100Sdes 9491100Sdesstatic int 9591100Sdesrm_path(char *path) 9691100Sdes{ 9791100Sdes char *p; 9891100Sdes 9991100Sdes p = path + strlen(path); 10091100Sdes while (--p > path && *p == '/') 10191100Sdes ; 10291100Sdes *++p = '\0'; 10391100Sdes while ((p = strrchr(path, '/')) != NULL) { 10491100Sdes /* Delete trailing slashes. */ 10591100Sdes while (--p >= path && *p == '/') 10691100Sdes ; 10791100Sdes *++p = '\0'; 10891100Sdes if (p == path) 10991100Sdes break; 11091100Sdes 11191100Sdes if (rmdir(path) < 0) { 11291100Sdes warn("%s", path); 11391100Sdes return (1); 11491100Sdes } 11591100Sdes if (vflag) 11691100Sdes printf("%s\n", path); 11791100Sdes } 11891100Sdes 11991100Sdes return (0); 12091100Sdes} 12191100Sdes 12291100Sdesstatic void 12391100Sdesusage(void) 12491100Sdes{ 12591100Sdes 12691100Sdes (void)fprintf(stderr, "usage: rmdir [-pv] directory ...\n"); 12791100Sdes exit(1); 12891100Sdes} 12991100Sdes