rmdir.c revision 1.14
11.14Schristos/*	$NetBSD: rmdir.c,v 1.14 1997/07/20 20:52:05 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.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.14Schristos__RCSID("$NetBSD: rmdir.c,v 1.14 1997/07/20 20:52:05 christos Exp $");
471.13Scgd#endif
481.1Scgd#endif /* not lint */
491.1Scgd
501.12Smycroft#include <err.h>
511.12Smycroft#include <errno.h>
521.1Scgd#include <stdio.h>
531.4Sjtc#include <stdlib.h>
541.4Sjtc#include <string.h>
551.10Sjtc#include <locale.h>
561.7Sjtc#include <unistd.h>
571.1Scgd
581.12Smycroftint rm_path __P((char *));
591.12Smycroftvoid usage __P((void));
601.14Schristosint main __P((int, char *[]));
611.7Sjtc
621.7Sjtcint
631.1Scgdmain(argc, argv)
641.1Scgd	int argc;
651.12Smycroft	char *argv[];
661.1Scgd{
671.12Smycroft	int ch, errors;
681.12Smycroft	int pflag;
691.10Sjtc
701.10Sjtc	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.4Sjtc	for (errors = 0; *argv; argv++) {
891.12Smycroft		char *p;
901.11Sjtc
911.12Smycroft		/* Delete trailing slashes, per POSIX. */
921.12Smycroft		p = *argv + strlen(*argv);
931.12Smycroft		while (--p > *argv && *p == '/')
941.12Smycroft			;
951.12Smycroft		*++p = '\0';
961.11Sjtc
971.12Smycroft		if (rmdir(*argv) < 0) {
981.12Smycroft			warn("%s", *argv);
991.11Sjtc			errors = 1;
1001.12Smycroft		} else if (pflag)
1011.12Smycroft			errors |= rm_path(*argv);
1021.4Sjtc	}
1031.4Sjtc
1041.1Scgd	exit(errors);
1051.4Sjtc}
1061.4Sjtc
1071.12Smycroftint
1081.12Smycroftrm_path(path)
1091.7Sjtc	char *path;
1101.4Sjtc{
1111.12Smycroft	char *p;
1121.4Sjtc
1131.12Smycroft	while ((p = strrchr(path, '/')) != NULL) {
1141.12Smycroft		/* Delete trailing slashes. */
1151.12Smycroft		while (--p > path && *p == '/')
1161.12Smycroft			;
1171.12Smycroft		*++p = '\0';
1181.12Smycroft
1191.12Smycroft		if (rmdir(path) < 0) {
1201.12Smycroft			warn("%s", path);
1211.12Smycroft			return (1);
1221.8Sjtc		}
1231.4Sjtc	}
1241.4Sjtc
1251.12Smycroft	return (0);
1261.4Sjtc}
1271.4Sjtc
1281.12Smycroftvoid
1291.4Sjtcusage()
1301.4Sjtc{
1311.12Smycroft
1321.12Smycroft	(void)fprintf(stderr, "usage: rmdir [-p] directory ...\n");
1331.4Sjtc	exit(1);
1341.1Scgd}
135