micro-utils/coreutils/mknod.c
2023-11-07 16:21:46 +03:00

82 lines
1.5 KiB
C

#ifdef __linux__
#include <sys/sysmacros.h>
#endif
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include "parse_mode.h"
long parse_int(const char *str) {
char *ptr = NULL;
long value = strtol(str, &ptr, 10);
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;
}
int main(int argc, char **argv) {
mode_t mode = 0666;
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;
}
}
argv += optind;
argc -= optind;
if (argc <= 1) {
fprintf(stderr, "mknod: missing operand\n");
return 1;
}
dev_t dev = 0;
if (argc == 4)
dev = makedev(parse_int(argv[2]), parse_int(argv[3]));
if (!strncmp("b", argv[1], 1))
mode |= S_IFBLK;
else if (!strncmp("c", argv[1], 1) || !strncmp("u", argv[1], 1))
mode |= S_IFCHR;
else if (!strncmp("p", argv[1], 1))
mode |= S_IFIFO;
else if (!strncmp("s", argv[1], 1))
mode |= S_IFSOCK;
else {
fprintf(stderr, "mknod: unknow file type\n");
return 1;
}
if (mknod(argv[0], mode, dev) < 0) {
fprintf(stderr, "mknod: %s\n", strerror(errno));
return 1;
}
return 0;
}