57b2d6c7066038a342cdba8c5f574821d3acd8dc
[people/mcb30/busybox.git] / miscutils / makedevs.c
1 /* vi: set sw=4 ts=4: */
2
3 #include <sys/types.h>
4
5 #include <fcntl.h>
6 #include <getopt.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <time.h>
11 #include <unistd.h>
12
13 #include "busybox.h"
14
15 #ifdef CONFIG_FEATURE_MAKEDEVS_LEAF
16
17 /*
18  * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
19  *
20  * makedevs
21  * Make ranges of device files quickly.
22  * known bugs: can't deal with alpha ranges
23  */
24  
25 int makedevs_main(int argc, char **argv)
26 {
27         mode_t mode;
28         char *basedev, *type, *nodname, buf[255];
29         int major, Sminor, S, E;
30
31         if (argc < 7 || *argv[1]=='-')
32                 bb_show_usage();
33
34         basedev = argv[1];
35         type = argv[2];
36         major = atoi(argv[3]) << 8;  /* correcting param to mknod() */
37         Sminor = atoi(argv[4]);
38         S = atoi(argv[5]);
39         E = atoi(argv[6]);
40         nodname = argc == 8 ? basedev : buf;
41
42         mode = 0660;
43
44         switch (type[0]) {
45         case 'c':
46                 mode |= S_IFCHR;
47                 break;
48         case 'b':
49                 mode |= S_IFBLK;
50                 break;
51         case 'f':
52                 mode |= S_IFIFO;
53                 break;
54         default:
55                 bb_show_usage();
56         }
57
58         while (S <= E) {
59                 int sz;
60
61                 sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S);
62                 if(sz<0 || sz>=sizeof(buf))  /* libc different */
63                         bb_error_msg_and_die("%s too large", basedev);
64
65         /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */
66
67                 if (mknod(nodname, mode, major | Sminor))
68                         bb_error_msg("Failed to create: %s", nodname);
69
70                 if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */
71                         nodname = buf;
72                 S++;
73                 Sminor++;
74         }
75
76         return 0;
77 }
78
79 #elif defined CONFIG_FEATURE_MAKEDEVS_TABLE
80
81 /*
82  *  This program is free software; you can redistribute it and/or modify
83  *  it under the terms of the GNU General Public License version 2 as
84  *  published by the Free Software Foundation.
85  *
86  *  This program is distributed in the hope that it will be useful,
87  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
88  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
89  *  GNU Library General Public License for more details.
90  *
91  *  You should have received a copy of the GNU General Public License
92  *  along with this program; if not, write to the Free Software
93  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
94  *
95  */
96
97 static const struct option makedevs_long_options[] = {
98         {"root", 1, NULL, 'r'},
99         {0, 0, 0, 0}
100 };
101
102 extern int makedevs_main(int argc, char **argv)
103 {
104         FILE *table;
105         int opt;
106         char *rootdir = "./";
107         char *line;
108         int ret = EXIT_SUCCESS;
109
110         bb_opt_complementaly = "d~r";
111         bb_applet_long_options = makedevs_long_options;
112         opt = bb_getopt_ulflags(argc, argv, "d:r:", &rootdir, &rootdir);
113
114         if (optind + 1 == argc) {
115                 table = bb_xfopen(argv[optind], "r");
116         } else {
117                 table = stdin;
118         }
119
120         if (chdir(rootdir) == -1) {
121                 bb_perror_msg_and_die("Couldnt chdor to %s", rootdir);
122         }
123
124         umask(0);
125
126         while ((line = bb_get_chomped_line_from_file(table))) {
127                 char type;
128                 unsigned int mode = 0755;
129                 unsigned int major = 0;
130                 unsigned int minor = 0;
131                 unsigned int count = 0;
132                 unsigned int increment = 0;
133                 unsigned int start = 0;
134                 char name[41];
135                 char user[41];
136                 char group[41];
137                 char *full_name;
138                 uid_t uid;
139                 gid_t gid;
140
141                 if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", name,
142                         &type, &mode, user, group, &major,
143                         &minor, &start, &increment, &count)) ||
144                         ((major | minor | start | count | increment) > 255)) {
145                         bb_error_msg("Ignoring invalid line\n%s\n", line);
146                         ret = EXIT_FAILURE;
147                         continue;
148                 }
149                 if (name[0] == '#') {
150                         continue;
151                 }
152                 if (group) {
153                         gid = get_ug_id(group, my_getgrnam);
154                 } else {
155                         gid = getgid();
156                 }
157                 if (user) {
158                         uid = get_ug_id(user, my_getpwnam);
159                 } else {
160                         uid = getuid();
161                 }
162                 full_name = concat_path_file(rootdir, name);
163
164                 if (type == 'd') {
165                         bb_make_directory(full_name, mode | S_IFDIR, 0);
166                         if (chown(full_name, uid, gid) == -1) {
167                                 bb_perror_msg("chown failed for %s", full_name);
168                                 ret = EXIT_FAILURE;
169                                 goto loop;
170                         }
171                 } else {
172                         dev_t rdev;
173
174                         if (type == 'p') {
175                                 mode |= S_IFIFO;
176                         }
177                         else if (type == 'c') {
178                                 mode |= S_IFCHR;
179                         }
180                         else if (type == 'b') {
181                                 mode |= S_IFBLK;
182                         } else {
183                                 bb_error_msg("Unsupported file type %c", type);
184                                 ret = EXIT_FAILURE;
185                                 goto loop;
186                         }
187
188                         if (count > 0) {
189                                 int i;
190                                 char *full_name_inc;
191
192                                 full_name_inc = xmalloc(strlen(full_name) + 4);
193                                 for (i = start; i < count; i++) {
194                                         sprintf(full_name_inc, "%s%d", full_name, i);
195                                         rdev = (major << 8) + minor + (i * increment - start);
196                                         if (mknod(full_name_inc, mode, rdev) == -1) {
197                                                 bb_perror_msg("Couldnt create node %s", full_name_inc);
198                                                 ret = EXIT_FAILURE;
199                                         }
200                                         else if (chown(full_name_inc, uid, gid) == -1) {
201                                                 bb_perror_msg("chown failed for %s", full_name_inc);
202                                                 ret = EXIT_FAILURE;
203                                         }
204                                 }
205                                 free(full_name_inc);
206                         } else {
207                                 rdev = (major << 8) + minor;
208                                 if (mknod(full_name, mode, rdev) == -1) {
209                                         bb_perror_msg("Couldnt create node %s", full_name);
210                                         ret = EXIT_FAILURE;
211                                 }
212                                 else if (chown(full_name, uid, gid) == -1) {
213                                         bb_perror_msg("chown failed for %s", full_name);
214                                         ret = EXIT_FAILURE;
215                                 }
216                         }
217                 }
218 loop:
219                 free(line);
220                 free(full_name);
221         }
222         fclose(table);
223
224         return 0;
225 }
226 #else
227 # error makdedevs configuration error, either leaf or table must be selected
228 #endif