add lseek (SEEK_SET, SEEK_CUR, SEEK_END)

- still missing some filetype checking
This commit is contained in:
Marian Ohligs 2011-05-18 18:14:23 +02:00
parent 1f74434a46
commit 9e5dcfc2b4
4 changed files with 38 additions and 5 deletions

View file

@ -23,6 +23,16 @@
* @brief Stringstream related functions. Mainly printf-stuff.
*/
#ifndef SEEK_SET
#define SEEK_SET 0 /* set file offset to offset */
#endif
#ifndef SEEK_CUR
#define SEEK_CUR 1 /* set file offset to current plus offset */
#endif
#ifndef SEEK_END
#define SEEK_END 2 /* set file offset to EOF plus offset */
#endif
#ifndef __STDIO_H__
#define __STDIO_H__

View file

@ -29,7 +29,7 @@ static int sys_write(int fd, const char *buf, size_t len)
{
unsigned int wrotebytes;
wrotebytes = write_fs(per_core(current_task)->fildes_table[fd].node, (uint8_t*)buf, len, per_core(current_task)->fildes_table[fd].offset);
kprintf("writing into filesystem -- fd:%i, Filelength:%i, Writtenbytes: %i, Bufferlength: %s \n", fd, len, wrotebytes, buf);
//kprintf("writing into filesystem -- fd:%i, Filelength:%i, Writtenbytes: %i, Bufferlength: %s \n", fd, len, wrotebytes, buf);
per_core(current_task)->fildes_table[fd].offset += wrotebytes;
return wrotebytes;
@ -74,8 +74,31 @@ static int sys_read(int fd, const char *buf, size_t len)
static int sys_lseek(int fd, off_t pos, int origin)
{
kprintf("lseek used, but not ready yet");
return 0;
int ret = -EINVAL;
/* Beware: still not checking the filetype & size*/
switch(origin)
{
case SEEK_SET: { /* set file offset to offset */
per_core(current_task)->fildes_table[fd].offset = pos;
ret = 0;
break;
}
case SEEK_CUR: { /* set file offset to current plus offset */
ret = pos + per_core(current_task)->fildes_table[fd].offset;
break;
}
case SEEK_END: { /* set file offset to EOF plus offset */
per_core(current_task)->fildes_table[fd].offset = pos + per_core(current_task)->fildes_table[fd].node->block_size;
ret = 0;
break;
}
default:
ret = -EINVAL;
break;
}
return ret;
}
static int sys_sbrk(int incr)
@ -150,7 +173,6 @@ int syscall_handler(uint32_t sys_nr, ...)
off_t pos = va_arg(vl, off_t);
int origin = va_arg(vl, int);
ret = sys_lseek(fd, pos, origin);
kprintf("hallo!!! %i", ret);
break;
}
case __NR_sbrk: {

View file

@ -35,6 +35,7 @@ int main(int argc, char** argv)
setbuf(testfile, NULL);
fflush(NULL);
fwrite("wsx", 3, 1, testfile);
fseek(testfile, 2, SEEK_CUR);
fwrite("nextnextnext", 1, 10, testfile);
fclose(testfile);

View file

@ -1 +1 @@
d
123456789