rmdir.c revision 1.13
11.13Scgd/*	$NetBSD: rmdir.c,v 1.13 1995/03/21 09:08:31 cgd 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.1Scgd#ifndef lint
371.12Smycroftstatic char copyright[] =
381.12Smycroft"@(#) Copyright (c) 1992, 1993, 1994\n\
391.12Smycroft	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.13Scgdstatic char rcsid[] = "$NetBSD: rmdir.c,v 1.13 1995/03/21 09:08:31 cgd 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.7Sjtc
611.7Sjtcint
621.1Scgdmain(argc, argv)
631.1Scgd	int argc;
641.12Smycroft	char *argv[];
651.1Scgd{
661.12Smycroft	int ch, errors;
671.12Smycroft	int pflag;
681.10Sjtc
691.10Sjtc	setlocale(LC_ALL, "");
701.1Scgd
711.12Smycroft	pflag = 0;
721.12Smycroft	while ((ch = getopt(argc, argv, "p")) != -1)
731.12Smycroft		switch(ch) {
741.4Sjtc		case 'p':
751.12Smycroft			pflag = 1;
761.4Sjtc			break;
771.4Sjtc		case '?':
781.4Sjtc		default:
791.4Sjtc			usage();
801.4Sjtc		}
811.12Smycroft	argc -= optind;
821.12Smycroft	argv += optind;
831.4Sjtc
841.12Smycroft	if (argc == 0)
851.12Smycroft		usage();
861.4Sjtc
871.4Sjtc	for (errors = 0; *argv; argv++) {
881.12Smycroft		char *p;
891.11Sjtc
901.12Smycroft		/* Delete trailing slashes, per POSIX. */
911.12Smycroft		p = *argv + strlen(*argv);
921.12Smycroft		while (--p > *argv && *p == '/')
931.12Smycroft			;
941.12Smycroft		*++p = '\0';
951.11Sjtc
961.12Smycroft		if (rmdir(*argv) < 0) {
971.12Smycroft			warn("%s", *argv);
981.11Sjtc			errors = 1;
991.12Smycroft		} else if (pflag)
1001.12Smycroft			errors |= rm_path(*argv);
1011.4Sjtc	}
1021.4Sjtc
1031.1Scgd	exit(errors);
1041.4Sjtc}
1051.4Sjtc
1061.12Smycroftint
1071.12Smycroftrm_path(path)
1081.7Sjtc	char *path;
1091.4Sjtc{
1101.12Smycroft	char *p;
1111.4Sjtc
1121.12Smycroft	while ((p = strrchr(path, '/')) != NULL) {
1131.12Smycroft		/* Delete trailing slashes. */
1141.12Smycroft		while (--p > path && *p == '/')
1151.12Smycroft			;
1161.12Smycroft		*++p = '\0';
1171.12Smycroft
1181.12Smycroft		if (rmdir(path) < 0) {
1191.12Smycroft			warn("%s", path);
1201.12Smycroft			return (1);
1211.8Sjtc		}
1221.4Sjtc	}
1231.4Sjtc
1241.12Smycroft	return (0);
1251.4Sjtc}
1261.4Sjtc
1271.12Smycroftvoid
1281.4Sjtcusage()
1291.4Sjtc{
1301.12Smycroft
1311.12Smycroft	(void)fprintf(stderr, "usage: rmdir [-p] directory ...\n");
1321.4Sjtc	exit(1);
1331.1Scgd}
134