2023-10-20 19:16:01 +03:00
|
|
|
#ifdef __linux__
|
2023-10-12 22:11:52 +03:00
|
|
|
#include <sys/sysmacros.h>
|
2023-10-20 19:16:01 +03:00
|
|
|
#endif
|
|
|
|
|
2023-10-12 22:11:52 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2023-11-07 16:21:46 +03:00
|
|
|
#include <unistd.h>
|
2023-10-12 22:11:52 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2023-11-01 15:33:16 +03:00
|
|
|
#include "parse_mode.h"
|
2023-10-12 22:11:52 +03:00
|
|
|
|
2023-11-01 12:25:51 +03:00
|
|
|
long parse_int(const char *str) {
|
2023-10-12 22:11:52 +03:00
|
|
|
char *ptr = NULL;
|
2023-11-01 12:25:51 +03:00
|
|
|
long value = strtol(str, &ptr, 10);
|
2023-10-12 22:11:52 +03:00
|
|
|
|
|
|
|
if (*ptr) {
|
|
|
|
fprintf(stderr, "mknod: not a number: %s\n", str);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (value < 1) {
|
|
|
|
fprintf(stderr, "mknod: number is negative: %s\n", str);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2023-11-07 16:21:46 +03:00
|
|
|
int main(int argc, char **argv) {
|
2023-10-12 22:11:52 +03:00
|
|
|
mode_t mode = 0666;
|
|
|
|
|
2023-11-07 16:21:46 +03:00
|
|
|
int opt;
|
|
|
|
while ((opt = getopt(argc, argv, "m:")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'm':
|
|
|
|
mode = mu_parse_mode(optarg, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("mknod [-m MODE] [NAME] [TYPE] [MAJOR MINOR]\n");
|
|
|
|
printf("Types:\n\tb - block device\n\tc or u - character device\n\tp - named pipe/fifo (MAJOR MINOR must be omitted)\n\ts - socket\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2023-10-12 22:11:52 +03:00
|
|
|
|
2023-11-07 16:21:46 +03:00
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
|
|
|
if (argc <= 1) {
|
|
|
|
fprintf(stderr, "mknod: missing operand\n");
|
|
|
|
return 1;
|
2023-10-12 22:11:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dev_t dev = 0;
|
2023-11-07 16:21:46 +03:00
|
|
|
if (argc == 4)
|
|
|
|
dev = makedev(parse_int(argv[2]), parse_int(argv[3]));
|
2023-10-12 22:11:52 +03:00
|
|
|
|
2023-11-07 16:21:46 +03:00
|
|
|
if (!strncmp("b", argv[1], 1))
|
|
|
|
mode |= S_IFBLK;
|
2023-10-12 22:11:52 +03:00
|
|
|
|
2023-11-07 16:21:46 +03:00
|
|
|
else if (!strncmp("c", argv[1], 1) || !strncmp("u", argv[1], 1))
|
|
|
|
mode |= S_IFCHR;
|
2023-10-12 22:11:52 +03:00
|
|
|
|
2023-11-07 16:21:46 +03:00
|
|
|
else if (!strncmp("p", argv[1], 1))
|
|
|
|
mode |= S_IFIFO;
|
2023-10-12 22:11:52 +03:00
|
|
|
|
2023-11-07 16:21:46 +03:00
|
|
|
else if (!strncmp("s", argv[1], 1))
|
|
|
|
mode |= S_IFSOCK;
|
2023-10-12 22:11:52 +03:00
|
|
|
|
2023-11-07 16:21:46 +03:00
|
|
|
else {
|
|
|
|
fprintf(stderr, "mknod: unknow file type\n");
|
|
|
|
return 1;
|
2023-10-12 22:11:52 +03:00
|
|
|
}
|
|
|
|
|
2023-11-07 16:21:46 +03:00
|
|
|
if (mknod(argv[0], mode, dev) < 0) {
|
|
|
|
fprintf(stderr, "mknod: %s\n", strerror(errno));
|
|
|
|
return 1;
|
2023-10-12 22:11:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|