Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1297 - (show annotations) (download)
Fri May 27 15:12:11 2011 UTC (13 years ago) by niro
File MIME type: text/plain
File size: 1296 byte(s)
-updated to klibc-1.5.22 with mntproc definitions patch included
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 if (argc == 1)
24 usage();
25
26 if (argv[0][0] == '-' && argv[0][1] == 'm' && !argv[0][2]) {
27 mode_set = strtoul(argv[1], &endp, 8);
28 argv += 2;
29 }
30
31 name = *argv++;
32 if (!name)
33 usage();
34
35 type = *argv++;
36 if (!type || !type[0] || type[1])
37 usage();
38 typec = type[0];
39
40 mode = 0;
41 switch (typec) {
42 case 'c':
43 mode = S_IFCHR;
44 break;
45 case 'b':
46 mode = S_IFBLK;
47 break;
48 case 'p':
49 mode = S_IFIFO;
50 break;
51 default:
52 usage();
53 }
54
55 if (mode == S_IFIFO) {
56 dev = 0;
57 } else {
58 if (!argv[0] || !argv[1])
59 usage();
60
61 major_num = strtol(*argv++, &endp, 0);
62 if (*endp != '\0')
63 usage();
64 minor_num = strtol(*argv++, &endp, 0);
65 if (*endp != '\0')
66 usage();
67 dev = makedev(major_num, minor_num);
68 }
69
70 if (*argv)
71 usage();
72
73 if (mknod(name, mode|0666, dev) == -1) {
74 perror("mknod");
75 exit(1);
76 }
77
78 if (mode_set && chmod(name, mode_set)) {
79 perror("chmod");
80 exit(1);
81 }
82
83 exit(0);
84 }