53 lines
888 B
C
53 lines
888 B
C
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
|
|
long parse_long(const char *str) {
|
|
char *ptr = NULL;
|
|
long value = strtol(str, &ptr, 10);
|
|
|
|
if (*ptr) {
|
|
fprintf(stderr, "mkfifo: 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 = parse_long(optarg);
|
|
break;
|
|
|
|
default:
|
|
printf("mkfifo [m] [file1 file2...]\n\t-m MODE\n");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
argv += optind;
|
|
argc -= optind;
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
if (mkfifo(argv[i], mode)) {
|
|
fprintf(stderr, "mkfifo: %s %s\n", argv[i], strerror(errno));
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|