view vdisk.c @ 43:7a83a6a1685a

vdisk start
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 17 Jul 2018 17:08:09 +0900
parents
children b26c23331d02
line wrap: on
line source

/********************************************************************
* Virtual RBF - Random Block File Manager
*
*         Shinji KONO  (kono@ie.u-ryukyu.ac.jp)  2018/7/17
*         GPL v1 license
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/select.h>


/****************************
*
* Main entry point for RBF
*
         lbra  Open
         lbra  MakDir
         lbra  ChgDir
         lbra  Delete
         lbra  Seek
         lbra  Read
         lbra  Write
         lbra  ReadLn
         lbra  WriteLn
         lbra  GetStat
         lbra  SetStat
         lbra  Close
 */


/*
* I$Create Entry Point
*
* Entry: A = access mode desired
*        B = file attributes
*        X = address of the pathlist
*
* Exit:  A = pathnum
*        X = last byte of pathlist address
*
* Error: CC Carry set
*        B = errcode
*/

#define MAXPAHTLEN 256

char * checkFileName(char *path, PathDesc *pd) {
    char *p = path;
    int maxlen = MAXPAHTLEN;
    pd->lastChar = p;
    while(*p!=0 && (*p&80)==0 && maxlen-->0) p++;
    if (maxlen==MAXPAHTLEN) return 0;
    pd->lastChar = p;
    if (*p!=0) {
        p = (char *)malloc(len+1); 
        strncpy(path,p, MAXPAHTLEN-maxlen);
    }
    return p;
}

int os9toUnixAttr(Byte attr) {
    return "r+";
}

int vrbf_create(byte mode,Byte attr,char *path,PathDesc *pd) {
    char *p = pd->unixName  = checkFileName(path,pd);
    if (p==0) return -1;
    p->dir = 0;
    p->fp = fopen(p->unixName,"w+"); // os9toUnixAttr(attr));
    if  (fd) return 0;
    return -1;
}


/*
* I$Open Entry Point
*
* Entry: A = access mode desired
*        X = address of the pathlist
*
* Exit:  A = pathnum
*        X = last byte of pathlist address
*
* Error: CC Carry set
*        B = errcode
*/

int vrbf_open(byte mode,char *path, PathDesc *pd) {
    char *p = pd->unixName = checkFileName(path,pd);
    if (p==0) return -1;
    struct stat buf;
    if (stat(p,&buf)!=0) return -1;
    if ((buf.st_mode & S_IFMT) == S_IFDIR) {
        pd->dir = 1;
        return 0;
    }
    pd->dir = 0;
    pd->fp = fopen(p->unixName,"r"); // os9toUnixAttr(attr));
    if  (fp) return 0;
    return -1;
}


/*
* I$MakDir Entry Point
*
* Entry: X = address of the pathlist
*
* Exit:  X = last byte of pathlist address
*
* Error: CC Carry set
*        B = errcode
*/

int vrbf_mkdir(char **path,PathDesc *pd) {
    char *p = checkFileName(path,pd);
    if (p==0) return -1;
    return mkdir(p,0);
}

/*
* I$ChgDir Entry Point
*
* Entry:
*
* Exit:
*
* Error: CC Carry set
*        B = errcode
*/

char * vrbf_chdir(char *path) {
    cahr *p = checkFileName(path);
    if (p==0) return -1;
    struct stat buf;
    if (stat(p,&buf)!=0) return -1;
    if ((buf.st_mode & S_IFMT) == S_IFDIR) {
        chdir(p);
        return p;
    } 
    return -1;
}


/*
* I$Delete Entry Point
*
* Entry:
*
* Exit:
*
* Error: CC Carry set
*        B = errcode
*/

int vrbf_delete(char *path) {
    cahr *p = checkFileName(path);
    if (p==0) return -1;
    return rmdir(p);
}


/*
* I$Seek Entry Point
*
* Entry:
*
* Exit:
*
* Error: CC Carry set
*        B = errcode
*/

int vrbf_seek(PathDesc *p, int seek) {
    return fseek(p->fp,(off_t)seek,SEEK_SET);
}


*
* I$ReadLn Entry Point
*
* Entry:
*
* Exit:
*
* Error: CC Carry set
*        B = errcode
*
*
int vrbf_readln(PathDesc *p, byte *buf, int len) {
    if (p->dir) return -1;
    if (fgets(buf,len,p->fp)) {
        int i;
        for(i=0;i<len && buf[i];i++);
        if (i>0 && buf[i-1]=='\n') {
            buf[i-1] = '\r';
            return i;
        }
    } 
    return -1;
}

/*
* I$Read Entry Point
*
* Entry:
*
* Exit:
*
* Error: CC Carry set
*        B = errcode
*/

int vrbf_read(PathDesc *p, byte *buf, int len) {
    if (p->dir) return dirread(p,buf,len);
    return fread(buf,len,1,p->fp);
}

/*
* I$WritLn Entry Point
*
* Entry:
*
* Exit:
*
* Error: CC Carry set
*        B = errcode
*/

int vrbf_writeln(PathDesc *p, byte *buf, int len) {
    if (p->dir) return -1;
    int = 0;
    while(len>0 && *buf !='\r) {
        fputc(buf[i++],p->fp);
        len--;
    }
    if (buf[i]=='\r') {
        fputc('\n',p->fp);
        i++;
    }
    return i;
}

/*
* I$Write Entry Point
*
* Entry:
*
* Exit:
*
* Error: CC Carry set
*        B = errcode
*/

int vrbf_write(PathDesc *p, byte *buf, int len) {
    if (p->dir) return -1;
    return fwrite(buf,len,1,p->fp);
}

/* I$Close Entry Point
*
* Entry: A = path number
*
* Exit:
*
* Error: CC Carry set
*        B = errcode
*
*/

int vrbf_close(PathDesc *p, byte *buf) {
    return fclose(p->fp);
}

/*
* I$GetStat Entry Point
*
* Entry:
*
* Exit:
*
* Error: CC Carry set
*        B = errcode
*/

int vrbf_getstat(int code,OPathDesc *p, O9Stat *s) {
    struct stat st;

    switch (code) {
        case 01: // SS.Ready
            fstat(fileno(p->fp),&st);
            if (pos = ftell(fp)) {
                return st.size - pos;
            }
            break;
        case 02: // SS.SIZ
            fstat(fileno(p->fp),&st);
            return st.size ;
            break;
        case 05: // SS.Pos
            return ftell(s->fp);
            break;
        case 15: // SS.FD
            break;
        case 0x20: // Pos.FDInf
            break;
        default: return -1;
    }
    return -1;
}

/*
* I$SetStat Entry Point
*
* Entry:
*
* Exit:
*
* Error: CC Carry set
*        B = errcode
*/

int vrbf_getstat(int code,OPathDesc *p, O9Stat *s) {
    switch (code) {
        case 0: // SS.Opt
            break;
        case 02: // SS.SIZ
            break;
        case 15: // SS.FD
            break;
        case 0x11: // SS.Lock
            break;
        case 0x10: // SS.Ticks
            break;
        case 0x20: // SS.RsBit
            break;
        case 0x1c: // SS.Attr
            break;
        default: return -1;
    }
}