You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.9 KiB

/*
* linux/lib/update.c
*
* (C) 2021 Huan
*/
#include <errno.h>
#include <a.out.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <asm/segment.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <new.h>
#define DIRBUF 8192 /* buffer size for fs-indep. dirs */
/* must in general be larger than the filesystem buffer size */
int sys_getdents(unsigned int fd, struct linux_dirent *d, unsigned int count)
{
struct linux_dirent tmp;
struct file * file;
struct m_inode * inode;
struct buffer_head * block;
struct dir_entry * ptr;
char * buf;
int inum; /*the "inum"st inode.*/
int dnum; /*the "dnum"st element of array "d".*/
int ld_size;
int i;
int result;
ld_size = sizeof(struct linux_dirent); /*the size of struct "linux_dirent".(24)*/
file = current->filp[fd]; /*get the current file pointer we need.*/
if (fd >= NR_OPEN) return -EINVAL; /*shu zu yue jie*/
if (count == 0) return -1; /*the size of dirp is too small(0)*/
if (!file) return ENOTDIR; /*can't open file.*/
inode = file->f_inode; /*get the inode of this file.*/
block = bread(inode->i_dev,inode->i_zone[0]); /*get the block that storing inodes.*/
tmp.d_reclen = sizeof(tmp); /*the size of struct "linux_dirent".(24)*/
tmp.d_off = 0; /*useless*/
buf = &tmp; /*get the address of tmp.*/
dnum = 0; /*the "pian yi liang" of array "d".*/
for (inum = 0;(inum*sizeof(struct dir_entry))<inode->i_size;inum++) {
if (dnum >= (count-ld_size)) return 0; /*shu zu "d" yue jie*/
ptr = (struct dir_entry *) (block->b_data + (inum*sizeof(struct dir_entry)));
/*get the address of the next inode.*/
if (!ptr -> inode) continue; /*ignore current inode if it is empty.*/
tmp.d_ino = ptr -> inode; /*copy the inode into tmp.*/
for (i = 0; i < 14; i++) tmp.d_name[i] = ptr -> name[i]; /*copy current file name into tmp.*/
for (i = 0; i < tmp.d_reclen; i++){
put_fs_byte(*(buf+i),((char*)d)+i+dnum*tmp.d_reclen); /*copy tmp to array "d"(from kernal to user).*/
}
dnum++; /*fill the next element of array "d".*/
}
result = dnum*tmp.d_reclen; /*lenth of the array "d"*/
return result;
}
int sys_pipe2(int* fd, int flags){
if(flags == 0){
struct m_inode * inode;
struct file * f[2];
int fp[2];
int i,j;
j=0;
for(i=0;j<2 && i<NR_FILE;i++)
if (!file_table[i].f_count)
(f[j++]=i+file_table)->f_count++;
if (j==1)
f[0]->f_count=0;
if (j<2)
return -1;
j=0;
for(i=0;j<2 && i<NR_OPEN;i++)
if (!current->filp[i]) {
current->filp[ fp[j]=i ] = f[j];
j++;
}
if (j==1)
current->filp[fp[0]]=NULL;
if (j<2) {
f[0]->f_count=f[1]->f_count=0;
return -1;
}
if (!(inode=get_pipe_inode())) {
current->filp[fp[0]] =
current->filp[fp[1]] = NULL;
f[0]->f_count = f[1]->f_count = 0;
return -1;
}
f[0]->f_inode = f[1]->f_inode = inode;
f[0]->f_pos = f[1]->f_pos = 0;
f[0]->f_mode = 1; /* read */
f[1]->f_mode = 2; /* write */
put_fs_long(fp[0],0+fd);
put_fs_long(fp[1],1+fd);
return 0;
}
}
int sys_sleep(unsigned int seconds){
sys_signal(SIGALRM,SIG_IGN,NULL);
sys_alarm(seconds);
pause();
return 0;
}