Magellan Linux

Contents of /tags/mkinitrd-6_3_0/klibc/usr/utils/mknod.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1139 - (show annotations) (download)
Thu Aug 19 10:14:02 2010 UTC (13 years, 10 months ago) by niro
File MIME type: text/plain
File size: 1271 byte(s)
tagged 'mkinitrd-6_3_0'
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/stat.h>
5
6 char *progname;
7
8 static __noreturn usage(void)
9 {
10 fprintf(stderr, "Usage: %s [-m mode] name {b|c|p} major minor\n",
11 progname);
12 exit(1);
13 }
14
15 int main(int argc, char *argv[])
16 {
17 char *name, *type, typec, *endp;
18 unsigned int major_num, minor_num;
19 mode_t mode, mode_set = 0;
20 dev_t dev;
21
22 progname = *argv++;
23
24 if (argv[0][0] == '-' && argv[0][1] == 'm' && !argv[0][2]) {
25 mode_set = strtoul(argv[1], &endp, 8);
26 argv += 2;
27 }
28
29 name = *argv++;
30 if (!name)
31 usage();
32
33 type = *argv++;
34 if (!type || !type[0] || type[1])
35 usage();
36 typec = type[0];
37
38 mode = 0;
39 switch (type[0]) {
40 case 'c':
41 mode = S_IFCHR;
42 break;
43 case 'b':
44 mode = S_IFBLK;
45 break;
46 case 'p':
47 mode = S_IFIFO;
48 break;
49 default:
50 usage();
51 }
52
53 if (mode == S_IFIFO) {
54 dev = 0;
55 } else {
56 if (!argv[0] || !argv[1])
57 usage();
58
59 major_num = strtol(*argv++, &endp, 0);
60 if (*endp != '\0')
61 usage();
62 minor_num = strtol(*argv++, &endp, 0);
63 if (*endp != '\0')
64 usage();
65 dev = makedev(major_num, minor_num);
66 }
67
68 if (*argv)
69 usage();
70
71 if (mknod(name, mode|0666, dev) == -1) {
72 perror("mknod");
73 exit(1);
74 }
75
76 if (mode_set && chmod(name, mode_set)) {
77 perror("chmod");
78 exit(1);
79 }
80
81 exit(0);
82 }