diff src/vdisk.c @ 61:80f4ec9a3420

fix writeln chdir don't touch the path descriptor contents
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 25 Jul 2018 20:05:06 +0900
parents 7c6dc25c2b05
children 5b3871f8bdaa
line wrap: on
line diff
--- a/src/vdisk.c	Tue Jul 24 17:04:46 2018 +0900
+++ b/src/vdisk.c	Wed Jul 25 20:05:06 2018 +0900
@@ -16,8 +16,9 @@
 #include <string.h>
 #include <time.h>
 #include <arpa/inet.h>
+#include <fcntl.h>
 
-static int vdiskdebug = 0;  //   bit 1 trace, bit 2 filename
+static int vdiskdebug = 1;  //   bit 1 trace, bit 2 filename
 
 
 Byte pmmu[8];  // process dat mmu
@@ -200,6 +201,8 @@
         for(i=0;i<p-path;i++) name[i] = path[i];
         if (eighth) { name[i] = path[i]&0x7f; p++ ; i++; }
         name[i] = 0;
+        // skip trailing space
+        while(*p==' ') p++;
     }
     char *name1 = addCurdir(name,pd,curdir);
     if (name1!=name && name1!=path) free(name);
@@ -233,13 +236,27 @@
 }
 
 static char * 
-os9toUnixAttr(Byte attr) {
-    if ((attr&0x1) && (attr&0x2)) return "r+";
-    if (!(attr&0x1) && (attr&0x2)) return "w";
-    if ((attr&0x1) && !(attr&0x2)) return "r";
+os9toUnixAttr(Byte mode) {
+    if ((mode&0x1) && (mode&0x2)) return "r+";
+    if (!(mode&0x1) && (mode&0x2)) return "w";
+    if ((mode&0x1) && !(mode&0x2)) return "r";
     return "r";
 }
 
+static int 
+os9mode(Byte m) {
+    int mode = 0;
+    if ((m&0x80)) mode|=S_IFDIR ;
+    if ((m&0x01)) mode|=S_IRUSR ;
+    if ((m&0x02)) mode|=S_IWUSR ;
+    if ((m&0x04)) mode|=S_IXUSR ;
+    if ((m&0x08)) mode|=S_IROTH ;
+    if ((m&0x10)) mode|=S_IWOTH ;
+    if ((m&0x20)) mode|=S_IXOTH ;
+    return mode;
+}
+
+
 /*
  *   os9 file descriptor
  *   * File Descriptor Format
@@ -470,16 +487,20 @@
         case 0xd1:
             mode = *areg;
             attr = *breg;
+            pd->fp = 0;
             path = (char *)pmem(xreg);
-            next = pd->name  = checkFileName(path,pd,curdir);
-            pd->dir = 0;
-            pd->fp = fopen(pd->name, os9toUnixAttr(attr));
+            next = checkFileName(path,pd,curdir);
+            *breg = 0xff;
+            int fd = open(pd->name, O_RDWR+O_CREAT,os9mode(attr) );
+            if (fd>0)
+                pd->fp = fdopen(fd, os9toUnixAttr(mode));
             if (next!=0 && pd->fp ) {
+                *breg = 0;
+                *areg = pd->num;
+                *smem(u+1) = *areg ;
                 xreg += ( next - path );
                 pd->use = 1;
             } else  {
-                *breg = 0xff;
-                free(pd->name);
                 pd->use = 0;
             }
             break;
@@ -497,7 +518,6 @@
         *        B = errcode
         */
         case 0xd2:
-            *breg = 0xff;
             mode = *areg;
             attr = *breg;
             pd->fp = 0;
@@ -573,23 +593,24 @@
         *
         *
         * we keep track a cwd and a cxd for a process using 8bit id
+        * don't use path descriptor on y 
         */
-        case 0xd4:
+        case 0xd4: {
+            PathDesc dm = *pd;
             path = (char*)pmem(xreg);
-            next = checkFileName(path,pd,curdir);
+            next = checkFileName(path,&dm,curdir);
             if (next!=0) { 
                 struct stat buf;
-                if (stat(pd->name,&buf)!=0) break;
+                if (stat(dm.name,&buf)!=0) break;
                 if ((buf.st_mode & S_IFMT) != S_IFDIR) break;
                 xreg += ( next - path );
-                *areg = setcd(pd->name);
+                *areg = setcd(dm.name);
                 *smem(u+1) = *areg ;
-                pd->use = 1;
-                pd->dir = 1;
                 *breg = 0;
                 break;
             } 
             *breg = 0xff;
+         }
             break;
 
         /*
@@ -716,7 +737,7 @@
             int len = yreg;
             int i = 0;
             Byte *buf = pmem(xreg);
-            while(len>0 && *buf !='\r') {
+            while(len>0 && buf[i] !='\r') {
                 fputc(buf[i++],pd->fp);
                 len--;
             }
@@ -883,6 +904,12 @@
     ureg  = getword(frame+8);
     pcreg  = getword(frame+10)-3;            // point os9 swi2
     prog = (char*)(pmem(pcreg) - pcreg);
+    if (*pmem(pcreg)==0 && *pmem(pcreg+1)==0) {
+        // may be we are called from system state
+        // of coursel, this may wrong. but in system state, <$50 process has wrong DAT for pc
+        // and we can't know wether we are called from system or user
+        prog = (char*)(smem(pcreg) - pcreg);
+    }
     do_trace(fp);
 }