Updates
[oweals/busybox.git] / coreutils / rm.c
1 /*
2  * Mini rm implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
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
24 #include "internal.h"
25 #include <stdio.h>
26 #include <time.h>
27 #include <utime.h>
28 #include <dirent.h>
29 #include <errno.h>
30
31 static const char* rm_usage = "rm [OPTION]... FILE...\n\n"
32 "Remove (unlink) the FILE(s).\n\n"
33 "Options:\n"
34 "\t-f\tremove existing destinations, never prompt\n"
35 "\t-r\tremove the contents of directories recursively\n";
36
37
38 static int recursiveFlag = FALSE;
39 static int forceFlag = FALSE;
40 static const char *srcName;
41
42
43 static int fileAction(const char *fileName, struct stat* statbuf)
44 {
45     if (unlink( fileName) < 0 ) {
46         perror( fileName);
47         return ( FALSE);
48     }
49     return ( TRUE);
50 }
51
52 static int dirAction(const char *fileName, struct stat* statbuf)
53 {
54     if (rmdir( fileName) < 0 ) {
55         perror( fileName);
56         return ( FALSE);
57     }
58     return ( TRUE);
59 }
60
61 extern int rm_main(int argc, char **argv)
62 {
63     struct stat statbuf;
64
65     if (argc < 2) {
66         usage( rm_usage);
67     }
68     argc--;
69     argv++;
70
71     /* Parse any options */
72     while (**argv == '-') {
73         while (*++(*argv))
74             switch (**argv) {
75             case 'r':
76                 recursiveFlag = TRUE;
77                 break;
78             case 'f':
79                 forceFlag = TRUE;
80                 break;
81             default:
82                 usage( rm_usage);
83             }
84         argc--;
85         argv++;
86     }
87
88     while (argc-- > 0) {
89         srcName = *(argv++);
90         if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0 && errno == ENOENT) {
91             /* do not reports errors for non-existent files if -f, just skip them */
92         }
93         else {
94             if (recursiveAction( srcName, recursiveFlag, FALSE, 
95                         TRUE, fileAction, dirAction) == FALSE) {
96                 exit( FALSE);
97             }
98         }
99     }
100     exit( TRUE);
101 }