rmdir.c revision 1.17
11.17Swiz/* $NetBSD: rmdir.c,v 1.17 2001/09/16 21:21:14 wiz 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.17Swiz__RCSID("$NetBSD: rmdir.c,v 1.17 2001/09/16 21:21:14 wiz Exp $");
471.13Scgd#endif
481.1Scgd#endif /* not lint */
491.1Scgd
501.12Smycroft#include <err.h>
511.12Smycroft#include <errno.h>
521.17Swiz#include <locale.h>
531.1Scgd#include <stdio.h>
541.4Sjtc#include <stdlib.h>
551.4Sjtc#include <string.h>
561.7Sjtc#include <unistd.h>
571.1Scgd
581.17Swizint rm_path(char *);
591.17Swizvoid usage(void);
601.17Swizint main(int, char *[]);
611.7Sjtc
621.7Sjtcint
631.17Swizmain(int argc, char *argv[])
641.1Scgd{
651.17Swiz	int ch, errors, pflag;
661.10Sjtc
671.17Swiz	setprogname(argv[0]);
681.15Scgd	(void)setlocale(LC_ALL, "");
691.1Scgd
701.12Smycroft	pflag = 0;
711.12Smycroft	while ((ch = getopt(argc, argv, "p")) != -1)
721.12Smycroft		switch(ch) {
731.4Sjtc		case 'p':
741.12Smycroft			pflag = 1;
751.4Sjtc			break;
761.4Sjtc		case '?':
771.4Sjtc		default:
781.4Sjtc			usage();
791.4Sjtc		}
801.12Smycroft	argc -= optind;
811.12Smycroft	argv += optind;
821.4Sjtc
831.12Smycroft	if (argc == 0)
841.12Smycroft		usage();
851.4Sjtc
861.4Sjtc	for (errors = 0; *argv; argv++) {
871.12Smycroft		char *p;
881.11Sjtc
891.12Smycroft		/* Delete trailing slashes, per POSIX. */
901.12Smycroft		p = *argv + strlen(*argv);
911.12Smycroft		while (--p > *argv && *p == '/')
921.12Smycroft			;
931.12Smycroft		*++p = '\0';
941.11Sjtc
951.12Smycroft		if (rmdir(*argv) < 0) {
961.12Smycroft			warn("%s", *argv);
971.11Sjtc			errors = 1;
981.12Smycroft		} else if (pflag)
991.12Smycroft			errors |= rm_path(*argv);
1001.4Sjtc	}
1011.4Sjtc
1021.1Scgd	exit(errors);
1031.15Scgd	/* NOTREACHED */
1041.4Sjtc}
1051.4Sjtc
1061.12Smycroftint
1071.17Swizrm_path(char *path)
1081.4Sjtc{
1091.12Smycroft	char *p;
1101.4Sjtc
1111.12Smycroft	while ((p = strrchr(path, '/')) != NULL) {
1121.12Smycroft		/* Delete trailing slashes. */
1131.12Smycroft		while (--p > path && *p == '/')
1141.12Smycroft			;
1151.12Smycroft		*++p = '\0';
1161.12Smycroft
1171.12Smycroft		if (rmdir(path) < 0) {
1181.12Smycroft			warn("%s", path);
1191.12Smycroft			return (1);
1201.8Sjtc		}
1211.4Sjtc	}
1221.4Sjtc
1231.12Smycroft	return (0);
1241.4Sjtc}
1251.4Sjtc
1261.12Smycroftvoid
1271.17Swizusage(void)
1281.4Sjtc{
1291.17Swiz	(void)fprintf(stderr, "usage: %s [-p] directory ...\n", getprogname());
1301.4Sjtc	exit(1);
1311.16Smycroft	/* NOTREACHED */
1321.1Scgd}
133