Framework for exporting system header to user space (#59)
[lunaix-os.git] / lunaix-os / usr / testp.c
index 8011c304b028451d289a211a49a3f2bbe318601d..c7d4c3882e97aa923e78a4234e3c802517f515be 100644 (file)
@@ -1,31 +1,37 @@
 #include <errno.h>
 #include <fcntl.h>
-#include <lunaix/lunaix.h>
+#include <sys/wait.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 
 void
-test_serial()
+test_serial(char* dev, int wr)
 {
-    int fd = open("/dev/ttyS0", FO_WRONLY);
+    int fd = open(dev, FO_WRONLY);
     if (fd < 0) {
-        printf("ttyS0 not accessable (%d)\n", errno);
+        printf("tty %s not accessable (%d)\n", dev, errno);
         return;
     }
 
-    char buf[32];
     int sz = 0;
+    char buf[256];
 
-    printf("ttyS0 input: ");
+    if (!wr) {
+        printf("tty input: ");
 
-    if ((sz = read(fd, buf, 31)) < 0) {
-        printf("write to ttyS0 failed (%d)\n", errno);
-    }
+        if ((sz = read(fd, buf, 31)) < 0) {
+            printf("read to tty failed (%d)\n", errno);
+        }
 
-    buf[sz] = 0;
+        buf[sz] = 0;
 
-    printf("%s", buf);
+        printf("%s", buf);
+    }
+    else {
+        int size = snprintf(buf, 256, "serial test: %s", dev);
+        write(fd, buf, size);
+    }
 
     close(fd);
 }
@@ -33,18 +39,14 @@ test_serial()
 int
 main(int argc, char* argv[])
 {
-    if (argc <= 1) {
+    if (argc != 2) {
+        printf("usage: testp path_to_dev\n");
         return 1;
     }
 
     char* target = argv[1];
 
-    if (!strcmp(target, "serial")) {
-        test_serial();
-    } else {
-        printf("unknown test: %s\n", target);
-        return 1;
-    }
+    test_serial(target, 1);
 
     return 0;
 }
\ No newline at end of file