rmdir.c revision 1.21
11.21Schristos/* $NetBSD: rmdir.c,v 1.21 2003/09/11 17:38:23 christos 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.19Sagc * 3. Neither the name of the University nor the names of its contributors 161.1Scgd * may be used to endorse or promote products derived from this software 171.1Scgd * without specific prior written permission. 181.1Scgd * 191.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 201.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 211.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 221.1Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 231.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 241.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 251.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 261.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 271.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 281.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 291.1Scgd * SUCH DAMAGE. 301.1Scgd */ 311.1Scgd 321.14Schristos#include <sys/cdefs.h> 331.1Scgd#ifndef lint 341.14Schristos__COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\ 351.14Schristos The Regents of the University of California. All rights reserved.\n"); 361.1Scgd#endif /* not lint */ 371.1Scgd 381.1Scgd#ifndef lint 391.13Scgd#if 0 401.13Scgdstatic char sccsid[] = "@(#)rmdir.c 8.3 (Berkeley) 4/2/94"; 411.13Scgd#else 421.21Schristos__RCSID("$NetBSD: rmdir.c,v 1.21 2003/09/11 17:38:23 christos Exp $"); 431.13Scgd#endif 441.1Scgd#endif /* not lint */ 451.1Scgd 461.18Sjschauma#include <sys/param.h> 471.18Sjschauma 481.12Smycroft#include <err.h> 491.12Smycroft#include <errno.h> 501.17Swiz#include <locale.h> 511.1Scgd#include <stdio.h> 521.4Sjtc#include <stdlib.h> 531.4Sjtc#include <string.h> 541.7Sjtc#include <unistd.h> 551.18Sjschauma#include <vis.h> 561.18Sjschauma 571.18Sjschaumaint stdout_ok; /* stdout connected to a terminal */ 581.1Scgd 591.18Sjschaumaint rm_path(char *); 601.18Sjschaumavoid usage(void); 611.18Sjschaumaint main(int, char *[]); 621.18Sjschaumachar *printescaped(const char *); 631.7Sjtc 641.7Sjtcint 651.17Swizmain(int argc, char *argv[]) 661.1Scgd{ 671.17Swiz int ch, errors, pflag; 681.10Sjtc 691.17Swiz setprogname(argv[0]); 701.15Scgd (void)setlocale(LC_ALL, ""); 711.1Scgd 721.12Smycroft pflag = 0; 731.12Smycroft while ((ch = getopt(argc, argv, "p")) != -1) 741.12Smycroft switch(ch) { 751.4Sjtc case 'p': 761.12Smycroft pflag = 1; 771.4Sjtc break; 781.4Sjtc case '?': 791.4Sjtc default: 801.4Sjtc usage(); 811.4Sjtc } 821.12Smycroft argc -= optind; 831.12Smycroft argv += optind; 841.4Sjtc 851.12Smycroft if (argc == 0) 861.12Smycroft usage(); 871.4Sjtc 881.18Sjschauma stdout_ok = isatty(STDIN_FILENO); 891.18Sjschauma 901.4Sjtc for (errors = 0; *argv; argv++) { 911.21Schristos#ifdef notdef 921.12Smycroft char *p; 931.11Sjtc 941.21Schristos /* Kernel takes care of this */ 951.12Smycroft /* Delete trailing slashes, per POSIX. */ 961.12Smycroft p = *argv + strlen(*argv); 971.12Smycroft while (--p > *argv && *p == '/') 981.12Smycroft ; 991.12Smycroft *++p = '\0'; 1001.21Schristos#endif 1011.11Sjtc 1021.12Smycroft if (rmdir(*argv) < 0) { 1031.18Sjschauma char *dn; 1041.18Sjschauma dn = printescaped(*argv); 1051.18Sjschauma warn("%s", dn); 1061.18Sjschauma free(dn); 1071.11Sjtc errors = 1; 1081.12Smycroft } else if (pflag) 1091.12Smycroft errors |= rm_path(*argv); 1101.4Sjtc } 1111.4Sjtc 1121.1Scgd exit(errors); 1131.15Scgd /* NOTREACHED */ 1141.4Sjtc} 1151.4Sjtc 1161.12Smycroftint 1171.17Swizrm_path(char *path) 1181.4Sjtc{ 1191.12Smycroft char *p; 1201.4Sjtc 1211.12Smycroft while ((p = strrchr(path, '/')) != NULL) { 1221.12Smycroft /* Delete trailing slashes. */ 1231.12Smycroft while (--p > path && *p == '/') 1241.12Smycroft ; 1251.12Smycroft *++p = '\0'; 1261.12Smycroft 1271.12Smycroft if (rmdir(path) < 0) { 1281.18Sjschauma char *pn; 1291.18Sjschauma pn = printescaped(path); 1301.18Sjschauma warn("%s", pn); 1311.18Sjschauma free(pn); 1321.12Smycroft return (1); 1331.8Sjtc } 1341.4Sjtc } 1351.4Sjtc 1361.12Smycroft return (0); 1371.4Sjtc} 1381.4Sjtc 1391.12Smycroftvoid 1401.17Swizusage(void) 1411.4Sjtc{ 1421.17Swiz (void)fprintf(stderr, "usage: %s [-p] directory ...\n", getprogname()); 1431.4Sjtc exit(1); 1441.16Smycroft /* NOTREACHED */ 1451.18Sjschauma} 1461.18Sjschauma 1471.18Sjschaumachar * 1481.18Sjschaumaprintescaped(const char *src) 1491.18Sjschauma{ 1501.18Sjschauma size_t len; 1511.18Sjschauma char *retval; 1521.18Sjschauma 1531.18Sjschauma len = strlen(src); 1541.18Sjschauma if (len != 0 && SIZE_T_MAX/len <= 4) { 1551.18Sjschauma errx(EXIT_FAILURE, "%s: name too long", src); 1561.18Sjschauma /* NOTREACHED */ 1571.18Sjschauma } 1581.18Sjschauma 1591.18Sjschauma retval = (char *)malloc(4*len+1); 1601.18Sjschauma if (retval != NULL) { 1611.18Sjschauma if (stdout_ok) 1621.18Sjschauma (void)strvis(retval, src, VIS_NL | VIS_CSTYLE); 1631.18Sjschauma else 1641.20Sitojun (void)strlcpy(retval, src, 4 * len + 1); 1651.18Sjschauma return retval; 1661.18Sjschauma } else 1671.18Sjschauma errx(EXIT_FAILURE, "out of memory!"); 1681.18Sjschauma /* NOTREACHED */ 1691.1Scgd} 170