regression: mmap for fd
[lunaix-os.git] / lunaix-os / kernel / demos / simple_sh.c
index acebe211de046867f99bc1cc2503d9cdbd93f363..8bb357bef15f741b284da29f6f0fb7e6008c97f3 100644 (file)
@@ -6,6 +6,8 @@
 #include <lunaix/signal.h>
 #include <lunaix/status.h>
 
+#include <usr/sys/mann.h>
+
 #include <klibc/string.h>
 #include <ulibc/stdio.h>
 
@@ -101,7 +103,7 @@ do_ls(const char* path)
     } else {
         struct dirent ent = { .d_offset = 0 };
         int status;
-        while ((status = readdir(fd, &ent)) == 1) {
+        while ((status = sys_readdir(fd, &ent)) == 1) {
             if (ent.d_type == DT_DIR) {
                 printf(" \033[3m%s\033[39;49m\n", ent.d_name);
             } else {
@@ -116,6 +118,25 @@ do_ls(const char* path)
     }
 }
 
+void
+do_mcat(const char* file)
+{
+    int fd = open(file, 0);
+    if (fd < 0) {
+        sh_printerr();
+    } else {
+        ptr_t p = mmap(NULL, 2048, 0, 0, fd, 0);
+        if ((int)p < 0) {
+            sh_printerr();
+        } else {
+            printf("%s\n", p);
+        }
+        munmap(p, 1);
+        close(fd);
+        printf("\n");
+    }
+}
+
 void
 sh_loop()
 {
@@ -161,6 +182,11 @@ sh_loop()
                 do_cat(argpart);
                 _exit(0);
             }
+        } else if (streq(cmd, "mcat")) {
+            if (!(p = fork())) {
+                do_mcat(argpart);
+                _exit(0);
+            }
         } else {
             printf("unknow command\n");
             goto cont;