Magellan Linux

Contents of /trunk/mkinitrd-magellan/klibc/usr/utils/nuke.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1297 - (show annotations) (download)
Fri May 27 15:12:11 2011 UTC (12 years, 11 months ago) by niro
File MIME type: text/plain
File size: 3067 byte(s)
-updated to klibc-1.5.22 with mntproc definitions patch included
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 2004-2006 H. Peter Anvin - All Rights Reserved
4 *
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
12 * conditions:
13 *
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * ----------------------------------------------------------------------- */
27
28 /*
29 * Simple program which does the same thing as rm -rf, except it takes
30 * no options and can therefore not get confused by filenames starting
31 * with -. Similarly, an empty list of inputs is assumed to mean don't
32 * do anything.
33 */
34
35 #include <assert.h>
36 #include <dirent.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42
43 static const char *program;
44
45 static int nuke(const char *what);
46
47 static int nuke_dirent(int len, const char *dir, const char *name)
48 {
49 int bytes = len + strlen(name) + 2;
50 char path[bytes];
51 int xlen;
52
53 xlen = snprintf(path, bytes, "%s/%s", dir, name);
54 assert(xlen < bytes);
55
56 return nuke(path);
57 }
58
59 /* Wipe the contents of a directory, but not the directory itself */
60 static int nuke_dir(const char *what)
61 {
62 int len = strlen(what);
63 DIR *dir;
64 struct dirent *d;
65 int err = 0;
66
67 dir = opendir(what);
68 if (!dir) {
69 /* EACCES means we can't read it. Might be empty and removable;
70 if not, the rmdir() in nuke() will trigger an error. */
71 return (errno == EACCES) ? 0 : errno;
72 }
73
74 while ((d = readdir(dir))) {
75 /* Skip . and .. */
76 if (d->d_name[0] == '.' &&
77 (d->d_name[1] == '\0' ||
78 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
79 continue;
80
81 err = nuke_dirent(len, what, d->d_name);
82 if (err) {
83 closedir(dir);
84 return err;
85 }
86 }
87
88 closedir(dir);
89
90 return 0;
91 }
92
93 static int nuke(const char *what)
94 {
95 int rv;
96 int err = 0;
97
98 rv = unlink(what);
99 if (rv < 0) {
100 if (errno == EISDIR) {
101 /* It's a directory. */
102 err = nuke_dir(what);
103 if (!err)
104 rmdir(what);
105 }
106 }
107
108 return 0;
109 }
110
111 int main(int argc, char *argv[])
112 {
113 int i;
114
115 program = argv[0];
116
117 for (i = 1; i < argc; i++)
118 nuke(argv[i]);
119
120 return 0;
121 }