blob: 6d92b5daabf15d1590125a239653dd8d4210380e [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenbed30e91999-10-18 19:02:32 +00002/*
3 * Mini rm implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00006 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Eric Andersenc4996011999-10-20 22:08:37 +00007 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersenbed30e91999-10-18 19:02:32 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
Eric Andersenbed30e91999-10-18 19:02:32 +000025#include <stdio.h>
26#include <time.h>
27#include <utime.h>
28#include <dirent.h>
Eric Andersena9c95ea1999-11-15 17:33:30 +000029#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +000030#include <unistd.h>
31#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000032#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000033
Eric Andersenbed30e91999-10-18 19:02:32 +000034static int recursiveFlag = FALSE;
35static int forceFlag = FALSE;
Mark Whitleye0bf91d2001-03-13 00:40:19 +000036#ifdef BB_FEATURE_RM_INTERACTIVE
37 static int interactiveFlag = FALSE;
38#endif
Eric Andersenbed30e91999-10-18 19:02:32 +000039static const char *srcName;
40
41
Erik Andersen3364d782000-03-28 00:58:14 +000042static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
Eric Andersencc8ed391999-10-05 16:24:54 +000043{
Mark Whitleye0bf91d2001-03-13 00:40:19 +000044#ifdef BB_FEATURE_RM_INTERACTIVE
45 if (interactiveFlag == TRUE) {
46 printf("rm: remove `%s'? ", fileName);
47 if (ask_confirmation() == 0)
48 return (TRUE);
49 }
50#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 if (unlink(fileName) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000052 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 return (FALSE);
54 }
55 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000056}
57
Erik Andersen3364d782000-03-28 00:58:14 +000058static int dirAction(const char *fileName, struct stat *statbuf, void* junk)
Eric Andersencc8ed391999-10-05 16:24:54 +000059{
Matt Kraai7c22b772000-09-20 23:10:21 +000060 if (recursiveFlag == FALSE) {
61 errno = EISDIR;
Mark Whitleyf57c9442000-12-07 19:56:48 +000062 perror_msg("%s", fileName);
Matt Kraai7c22b772000-09-20 23:10:21 +000063 return (FALSE);
64 }
Mark Whitleye0bf91d2001-03-13 00:40:19 +000065#ifdef BB_FEATURE_RM_INTERACTIVE
66 if (interactiveFlag == TRUE) {
67 printf("rm: remove directory `%s'? ", fileName);
68 if (ask_confirmation() == 0)
69 return (TRUE);
70 }
71#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 if (rmdir(fileName) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000073 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 return (FALSE);
75 }
76 return (TRUE);
Eric Andersenbed30e91999-10-18 19:02:32 +000077}
78
79extern int rm_main(int argc, char **argv)
80{
Matt Kraaid27753a2000-12-05 05:11:41 +000081 int status = EXIT_SUCCESS;
Eric Andersen815e9042000-06-06 16:15:23 +000082 int stopIt=FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 struct stat statbuf;
Eric Andersenbed30e91999-10-18 19:02:32 +000084
Pavel Roskine97da402000-06-14 17:39:41 +000085 argc--;
Eric Andersenbed30e91999-10-18 19:02:32 +000086 argv++;
Eric Andersenbed30e91999-10-18 19:02:32 +000087
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 /* Parse any options */
Pavel Roskine97da402000-06-14 17:39:41 +000089 while (argc > 0 && stopIt == FALSE) {
90 if (**argv == '-') {
Eric Andersen815e9042000-06-06 16:15:23 +000091 while (*++(*argv))
92 switch (**argv) {
93 case 'R':
94 case 'r':
95 recursiveFlag = TRUE;
96 break;
97 case 'f':
98 forceFlag = TRUE;
Mark Whitleye0bf91d2001-03-13 00:40:19 +000099#ifdef BB_FEATURE_RM_INTERACTIVE
100 interactiveFlag = FALSE;
101#endif
102 break;
103 case 'i':
104#ifdef BB_FEATURE_RM_INTERACTIVE
105 interactiveFlag = TRUE;
106#endif
Eric Andersen815e9042000-06-06 16:15:23 +0000107 break;
108 case '-':
109 stopIt = TRUE;
110 break;
111 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000112 show_usage();
Eric Andersen815e9042000-06-06 16:15:23 +0000113 }
Pavel Roskine97da402000-06-14 17:39:41 +0000114 argc--;
115 argv++;
Eric Andersen815e9042000-06-06 16:15:23 +0000116 }
Pavel Roskine97da402000-06-14 17:39:41 +0000117 else
118 break;
119 }
120
121 if (argc < 1 && forceFlag == FALSE) {
Eric Andersen67991cf2001-02-14 21:23:06 +0000122 show_usage();
Eric Andersena9c95ea1999-11-15 17:33:30 +0000123 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124
125 while (argc-- > 0) {
126 srcName = *(argv++);
127 if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0
128 && errno == ENOENT) {
129 /* do not reports errors for non-existent files if -f, just skip them */
130 } else {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000131 if (recursive_action(srcName, recursiveFlag, FALSE,
Erik Andersen3364d782000-03-28 00:58:14 +0000132 TRUE, fileAction, dirAction, NULL) == FALSE) {
Matt Kraaid27753a2000-12-05 05:11:41 +0000133 status = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 }
135 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000136 }
Matt Kraaid27753a2000-12-05 05:11:41 +0000137 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000138}