11.28Skim/* $NetBSD: rmdir.c,v 1.28 2025/05/12 06:21:56 kim 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.19Sagc * 3. Neither the name of the University nor the names of its contributors
161.1Scgd *    may be used to endorse or promote products derived from this software
171.1Scgd *    without specific prior written permission.
181.1Scgd *
191.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291.1Scgd * SUCH DAMAGE.
301.1Scgd */
311.1Scgd
321.14Schristos#include <sys/cdefs.h>
331.1Scgd#ifndef lint
341.25Slukem__COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
351.25Slukem The Regents of the University of California.  All rights reserved.");
361.1Scgd#endif /* not lint */
371.1Scgd
381.1Scgd#ifndef lint
391.13Scgd#if 0
401.13Scgdstatic char sccsid[] = "@(#)rmdir.c	8.3 (Berkeley) 4/2/94";
411.13Scgd#else
421.28Skim__RCSID("$NetBSD: rmdir.c,v 1.28 2025/05/12 06:21:56 kim Exp $");
431.13Scgd#endif
441.1Scgd#endif /* not lint */
451.1Scgd
461.18Sjschauma#include <sys/param.h>
471.18Sjschauma
481.12Smycroft#include <err.h>
491.17Swiz#include <locale.h>
501.1Scgd#include <stdio.h>
511.4Sjtc#include <stdlib.h>
521.4Sjtc#include <string.h>
531.7Sjtc#include <unistd.h>
541.1Scgd
551.28Skimstatic int vflag;
561.28Skim
571.26Sjoergstatic int	rm_path(char *);
581.26Sjoerg__dead static void	usage(void);
591.7Sjtc
601.7Sjtcint
611.17Swizmain(int argc, char *argv[])
621.1Scgd{
631.17Swiz	int ch, errors, pflag;
641.10Sjtc
651.17Swiz	setprogname(argv[0]);
661.15Scgd	(void)setlocale(LC_ALL, "");
671.1Scgd
681.12Smycroft	pflag = 0;
691.28Skim	while ((ch = getopt(argc, argv, "pv")) != -1)
701.12Smycroft		switch(ch) {
711.4Sjtc		case 'p':
721.12Smycroft			pflag = 1;
731.4Sjtc			break;
741.28Skim		case 'v':
751.28Skim			vflag = 1;
761.28Skim			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.23Sdsl		/* We rely on the kernel to ignore trailing '/' characters. */
891.12Smycroft		if (rmdir(*argv) < 0) {
901.22Sjschauma			warn("%s", *argv);
911.11Sjtc			errors = 1;
921.28Skim		} else {
931.28Skim			if (vflag)
941.28Skim				(void)printf("%s\n", *argv);
951.28Skim			if (pflag)
961.28Skim				errors |= rm_path(*argv);
971.28Skim		}
981.4Sjtc	}
991.4Sjtc
1001.1Scgd	exit(errors);
1011.15Scgd	/* NOTREACHED */
1021.4Sjtc}
1031.4Sjtc
1041.26Sjoergstatic int
1051.17Swizrm_path(char *path)
1061.4Sjtc{
1071.12Smycroft	char *p;
1081.4Sjtc
1091.12Smycroft	while ((p = strrchr(path, '/')) != NULL) {
1101.23Sdsl		*p = 0;
1111.23Sdsl		if (p[1] == 0)
1121.23Sdsl			/* Ignore trailing '/' on deleted name */
1131.23Sdsl			continue;
1141.12Smycroft
1151.27Sginsbach		if (*path == 0)
1161.27Sginsbach			/* At top level (root) directory */
1171.27Sginsbach			break;
1181.27Sginsbach
1191.12Smycroft		if (rmdir(path) < 0) {
1201.22Sjschauma			warn("%s", path);
1211.12Smycroft			return (1);
1221.28Skim		} else if (vflag)
1231.28Skim			(void)printf("%s\n", path);
1241.4Sjtc	}
1251.4Sjtc
1261.12Smycroft	return (0);
1271.4Sjtc}
1281.4Sjtc
1291.26Sjoergstatic void
1301.17Swizusage(void)
1311.4Sjtc{
1321.28Skim	(void)fprintf(stderr, "usage: %s [-pv] directory ...\n", getprogname());
1331.4Sjtc	exit(1);
1341.16Smycroft	/* NOTREACHED */
1351.1Scgd}
136