blob: a3542aaf63da7747751ed813f249cb9522d4ff37 [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 *
Mark Whitleyf6ba2da2001-03-13 16:35:55 +00009 * INTERACTIVE feature Copyright (C) 2001 by Alcove
10 * written by Christophe Boyanique <Christophe.Boyanique@fr.alcove.com>
11 * for Ipanema Technologies
12 *
Eric Andersenbed30e91999-10-18 19:02:32 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
Eric Andersenbed30e91999-10-18 19:02:32 +000029#include <stdio.h>
30#include <time.h>
31#include <utime.h>
32#include <dirent.h>
Eric Andersena9c95ea1999-11-15 17:33:30 +000033#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +000034#include <unistd.h>
35#include <stdlib.h>
Eric Andersen7c254412001-03-19 18:52:37 +000036#include <getopt.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000037#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000038
Eric Andersenbed30e91999-10-18 19:02:32 +000039static int recursiveFlag = FALSE;
40static int forceFlag = FALSE;
Mark Whitleye0bf91d2001-03-13 00:40:19 +000041#ifdef BB_FEATURE_RM_INTERACTIVE
42 static int interactiveFlag = FALSE;
43#endif
Eric Andersenbed30e91999-10-18 19:02:32 +000044static const char *srcName;
45
46
Erik Andersen3364d782000-03-28 00:58:14 +000047static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
Eric Andersencc8ed391999-10-05 16:24:54 +000048{
Mark Whitleye0bf91d2001-03-13 00:40:19 +000049#ifdef BB_FEATURE_RM_INTERACTIVE
50 if (interactiveFlag == TRUE) {
51 printf("rm: remove `%s'? ", fileName);
52 if (ask_confirmation() == 0)
53 return (TRUE);
54 }
55#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 if (unlink(fileName) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000057 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 return (FALSE);
59 }
60 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000061}
62
Erik Andersen3364d782000-03-28 00:58:14 +000063static int dirAction(const char *fileName, struct stat *statbuf, void* junk)
Eric Andersencc8ed391999-10-05 16:24:54 +000064{
Matt Kraai7c22b772000-09-20 23:10:21 +000065 if (recursiveFlag == FALSE) {
66 errno = EISDIR;
Mark Whitleyf57c9442000-12-07 19:56:48 +000067 perror_msg("%s", fileName);
Matt Kraai7c22b772000-09-20 23:10:21 +000068 return (FALSE);
69 }
Mark Whitleye0bf91d2001-03-13 00:40:19 +000070#ifdef BB_FEATURE_RM_INTERACTIVE
71 if (interactiveFlag == TRUE) {
72 printf("rm: remove directory `%s'? ", fileName);
73 if (ask_confirmation() == 0)
74 return (TRUE);
75 }
76#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 if (rmdir(fileName) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000078 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 return (FALSE);
80 }
81 return (TRUE);
Eric Andersenbed30e91999-10-18 19:02:32 +000082}
83
84extern int rm_main(int argc, char **argv)
85{
Eric Andersen7c254412001-03-19 18:52:37 +000086 int opt;
Matt Kraaid27753a2000-12-05 05:11:41 +000087 int status = EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 struct stat statbuf;
Eric Andersen7c254412001-03-19 18:52:37 +000089
90
91 /* do normal option parsing */
92 while ((opt = getopt(argc, argv, "Rrf-"
Mark Whitleye0bf91d2001-03-13 00:40:19 +000093#ifdef BB_FEATURE_RM_INTERACTIVE
Eric Andersen7c254412001-03-19 18:52:37 +000094"i"
Mark Whitleye0bf91d2001-03-13 00:40:19 +000095#endif
Eric Andersen7c254412001-03-19 18:52:37 +000096)) > 0) {
97 switch (opt) {
98 case 'R':
99 case 'r':
100 recursiveFlag = TRUE;
101 break;
102 case 'f':
103 forceFlag = TRUE;
Eric Andersen7c254412001-03-19 18:52:37 +0000104 break;
Eric Andersen7c254412001-03-19 18:52:37 +0000105#ifdef BB_FEATURE_RM_INTERACTIVE
Eric Andersencc165b92001-03-19 18:59:01 +0000106 case 'i':
Eric Andersen7c254412001-03-19 18:52:37 +0000107 interactiveFlag = TRUE;
108#endif
109 break;
Eric Andersen7c254412001-03-19 18:52:37 +0000110 default:
111 show_usage();
Eric Andersen815e9042000-06-06 16:15:23 +0000112 }
Pavel Roskine97da402000-06-14 17:39:41 +0000113 }
Eric Andersen7c254412001-03-19 18:52:37 +0000114
115 if ((argc-optind) < 1 && forceFlag == FALSE) {
Eric Andersen67991cf2001-02-14 21:23:06 +0000116 show_usage();
Eric Andersena9c95ea1999-11-15 17:33:30 +0000117 }
Eric Andersen16f70152001-03-19 18:54:38 +0000118#ifdef BB_FEATURE_RM_INTERACTIVE
119 if (forceFlag == TRUE)
120 interactiveFlag = FALSE;
121#endif
122
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123
Eric Andersen7c254412001-03-19 18:52:37 +0000124 while (optind < argc) {
125 srcName = argv[optind];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0
127 && errno == ENOENT) {
128 /* do not reports errors for non-existent files if -f, just skip them */
129 } else {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000130 if (recursive_action(srcName, recursiveFlag, FALSE,
Erik Andersen3364d782000-03-28 00:58:14 +0000131 TRUE, fileAction, dirAction, NULL) == FALSE) {
Matt Kraaid27753a2000-12-05 05:11:41 +0000132 status = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133 }
134 }
Eric Andersen7c254412001-03-19 18:52:37 +0000135 optind++;
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}