rmdir.c revision 1.18
11.18Sjschauma/* $NetBSD: rmdir.c,v 1.18 2003/08/04 22:31:25 jschauma Exp $ */ 21.13Scgd 31.12Smycroft/*- 41.12Smycroft * Copyright (c) 1992, 1993, 1994 51.12Smycroft * The Regents of the University of California. All rights reserved. 61.1Scgd * 71.1Scgd * Redistribution and use in source and binary forms, with or without 81.1Scgd * modification, are permitted provided that the following conditions 91.1Scgd * are met: 101.1Scgd * 1. Redistributions of source code must retain the above copyright 111.1Scgd * notice, this list of conditions and the following disclaimer. 121.1Scgd * 2. Redistributions in binary form must reproduce the above copyright 131.1Scgd * notice, this list of conditions and the following disclaimer in the 141.1Scgd * documentation and/or other materials provided with the distribution. 151.1Scgd * 3. All advertising materials mentioning features or use of this software 161.1Scgd * must display the following acknowledgement: 171.1Scgd * This product includes software developed by the University of 181.1Scgd * California, Berkeley and its contributors. 191.1Scgd * 4. Neither the name of the University nor the names of its contributors 201.1Scgd * may be used to endorse or promote products derived from this software 211.1Scgd * without specific prior written permission. 221.1Scgd * 231.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 241.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 251.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 261.1Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 271.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 281.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 291.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 301.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 311.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 321.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 331.1Scgd * SUCH DAMAGE. 341.1Scgd */ 351.1Scgd 361.14Schristos#include <sys/cdefs.h> 371.1Scgd#ifndef lint 381.14Schristos__COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\ 391.14Schristos The Regents of the University of California. All rights reserved.\n"); 401.1Scgd#endif /* not lint */ 411.1Scgd 421.1Scgd#ifndef lint 431.13Scgd#if 0 441.13Scgdstatic char sccsid[] = "@(#)rmdir.c 8.3 (Berkeley) 4/2/94"; 451.13Scgd#else 461.18Sjschauma__RCSID("$NetBSD: rmdir.c,v 1.18 2003/08/04 22:31:25 jschauma Exp $"); 471.13Scgd#endif 481.1Scgd#endif /* not lint */ 491.1Scgd 501.18Sjschauma#include <sys/param.h> 511.18Sjschauma 521.12Smycroft#include <err.h> 531.12Smycroft#include <errno.h> 541.17Swiz#include <locale.h> 551.1Scgd#include <stdio.h> 561.4Sjtc#include <stdlib.h> 571.4Sjtc#include <string.h> 581.7Sjtc#include <unistd.h> 591.18Sjschauma#include <vis.h> 601.18Sjschauma 611.18Sjschaumaint stdout_ok; /* stdout connected to a terminal */ 621.1Scgd 631.18Sjschaumaint rm_path(char *); 641.18Sjschaumavoid usage(void); 651.18Sjschaumaint main(int, char *[]); 661.18Sjschaumachar *printescaped(const char *); 671.7Sjtc 681.7Sjtcint 691.17Swizmain(int argc, char *argv[]) 701.1Scgd{ 711.17Swiz int ch, errors, pflag; 721.10Sjtc 731.17Swiz setprogname(argv[0]); 741.15Scgd (void)setlocale(LC_ALL, ""); 751.1Scgd 761.12Smycroft pflag = 0; 771.12Smycroft while ((ch = getopt(argc, argv, "p")) != -1) 781.12Smycroft switch(ch) { 791.4Sjtc case 'p': 801.12Smycroft pflag = 1; 811.4Sjtc break; 821.4Sjtc case '?': 831.4Sjtc default: 841.4Sjtc usage(); 851.4Sjtc } 861.12Smycroft argc -= optind; 871.12Smycroft argv += optind; 881.4Sjtc 891.12Smycroft if (argc == 0) 901.12Smycroft usage(); 911.4Sjtc 921.18Sjschauma stdout_ok = isatty(STDIN_FILENO); 931.18Sjschauma 941.4Sjtc for (errors = 0; *argv; argv++) { 951.12Smycroft char *p; 961.11Sjtc 971.12Smycroft /* Delete trailing slashes, per POSIX. */ 981.12Smycroft p = *argv + strlen(*argv); 991.12Smycroft while (--p > *argv && *p == '/') 1001.12Smycroft ; 1011.12Smycroft *++p = '\0'; 1021.11Sjtc 1031.12Smycroft if (rmdir(*argv) < 0) { 1041.18Sjschauma char *dn; 1051.18Sjschauma dn = printescaped(*argv); 1061.18Sjschauma warn("%s", dn); 1071.18Sjschauma free(dn); 1081.11Sjtc errors = 1; 1091.12Smycroft } else if (pflag) 1101.12Smycroft errors |= rm_path(*argv); 1111.4Sjtc } 1121.4Sjtc 1131.1Scgd exit(errors); 1141.15Scgd /* NOTREACHED */ 1151.4Sjtc} 1161.4Sjtc 1171.12Smycroftint 1181.17Swizrm_path(char *path) 1191.4Sjtc{ 1201.12Smycroft char *p; 1211.4Sjtc 1221.12Smycroft while ((p = strrchr(path, '/')) != NULL) { 1231.12Smycroft /* Delete trailing slashes. */ 1241.12Smycroft while (--p > path && *p == '/') 1251.12Smycroft ; 1261.12Smycroft *++p = '\0'; 1271.12Smycroft 1281.12Smycroft if (rmdir(path) < 0) { 1291.18Sjschauma char *pn; 1301.18Sjschauma pn = printescaped(path); 1311.18Sjschauma warn("%s", pn); 1321.18Sjschauma free(pn); 1331.12Smycroft return (1); 1341.8Sjtc } 1351.4Sjtc } 1361.4Sjtc 1371.12Smycroft return (0); 1381.4Sjtc} 1391.4Sjtc 1401.12Smycroftvoid 1411.17Swizusage(void) 1421.4Sjtc{ 1431.17Swiz (void)fprintf(stderr, "usage: %s [-p] directory ...\n", getprogname()); 1441.4Sjtc exit(1); 1451.16Smycroft /* NOTREACHED */ 1461.18Sjschauma} 1471.18Sjschauma 1481.18Sjschaumachar * 1491.18Sjschaumaprintescaped(const char *src) 1501.18Sjschauma{ 1511.18Sjschauma size_t len; 1521.18Sjschauma char *retval; 1531.18Sjschauma 1541.18Sjschauma len = strlen(src); 1551.18Sjschauma if (len != 0 && SIZE_T_MAX/len <= 4) { 1561.18Sjschauma errx(EXIT_FAILURE, "%s: name too long", src); 1571.18Sjschauma /* NOTREACHED */ 1581.18Sjschauma } 1591.18Sjschauma 1601.18Sjschauma retval = (char *)malloc(4*len+1); 1611.18Sjschauma if (retval != NULL) { 1621.18Sjschauma if (stdout_ok) 1631.18Sjschauma (void)strvis(retval, src, VIS_NL | VIS_CSTYLE); 1641.18Sjschauma else 1651.18Sjschauma (void)strcpy(retval, src); 1661.18Sjschauma return retval; 1671.18Sjschauma } else 1681.18Sjschauma errx(EXIT_FAILURE, "out of memory!"); 1691.18Sjschauma /* NOTREACHED */ 1701.1Scgd} 171