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