⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file-syscall.c

📁 用于汇编领域的,运用于OS的MAIN函数.基于硬件基础的源代码
💻 C
字号:
#include "../hos.h"// file system callsint do_creat(char *fname){   struct Inode *ldir_inode;   struct DirBlock *ldir;   int x, new_ino,new_bno;   char leaf_name[EXT2_NAME_LEN]; // for leaf file name   // compute the inode for last directory, and leaf file name    ldir_inode = ITable_compute_inode_for_last_directory(fname, leaf_name);   printk("last dir ino %d : ",ldir_inode->ino);   // get the directory block for this dir   ldir = Inode_get_block(ldir_inode);   printk("its blk is "); FB_dump((char *)ldir);   // alloc new inode   new_ino = BitVect_get_free_bit(d_ibm);   printk("new ino %d : ",new_ino);   // alloc a block   new_bno = BitVect_get_free_bit(d_dbm);   printk("new blk %d : ", new_bno);   // make inode   ITable_make_inode(new_ino, new_bno);   // report this in the last directory   DirBlk_report_new_entry(ldir, new_ino, leaf_name);   // flush file system   flush_to_disk();   // now perform open for this file   x = do_open(fname);   return x;}int do_open(char *fname){   struct Inode *inode;   struct FilePtr *fptr;   int x;   inode = ITable_compute_inode(fname);   // report it in file table   fptr = FPTable_report_new_file(inode);   // report it in current->fd   x = FDTable_report_new_fd(current->fd, fptr);   printk("in do_open. returing fd %d\n",x);   return x;}int read_from_kbd(char *buf, int n){   int i;   //printk("read_from_kbd. we read: ");   for(i=0;i<n;i++){ // read max n bytes     asm("sti\n\t"::); // we need kbd int to read     buf[i]=wait_for_key(); 	 asm("cli\n\t"::); // the rule. we disable int whenever possible inside system call	 //printk("%x ", buf[i]);	 if (buf[i]==0xd){ // if carriage return	    buf[i]=0;		break; // we read all input string	 }   }   //printk("\n. buf is %s. len is %d",buf,i);   return i; // length of input string}void do_read(struct pt_regs *regs){//void do_read(int x, char *buf, int n){// read n bytes from file x. put it in buf      int x; char *buf; int n;   x=regs->ebx; buf = (char *)regs->ecx; n = regs->edx;   //printk("in do_read. we want to read from fd %d into buf %d by %d bytes\n",   //       x, (int)buf, n);   return kernel_do_read(x, buf, n);}int kernel_do_read(int x, char *buf, int n){   struct Inode *in;   char *blk;   int pos, len;   //printk("in kernel_do_read\n");   if (x==0){ // for now, treat specially      //printk("fd is 0. read from keyboard\n");      len=read_from_kbd(buf, n);	  //printk("read done. buf is %s. len is %d\n",buf, len);	  return len;   }   else{ // regular case      printk("read from a file\n");      pos = current->fd[x]->pos; // current file pointer      in = current->fd[x]->iptr;      // get it      if ((pos+n) >= BLKSIZE) panic("can't read past block, yet\n");      blk = FBTable_get_block(in->inode->i_block[0]);      substr(blk, buf, pos, pos+n-1);      current->fd[x]->pos += n; // move the file pointer	  return n;   }}void do_write(struct pt_regs *regs){//void do_write(int x, char *buf, int n){// write n bytes in buf to file x      int x; char *buf; int n;   x=regs->ebx; buf = (char *)regs->ecx; n = regs->edx;   buf[n]=0; // make it a string   //printk("in do_write. we want write %s into fd %d by %d bytes\n",   //       buf, x, n);   kernel_do_write(x, buf, n);}void kernel_do_write(int x, char *buf, int n){   struct Inode *in;   char *blk;   int pos;   //printk("in kernel_do_write\n");   if (x==1){ // treat specially for now      //printk("fd is 1. dump to the terminal\n");      buf[n]=0;	  printk(buf);   }   else{      printk("fd is not 1. dump to the file\n");      pos = current->fd[x]->pos; // current file pointer      in = current->fd[x]->iptr;      // assume all files are 1 block for now. get it      blk = FBTable_get_block(in->inode->i_block[0]);      write_substr(buf, blk, pos, pos+n-1);      current->fd[x]->pos += n; // move the file pointer      flush_to_disk();   }}      

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -