Add mknod
Change-Id: I1a5b77577784237851cf7478905a4ffd9f48eb0e
diff --git a/Makefile b/Makefile
index 8151fd1..064a5f3 100644
--- a/Makefile
+++ b/Makefile
@@ -45,7 +45,8 @@
vmstat \
lsof \
daemon \
- devmem
+ devmem \
+ mknod
LOCAL_SRC_FILES:= \
toolbox.c \
diff --git a/mknod.c b/mknod.c
new file mode 100644
index 0000000..8b64e49
--- /dev/null
+++ b/mknod.c
@@ -0,0 +1,58 @@
+#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;
+}