rmdir.c revision 137902
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: head/bin/rmdir/rmdir.c 137902 2004-11-20 00:41:08Z ru $");
431556Srgrimes
441556Srgrimes#include <err.h>
451556Srgrimes#include <errno.h>
461556Srgrimes#include <stdio.h>
471556Srgrimes#include <stdlib.h>
481556Srgrimes#include <string.h>
491556Srgrimes#include <unistd.h>
501556Srgrimes
51127257Sdesstatic int rm_path(char *);
52127257Sdesstatic void usage(void);
531556Srgrimes
54127257Sdesstatic int pflag;
55127257Sdesstatic int vflag;
56127257Sdes
571556Srgrimesint
5890110Simpmain(int argc, char *argv[])
591556Srgrimes{
601556Srgrimes	int ch, errors;
611556Srgrimes
62127257Sdes	while ((ch = getopt(argc, argv, "pv")) != -1)
631556Srgrimes		switch(ch) {
6420422Ssteve		case 'p':
6520422Ssteve			pflag = 1;
6620422Ssteve			break;
67127257Sdes		case 'v':
68127257Sdes			vflag = 1;
69127257Sdes			break;
701556Srgrimes		case '?':
711556Srgrimes		default:
721556Srgrimes			usage();
731556Srgrimes		}
741556Srgrimes	argc -= optind;
751556Srgrimes	argv += optind;
761556Srgrimes
771556Srgrimes	if (argc == 0)
781556Srgrimes		usage();
791556Srgrimes
8020422Ssteve	for (errors = 0; *argv; argv++) {
81137901Sru		if (rmdir(*argv) < 0) {
82137901Sru			warn("%s", *argv);
83137901Sru			errors = 1;
84127257Sdes		} else {
85127257Sdes			if (vflag)
86127257Sdes				printf("%s\n", *argv);
87137901Sru			if (pflag)
88137901Sru				errors |= rm_path(*argv);
89127257Sdes		}
9020422Ssteve	}
9120422Ssteve
921556Srgrimes	exit(errors);
931556Srgrimes}
941556Srgrimes
95127257Sdesstatic int
9690110Simprm_path(char *path)
9720422Ssteve{
9820422Ssteve	char *p;
9920422Ssteve
10062213Snbm	p = path + strlen(path);
10162213Snbm	while (--p > path && *p == '/')
10262213Snbm		;
10362213Snbm	*++p = '\0';
10420422Ssteve	while ((p = strrchr(path, '/')) != NULL) {
10520422Ssteve		/* Delete trailing slashes. */
106137902Sru		while (--p >= path && *p == '/')
10720422Ssteve			;
10820422Ssteve		*++p = '\0';
109137902Sru		if (p == path)
110137902Sru			break;
11120422Ssteve
11220422Ssteve		if (rmdir(path) < 0) {
11320422Ssteve			warn("%s", path);
11420422Ssteve			return (1);
11520422Ssteve		}
116127257Sdes		if (vflag)
117127257Sdes			printf("%s\n", path);
11820422Ssteve	}
11920422Ssteve
12020422Ssteve	return (0);
12120422Ssteve}
12220422Ssteve
123127257Sdesstatic void
12490110Simpusage(void)
1251556Srgrimes{
1261556Srgrimes
127129285Sru	(void)fprintf(stderr, "usage: rmdir [-pv] directory ...\n");
1281556Srgrimes	exit(1);
1291556Srgrimes}
130