rmdir.c revision 1.12
11.12Smycroft/*-
21.12Smycroft * Copyright (c) 1992, 1993, 1994
31.12Smycroft *	The Regents of the University of California.  All rights reserved.
41.1Scgd *
51.1Scgd * Redistribution and use in source and binary forms, with or without
61.1Scgd * modification, are permitted provided that the following conditions
71.1Scgd * are met:
81.1Scgd * 1. Redistributions of source code must retain the above copyright
91.1Scgd *    notice, this list of conditions and the following disclaimer.
101.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
111.1Scgd *    notice, this list of conditions and the following disclaimer in the
121.1Scgd *    documentation and/or other materials provided with the distribution.
131.1Scgd * 3. All advertising materials mentioning features or use of this software
141.1Scgd *    must display the following acknowledgement:
151.1Scgd *	This product includes software developed by the University of
161.1Scgd *	California, Berkeley and its contributors.
171.1Scgd * 4. Neither the name of the University nor the names of its contributors
181.1Scgd *    may be used to endorse or promote products derived from this software
191.1Scgd *    without specific prior written permission.
201.1Scgd *
211.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311.1Scgd * SUCH DAMAGE.
321.1Scgd */
331.1Scgd
341.1Scgd#ifndef lint
351.12Smycroftstatic char copyright[] =
361.12Smycroft"@(#) Copyright (c) 1992, 1993, 1994\n\
371.12Smycroft	The Regents of the University of California.  All rights reserved.\n";
381.1Scgd#endif /* not lint */
391.1Scgd
401.1Scgd#ifndef lint
411.12Smycroft/*static char sccsid[] = "from: @(#)rmdir.c	8.3 (Berkeley) 4/2/94";*/
421.12Smycroftstatic char *rcsid = "$Id: rmdir.c,v 1.12 1994/09/22 09:25:59 mycroft Exp $";
431.1Scgd#endif /* not lint */
441.1Scgd
451.12Smycroft#include <err.h>
461.12Smycroft#include <errno.h>
471.1Scgd#include <stdio.h>
481.4Sjtc#include <stdlib.h>
491.4Sjtc#include <string.h>
501.10Sjtc#include <locale.h>
511.7Sjtc#include <unistd.h>
521.1Scgd
531.12Smycroftint rm_path __P((char *));
541.12Smycroftvoid usage __P((void));
551.7Sjtc
561.7Sjtcint
571.1Scgdmain(argc, argv)
581.1Scgd	int argc;
591.12Smycroft	char *argv[];
601.1Scgd{
611.12Smycroft	int ch, errors;
621.12Smycroft	int pflag;
631.10Sjtc
641.10Sjtc	setlocale(LC_ALL, "");
651.1Scgd
661.12Smycroft	pflag = 0;
671.12Smycroft	while ((ch = getopt(argc, argv, "p")) != -1)
681.12Smycroft		switch(ch) {
691.4Sjtc		case 'p':
701.12Smycroft			pflag = 1;
711.4Sjtc			break;
721.4Sjtc		case '?':
731.4Sjtc		default:
741.4Sjtc			usage();
751.4Sjtc		}
761.12Smycroft	argc -= optind;
771.12Smycroft	argv += optind;
781.4Sjtc
791.12Smycroft	if (argc == 0)
801.12Smycroft		usage();
811.4Sjtc
821.4Sjtc	for (errors = 0; *argv; argv++) {
831.12Smycroft		char *p;
841.11Sjtc
851.12Smycroft		/* Delete trailing slashes, per POSIX. */
861.12Smycroft		p = *argv + strlen(*argv);
871.12Smycroft		while (--p > *argv && *p == '/')
881.12Smycroft			;
891.12Smycroft		*++p = '\0';
901.11Sjtc
911.12Smycroft		if (rmdir(*argv) < 0) {
921.12Smycroft			warn("%s", *argv);
931.11Sjtc			errors = 1;
941.12Smycroft		} else if (pflag)
951.12Smycroft			errors |= rm_path(*argv);
961.4Sjtc	}
971.4Sjtc
981.1Scgd	exit(errors);
991.4Sjtc}
1001.4Sjtc
1011.12Smycroftint
1021.12Smycroftrm_path(path)
1031.7Sjtc	char *path;
1041.4Sjtc{
1051.12Smycroft	char *p;
1061.4Sjtc
1071.12Smycroft	while ((p = strrchr(path, '/')) != NULL) {
1081.12Smycroft		/* Delete trailing slashes. */
1091.12Smycroft		while (--p > path && *p == '/')
1101.12Smycroft			;
1111.12Smycroft		*++p = '\0';
1121.12Smycroft
1131.12Smycroft		if (rmdir(path) < 0) {
1141.12Smycroft			warn("%s", path);
1151.12Smycroft			return (1);
1161.8Sjtc		}
1171.4Sjtc	}
1181.4Sjtc
1191.12Smycroft	return (0);
1201.4Sjtc}
1211.4Sjtc
1221.12Smycroftvoid
1231.4Sjtcusage()
1241.4Sjtc{
1251.12Smycroft
1261.12Smycroft	(void)fprintf(stderr, "usage: rmdir [-p] directory ...\n");
1271.4Sjtc	exit(1);
1281.1Scgd}
129