11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1992, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 4. Neither the name of the University nor the names of its contributors
141556Srgrimes *    may be used to endorse or promote products derived from this software
151556Srgrimes *    without specific prior written permission.
161556Srgrimes *
171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271556Srgrimes * SUCH DAMAGE.
281556Srgrimes */
291556Srgrimes
30114433Sobrien#if 0
311556Srgrimes#ifndef lint
3220422Sstevestatic char const copyright[] =
331556Srgrimes"@(#) Copyright (c) 1992, 1993, 1994\n\
341556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
351556Srgrimes#endif /* not lint */
361556Srgrimes
371556Srgrimes#ifndef lint
3836149Scharnierstatic char sccsid[] = "@(#)rmdir.c	8.3 (Berkeley) 4/2/94";
39114433Sobrien#endif /* not lint */
4036149Scharnier#endif
4199110Sobrien#include <sys/cdefs.h>
4299110Sobrien__FBSDID("$FreeBSD$");
431556Srgrimes
441556Srgrimes#include <err.h>
451556Srgrimes#include <stdio.h>
461556Srgrimes#include <stdlib.h>
471556Srgrimes#include <string.h>
481556Srgrimes#include <unistd.h>
491556Srgrimes
50127257Sdesstatic int rm_path(char *);
51127257Sdesstatic void usage(void);
521556Srgrimes
53127257Sdesstatic int pflag;
54127257Sdesstatic int vflag;
55127257Sdes
561556Srgrimesint
5790110Simpmain(int argc, char *argv[])
581556Srgrimes{
591556Srgrimes	int ch, errors;
601556Srgrimes
61127257Sdes	while ((ch = getopt(argc, argv, "pv")) != -1)
621556Srgrimes		switch(ch) {
6320422Ssteve		case 'p':
6420422Ssteve			pflag = 1;
6520422Ssteve			break;
66127257Sdes		case 'v':
67127257Sdes			vflag = 1;
68127257Sdes			break;
691556Srgrimes		case '?':
701556Srgrimes		default:
711556Srgrimes			usage();
721556Srgrimes		}
731556Srgrimes	argc -= optind;
741556Srgrimes	argv += optind;
751556Srgrimes
761556Srgrimes	if (argc == 0)
771556Srgrimes		usage();
781556Srgrimes
7920422Ssteve	for (errors = 0; *argv; argv++) {
80137901Sru		if (rmdir(*argv) < 0) {
81137901Sru			warn("%s", *argv);
82137901Sru			errors = 1;
83127257Sdes		} else {
84127257Sdes			if (vflag)
85127257Sdes				printf("%s\n", *argv);
86137901Sru			if (pflag)
87137901Sru				errors |= rm_path(*argv);
88127257Sdes		}
8920422Ssteve	}
9020422Ssteve
911556Srgrimes	exit(errors);
921556Srgrimes}
931556Srgrimes
94127257Sdesstatic int
9590110Simprm_path(char *path)
9620422Ssteve{
9720422Ssteve	char *p;
9820422Ssteve
9962213Snbm	p = path + strlen(path);
10062213Snbm	while (--p > path && *p == '/')
10162213Snbm		;
10262213Snbm	*++p = '\0';
10320422Ssteve	while ((p = strrchr(path, '/')) != NULL) {
10420422Ssteve		/* Delete trailing slashes. */
105137902Sru		while (--p >= path && *p == '/')
10620422Ssteve			;
10720422Ssteve		*++p = '\0';
108137902Sru		if (p == path)
109137902Sru			break;
11020422Ssteve
11120422Ssteve		if (rmdir(path) < 0) {
11220422Ssteve			warn("%s", path);
11320422Ssteve			return (1);
11420422Ssteve		}
115127257Sdes		if (vflag)
116127257Sdes			printf("%s\n", path);
11720422Ssteve	}
11820422Ssteve
11920422Ssteve	return (0);
12020422Ssteve}
12120422Ssteve
122127257Sdesstatic void
12390110Simpusage(void)
1241556Srgrimes{
1251556Srgrimes
126129285Sru	(void)fprintf(stderr, "usage: rmdir [-pv] directory ...\n");
1271556Srgrimes	exit(1);
1281556Srgrimes}
129