source: trunk/kernel/syscalls/sys_lseek.c @ 198

Last change on this file since 198 was 23, checked in by alain, 8 years ago

Introduce syscalls.

File size: 2.3 KB
Line 
1/*
2 * kern/sys_lseek.c - set the read/write offset of an opened file
3 *
4 * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
5 * Copyright (c) 2011,2012 UPMC Sorbonne Universites
6 *
7 * This file is part of ALMOS-kernel.
8 *
9 * ALMOS-kernel is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2.0 of the License.
12 *
13 * ALMOS-kernel is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with ALMOS-kernel; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <kernel_config.h>
24#include <hal_types.h>
25#include <hal_uspace.h>
26#include <errno.h>
27#include <vfs.h>
28#include <vmm.h>
29#include <thread.h>
30#include <printk.h>
31#include <process.h>
32
33////////////////////////////////
34int sys_lseek (uint32_t file_id,
35               uint32_t offset,
36               uint32_t whence )
37{
38        error_t    error;
39    xptr_t     file_xp;
40    uint32_t   new_offset;
41
42        thread_t  * this    = CURRENT_THREAD;
43        process_t * process = this->process;
44
45    // check file_id argument
46        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
47        {
48        printk("\n[ERROR] in %s : illegal file descriptor index = %d\n",
49               __FUNCTION__ , file_id );
50                this->errno = EBADFD;
51                return -1;
52        }
53
54    // get extended pointer on remote file descriptor
55    file_xp = process_fd_get_xptr( process , file_id );
56
57    if( file_xp == XPTR_NULL )
58    {
59        printk("\n[ERROR] in %s : undefined file descriptor index = %d\n",
60               __FUNCTION__ , file_id );
61                this->errno = EBADFD;
62                return -1;
63    }
64
65        /* FIXME: file may be closed in parallel
66         * of seek/read/write/mmap ..etc
67         * so file may be NULL or invalid */
68
69    // call relevant VFS function
70        error = vfs_lseek( file_xp , offset , whence , &new_offset );
71
72        if( error )
73        {
74        printk("\n[ERROR] in %s : cannot seek file = %d\n",
75               __FUNCTION__ , file_id );
76                this->errno = error;
77                return -1;
78        }
79 
80        return new_offset;
81}
Note: See TracBrowser for help on using the repository browser.