Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1297 - (hide 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 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 niro 1297 if (argc == 1)
24     usage();
25 niro 532
26 niro 815 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 niro 532 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 niro 1297 switch (typec) {
42 niro 532 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 niro 815 major_num = strtol(*argv++, &endp, 0);
62 niro 532 if (*endp != '\0')
63     usage();
64 niro 815 minor_num = strtol(*argv++, &endp, 0);
65 niro 532 if (*endp != '\0')
66     usage();
67 niro 815 dev = makedev(major_num, minor_num);
68 niro 532 }
69    
70     if (*argv)
71     usage();
72    
73     if (mknod(name, mode|0666, dev) == -1) {
74     perror("mknod");
75     exit(1);
76     }
77    
78 niro 815 if (mode_set && chmod(name, mode_set)) {
79     perror("chmod");
80     exit(1);
81     }
82    
83 niro 532 exit(0);
84     }