1 /* mknod.c - make block or character special file
2  *
3  * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
4  *
5  * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mknod.html
6 
7 USE_MKNOD(NEWTOY(mknod, "<2>4m(mode):", TOYFLAG_BIN|TOYFLAG_UMASK))
8 
9 config MKNOD
10   bool "mknod"
11   default y
12   help
13     usage: mknod [-m MODE] NAME TYPE [MAJOR MINOR]
14 
15     Create a special file NAME with a given type. TYPE is b for block device,
16     c or u for character device, p for named pipe (which ignores MAJOR/MINOR).
17 
18     -m	Mode (file permissions) of new device, in octal or u+x format
19 */
20 
21 #define FOR_mknod
22 #include "toys.h"
23 
GLOBALS(char * m;)24 GLOBALS(
25   char *m;
26 )
27 
28 void mknod_main(void)
29 {
30   mode_t modes[] = {S_IFIFO, S_IFCHR, S_IFCHR, S_IFBLK};
31   int major=0, minor=0, type;
32   int mode = TT.m ? string_to_mode(TT.m, 0777) : 0660;
33 
34   type = stridx("pcub", *toys.optargs[1]);
35   if (type == -1) perror_exit("bad type '%c'", *toys.optargs[1]);
36   if (type) {
37     if (toys.optc != 4) perror_exit("need major/minor");
38 
39     major = atoi(toys.optargs[2]);
40     minor = atoi(toys.optargs[3]);
41   }
42 
43   if (mknod(toys.optargs[0], mode | modes[type], makedev(major, minor)))
44     perror_exit("mknod %s failed", toys.optargs[0]);
45 }
46