rmdir.c revision 1.22
1/* $NetBSD: rmdir.c,v 1.22 2003/09/14 19:20:25 jschauma Exp $ */
2
3/*-
4 * Copyright (c) 1992, 1993, 1994
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#ifndef lint
34__COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\
35	The Regents of the University of California.  All rights reserved.\n");
36#endif /* not lint */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)rmdir.c	8.3 (Berkeley) 4/2/94";
41#else
42__RCSID("$NetBSD: rmdir.c,v 1.22 2003/09/14 19:20:25 jschauma Exp $");
43#endif
44#endif /* not lint */
45
46#include <sys/param.h>
47
48#include <err.h>
49#include <errno.h>
50#include <locale.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <unistd.h>
55#include <vis.h>
56
57int	stdout_ok;			/* stdout connected to a terminal */
58
59int	rm_path(char *);
60void	usage(void);
61int	main(int, char *[]);
62
63int
64main(int argc, char *argv[])
65{
66	int ch, errors, pflag;
67
68	setprogname(argv[0]);
69	(void)setlocale(LC_ALL, "");
70
71	pflag = 0;
72	while ((ch = getopt(argc, argv, "p")) != -1)
73		switch(ch) {
74		case 'p':
75			pflag = 1;
76			break;
77		case '?':
78		default:
79			usage();
80		}
81	argc -= optind;
82	argv += optind;
83
84	if (argc == 0)
85		usage();
86
87	stdout_ok = isatty(STDIN_FILENO);
88
89	for (errors = 0; *argv; argv++) {
90#ifdef notdef
91		char *p;
92
93		/* Kernel takes care of this */
94		/* Delete trailing slashes, per POSIX. */
95		p = *argv + strlen(*argv);
96		while (--p > *argv && *p == '/')
97			;
98		*++p = '\0';
99#endif
100
101		if (rmdir(*argv) < 0) {
102			warn("%s", *argv);
103			errors = 1;
104		} else if (pflag)
105			errors |= rm_path(*argv);
106	}
107
108	exit(errors);
109	/* NOTREACHED */
110}
111
112int
113rm_path(char *path)
114{
115	char *p;
116
117	while ((p = strrchr(path, '/')) != NULL) {
118		/* Delete trailing slashes. */
119		while (--p > path && *p == '/')
120			;
121		*++p = '\0';
122
123		if (rmdir(path) < 0) {
124			warn("%s", path);
125			return (1);
126		}
127	}
128
129	return (0);
130}
131
132void
133usage(void)
134{
135	(void)fprintf(stderr, "usage: %s [-p] directory ...\n", getprogname());
136	exit(1);
137	/* NOTREACHED */
138}
139