| #include <stdio.h> |
| #include <string.h> |
| #include <errno.h> |
| #include <sys/types.h> |
| #include <sys/stat.h> |
| #include <fcntl.h> |
| #include <unistd.h> |
| |
| static int usage() |
| { |
| fprintf(stderr,"mknod <path> <type> [major] [minor]\n"); |
| fprintf(stderr," type: b|c|p (block|character|pipe)\n"); |
| return 1; |
| } |
| |
| int mknod_main(int argc, char *argv[]) |
| { |
| if (argc < 3) { |
| return usage(); |
| } |
| |
| char *path; |
| char type; |
| dev_t dev = 0; |
| int major, minor; |
| mode_t mode = 0600; |
| |
| path = argv[1]; |
| type = argv[2][0]; |
| |
| switch (type) { |
| case 'b': |
| mode |= S_IFBLK; |
| if (argc != 5) return usage(); |
| break; |
| case 'c': |
| mode |= S_IFCHR; |
| if (argc != 5) return usage(); |
| break; |
| case 'p': |
| mode |= S_IFIFO; |
| break; |
| default: |
| return usage(); |
| } |
| |
| if (type == 'b' || type == 'c') { |
| major = strtol(argv[3], NULL, 0); |
| minor = strtol(argv[4], NULL, 0); |
| dev = makedev(major, minor); |
| } |
| |
| if (mknod(path, mode, dev) == -1) { |
| perror("mknod"); |
| return 1; |
| } |
| return 0; |
| } |