blob: ee434fb3984705e4379220f3cb0653585a21f6a0 [file] [log] [blame]
Eric Andersenbed30e91999-10-18 19:02:32 +00001/*
2 * Mini rm implementation for busybox
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersenbed30e91999-10-18 19:02:32 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include "internal.h"
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 Andersencc8ed391999-10-05 16:24:54 +000030
Eric Andersend73dc5b1999-11-10 23:13:02 +000031static const char* rm_usage = "rm [OPTION]... FILE...\n\n"
Eric Andersenbed30e91999-10-18 19:02:32 +000032"Remove (unlink) the FILE(s).\n\n"
Eric Andersend73dc5b1999-11-10 23:13:02 +000033"Options:\n"
Eric Andersen08b10341999-11-19 02:38:58 +000034"\t-f\t\tremove existing destinations, never prompt\n"
35"\t-r or -R\tremove the contents of directories recursively\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000036
Eric Andersenbed30e91999-10-18 19:02:32 +000037
38static int recursiveFlag = FALSE;
39static int forceFlag = FALSE;
40static const char *srcName;
41
42
43static int fileAction(const char *fileName, struct stat* statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +000044{
Eric Andersenbed30e91999-10-18 19:02:32 +000045 if (unlink( fileName) < 0 ) {
46 perror( fileName);
47 return ( FALSE);
48 }
49 return ( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000050}
51
Eric Andersenbed30e91999-10-18 19:02:32 +000052static int dirAction(const char *fileName, struct stat* statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +000053{
Eric Andersenbed30e91999-10-18 19:02:32 +000054 if (rmdir( fileName) < 0 ) {
55 perror( fileName);
56 return ( FALSE);
57 }
58 return ( TRUE);
59}
60
61extern int rm_main(int argc, char **argv)
62{
Eric Andersena9c95ea1999-11-15 17:33:30 +000063 struct stat statbuf;
Eric Andersenbed30e91999-10-18 19:02:32 +000064
65 if (argc < 2) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000066 usage( rm_usage);
Eric Andersenbed30e91999-10-18 19:02:32 +000067 }
68 argc--;
69 argv++;
70
71 /* Parse any options */
72 while (**argv == '-') {
73 while (*++(*argv))
74 switch (**argv) {
Eric Andersen08b10341999-11-19 02:38:58 +000075 case 'R':
Eric Andersenbed30e91999-10-18 19:02:32 +000076 case 'r':
77 recursiveFlag = TRUE;
78 break;
79 case 'f':
80 forceFlag = TRUE;
81 break;
82 default:
Eric Andersenb0e9a701999-10-18 22:28:26 +000083 usage( rm_usage);
Eric Andersenbed30e91999-10-18 19:02:32 +000084 }
85 argc--;
86 argv++;
87 }
88
89 while (argc-- > 0) {
90 srcName = *(argv++);
Eric Andersena9c95ea1999-11-15 17:33:30 +000091 if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0 && errno == ENOENT) {
92 /* do not reports errors for non-existent files if -f, just skip them */
93 }
94 else {
95 if (recursiveAction( srcName, recursiveFlag, FALSE,
96 TRUE, fileAction, dirAction) == FALSE) {
97 exit( FALSE);
98 }
Eric Andersencc8ed391999-10-05 16:24:54 +000099 }
Eric Andersenbed30e91999-10-18 19:02:32 +0000100 }
101 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000102}