1/* See LICENSE file for copyright and license details. */
2
3
4#include <sys/stat.h>
5#include <sys/types.h>
6#ifndef makedev
7#include <sys/sysmacros.h>
8#endif
9
10#include <fcntl.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <unistd.h>
15
16#include "util.h"
17
18static void
19usage(void)
20{
21 eprintf("usage: %s [-m mode] name b|c|u major minor\n"
22 " %s [-m mode] name p\n",
23 argv0, argv0);
24}
25
26// ?man mknod: create special files
27// ?man arguments: name b|c|u major minor
28// ?man create block or character special files
29int
30main(int argc, char *argv[])
31{
32 mode_t mode = 0666;
33 dev_t dev;
34
35 ARGBEGIN {
36// ?man -m:mode: specify mode or limit
37case 'm':
38 mode = parsemode(EARGF(usage()), mode, umask(0));
39 break;
40 default:
41 usage();
42 } ARGEND;
43
44 if (argc < 2)
45 usage();
46
47 if (strlen(argv[1]) != 1)
48 goto invalid;
49 switch (argv[1][0]) {
50 case 'b':
51 mode |= S_IFBLK;
52 break;
53 case 'u':
54 case 'c':
55 mode |= S_IFCHR;
56 break;
57 case 'p':
58 mode |= S_IFIFO;
59 break;
60 default:
61 invalid:
62 eprintf("invalid type '%s'\n", argv[1]);
63 }
64
65 if (S_ISFIFO(mode)) {
66 if (argc != 2)
67 usage();
68 dev = 0;
69 } else {
70 if (argc != 4)
71 usage();
72 dev = makedev(estrtonum(argv[2], 0, LLONG_MAX), estrtonum(argv[3], 0, LLONG_MAX));
73 }
74
75 if (mknod(argv[0], mode, dev) == -1)
76 eprintf("mknod %s:", argv[0]);
77 return 0;
78}