mknod.c (829B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <sys/stat.h> 3 #include <sys/types.h> 4 5 #include <fcntl.h> 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <string.h> 9 #include <unistd.h> 10 11 #include "util.h" 12 13 static void 14 usage(void) 15 { 16 eprintf("usage: %s [-m mode] name type major minor\n", argv0); 17 } 18 19 int 20 main(int argc, char *argv[]) 21 { 22 mode_t type, mode = 0644; 23 dev_t dev; 24 25 ARGBEGIN { 26 case 'm': 27 mode = estrtol(EARGF(usage()), 8); 28 break; 29 default: 30 usage(); 31 } ARGEND; 32 33 if (argc != 4) 34 usage(); 35 36 if (strlen(argv[1]) != 1 || !strchr("ucb", argv[1][0])) 37 eprintf("mknod: '%s': invalid type\n", argv[1]); 38 type = (argv[1][0] == 'b') ? S_IFBLK : S_IFCHR; 39 40 dev = makedev(estrtol(argv[2], 0), estrtol(argv[3], 0)); 41 42 if (mknod(argv[0], type|mode, dev) == -1) 43 eprintf("mknod: '%s':", argv[0]); 44 return 0; 45 }