Fork me on GitHub

root/kernel/syscalls/lseek.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sys_lseek

   1 /*
   2  * fiwix/kernel/syscalls/lseek.c
   3  *
   4  * Copyright 2018, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/types.h>
   9 #include <fiwix/syscalls.h>
  10 #include <fiwix/fs.h>
  11 #include <fiwix/errno.h>
  12 
  13 #ifdef __DEBUG__
  14 #include <fiwix/stdio.h>
  15 #include <fiwix/process.h>
  16 #endif /*__DEBUG__ */
  17 
  18 int sys_lseek(unsigned int ufd, __off_t offset, unsigned int whence)
  19 {
  20         struct inode *i;
  21         __off_t new_offset;
  22 
  23 #ifdef __DEBUG__
  24         printk("(pid %d) sys_lseek(%d, %d, %d)", current->pid, ufd, offset, whence);
  25 #endif /*__DEBUG__ */
  26 
  27         CHECK_UFD(ufd);
  28 
  29         i = fd_table[current->fd[ufd]].inode;
  30         switch(whence) {
  31                 case SEEK_SET:
  32                         new_offset = offset;
  33                         break;
  34                 case SEEK_CUR:
  35                         new_offset = fd_table[current->fd[ufd]].offset + offset;
  36                         break;
  37                 case SEEK_END:
  38                         new_offset = i->i_size + offset;
  39                         break;
  40                 default:
  41                         return -EINVAL;
  42         }
  43         if((int)new_offset < 0) {
  44                 return -EINVAL;
  45         }
  46         if(i->fsop && i->fsop->lseek) {
  47                 fd_table[current->fd[ufd]].offset = new_offset;
  48                 new_offset = i->fsop->lseek(i, new_offset);
  49         } else {
  50                 return -EPERM;
  51         }
  52 
  53 #ifdef __DEBUG__
  54         printk(" -> returning %d\n", new_offset);
  55 #endif /*__DEBUG__ */
  56 
  57         return new_offset;
  58 }

/* [previous][next][first][last][top][bottom][index][help] */