Contents of /trunk/mkinitrd-magellan/klibc/usr/utils/nuke.c
Parent Directory | Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 3083 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 3083 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
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 <alloca.h> |
36 | #include <assert.h> |
37 | #include <dirent.h> |
38 | #include <errno.h> |
39 | #include <string.h> |
40 | #include <stdio.h> |
41 | #include <sys/types.h> |
42 | #include <unistd.h> |
43 | |
44 | static const char *program; |
45 | |
46 | static int nuke(const char *what); |
47 | |
48 | static int nuke_dirent(int len, const char *dir, const char *name) |
49 | { |
50 | int bytes = len + strlen(name) + 2; |
51 | char path[bytes]; |
52 | int xlen; |
53 | |
54 | xlen = snprintf(path, bytes, "%s/%s", dir, name); |
55 | assert(xlen < bytes); |
56 | |
57 | return nuke(path); |
58 | } |
59 | |
60 | /* Wipe the contents of a directory, but not the directory itself */ |
61 | static int nuke_dir(const char *what) |
62 | { |
63 | int len = strlen(what); |
64 | DIR *dir; |
65 | struct dirent *d; |
66 | int err = 0; |
67 | |
68 | if (!(dir = opendir(what))) { |
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 | } |