Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 815 - (hide annotations) (download)
Fri Apr 24 18:32:46 2009 UTC (15 years, 1 month ago) by niro
File MIME type: text/plain
File size: 1271 byte(s)
-updated to klibc-1.5.15
1 niro 532 #include <stdio.h>
2     #include <stdlib.h>
3 niro 815 #include <string.h>
4 niro 532 #include <sys/stat.h>
5    
6     char *progname;
7    
8     static __noreturn usage(void)
9     {
10 niro 815 fprintf(stderr, "Usage: %s [-m mode] name {b|c|p} major minor\n",
11     progname);
12 niro 532 exit(1);
13     }
14    
15     int main(int argc, char *argv[])
16     {
17     char *name, *type, typec, *endp;
18 niro 815 unsigned int major_num, minor_num;
19     mode_t mode, mode_set = 0;
20 niro 532 dev_t dev;
21    
22     progname = *argv++;
23    
24 niro 815 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 niro 532 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 niro 815 major_num = strtol(*argv++, &endp, 0);
60 niro 532 if (*endp != '\0')
61     usage();
62 niro 815 minor_num = strtol(*argv++, &endp, 0);
63 niro 532 if (*endp != '\0')
64     usage();
65 niro 815 dev = makedev(major_num, minor_num);
66 niro 532 }
67    
68     if (*argv)
69     usage();
70    
71     if (mknod(name, mode|0666, dev) == -1) {
72     perror("mknod");
73     exit(1);
74     }
75    
76 niro 815 if (mode_set && chmod(name, mode_set)) {
77     perror("chmod");
78     exit(1);
79     }
80    
81 niro 532 exit(0);
82     }