10SN/A/*- 29914SN/A * SPDX-License-Identifier: BSD-3-Clause 30SN/A * 40SN/A * Copyright (c) 1992, 1993, 1994 50SN/A * The Regents of the University of California. All rights reserved. 60SN/A * 72362SN/A * Redistribution and use in source and binary forms, with or without 80SN/A * modification, are permitted provided that the following conditions 92362SN/A * are met: 100SN/A * 1. Redistributions of source code must retain the above copyright 110SN/A * notice, this list of conditions and the following disclaimer. 120SN/A * 2. Redistributions in binary form must reproduce the above copyright 130SN/A * notice, this list of conditions and the following disclaimer in the 140SN/A * documentation and/or other materials provided with the distribution. 150SN/A * 3. Neither the name of the University nor the names of its contributors 160SN/A * may be used to endorse or promote products derived from this software 170SN/A * without specific prior written permission. 180SN/A * 190SN/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 200SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 212362SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 222362SN/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 232362SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 240SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 250SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 260SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 270SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 280SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 290SN/A * SUCH DAMAGE. 300SN/A */ 310SN/A 320SN/A#include <err.h> 330SN/A#include <stdio.h> 340SN/A#include <stdlib.h> 350SN/A#include <string.h> 360SN/A#include <unistd.h> 370SN/A 380SN/Astatic int rm_path(char *); 390SN/Astatic void usage(void) __dead2; 400SN/A 410SN/Astatic int pflag; 420SN/Astatic int vflag; 438789SN/A 440SN/Aint 450SN/Amain(int argc, char *argv[]) 460SN/A{ 470SN/A int ch, errors; 480SN/A 490SN/A while ((ch = getopt(argc, argv, "pv")) != -1) 500SN/A switch(ch) { 510SN/A case 'p': 520SN/A pflag = 1; 530SN/A break; 540SN/A case 'v': 550SN/A vflag = 1; 560SN/A break; 570SN/A case '?': 580SN/A default: 590SN/A usage(); 600SN/A } 610SN/A argc -= optind; 620SN/A argv += optind; 630SN/A 640SN/A if (argc == 0) 650SN/A usage(); 660SN/A 670SN/A for (errors = 0; *argv; argv++) { 680SN/A if (rmdir(*argv) < 0) { 690SN/A warn("%s", *argv); 700SN/A errors = 1; 710SN/A } else { 720SN/A if (vflag) 730SN/A printf("%s\n", *argv); 740SN/A if (pflag) 750SN/A errors |= rm_path(*argv); 760SN/A } 770SN/A } 789914SN/A 790SN/A exit(errors); 800SN/A} 810SN/A 820SN/Astatic int 830SN/Arm_path(char *path) 840SN/A{ 850SN/A char *p; 860SN/A 870SN/A p = path + strlen(path); 880SN/A while (--p > path && *p == '/') 890SN/A ; 900SN/A *++p = '\0'; 910SN/A while ((p = strrchr(path, '/')) != NULL) { 928789SN/A /* Delete trailing slashes. */ 938789SN/A while (--p >= path && *p == '/') 940SN/A ; 950SN/A *++p = '\0'; 960SN/A if (p == path) 970SN/A break; 980SN/A 990SN/A if (rmdir(path) < 0) { 1000SN/A warn("%s", path); 1010SN/A return (1); 1020SN/A } 1030SN/A if (vflag) 1040SN/A printf("%s\n", path); 1050SN/A } 1060SN/A 1070SN/A return (0); 1080SN/A} 1090SN/A 1100SN/Astatic void 1110SN/Ausage(void) 1120SN/A{ 1130SN/A 1140SN/A (void)fprintf(stderr, "usage: rmdir [-pv] directory ...\n"); 1150SN/A exit(2); 1160SN/A} 1170SN/A