source: trunk/kernel/syscalls/sys_isatty.c @ 457

Last change on this file since 457 was 457, checked in by alain, 3 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

File size: 3.4 KB
Line 
1/*
2 * sys_isatty.c - test if a file descriptor is a TXT device.
3 *
4 * Author     Alain Greiner (2016,2017,2018)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
25#include <hal_kernel_types.h>
26#include <hal_uspace.h>
27#include <hal_irqmask.h>
28#include <hal_special.h>
29#include <errno.h>
30#include <vfs.h>
31#include <vmm.h>
32#include <chdev.h>
33#include <thread.h>
34#include <printk.h>
35#include <process.h>
36
37////////////////////////////////////
38int sys_isatty( uint32_t   file_id )
39{
40        xptr_t    file_xp;     // extended pointer on remote file descriptor
41    xptr_t    chdev_xp;    // extended pointer on remote chdev
42    cxy_t     chdev_cxy;
43    chdev_t * chdev_ptr;
44    uint32_t  chdev_func;
45    int       retval;
46
47        thread_t  *  this    = CURRENT_THREAD;
48        process_t *  process = this->process;
49 
50#if DEBUG_SYS_ISATTY
51uint64_t tm_start;
52uint64_t tm_end;
53tm_start = hal_get_cycles();
54if( DEBUG_SYS_ISATTY < tm_start )
55printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n"
56__FUNCTION__ , this, process->pid, (uint32_t)tm_start );
57#endif
58
59    // check file_id argument
60        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
61        {
62
63#if DEBUG_SYSCALLS_ERROR
64printk("\n[ERROR] in %s : illegal file descriptor index = %d\n", __FUNCTION__ , file_id );
65#endif
66                this->errno = EBADFD;
67                return -1;
68        }
69
70    // get extended pointer on remote file descriptor
71    file_xp = process_fd_get_xptr( process , file_id );
72
73    if( file_xp == XPTR_NULL )
74    {
75
76#if DEBUG_SYSCALLS_ERROR
77printk("\n[ERROR] in %s : undefined fd_id %d in process %x\n",
78__FUNCTION__ , file_id , process->pid );
79#endif
80        this->errno = EBADFD;
81        return -1;
82    }
83
84    // get file descriptor cluster and local pointer
85    vfs_file_t * file_ptr = GET_PTR( file_xp );
86    cxy_t        file_cxy = GET_CXY( file_xp );
87
88    // get file type
89    vfs_inode_type_t type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
90
91    // action depend on file type
92    if( type != INODE_TYPE_DEV )      // not a device
93    {
94        retval = 0;
95        }
96    else                              // it is a device
97    {
98        // get cluster and pointers on chdev
99        chdev_xp  = chdev_from_file( file_xp );
100        chdev_cxy = GET_CXY( chdev_xp );
101        chdev_ptr = GET_PTR( chdev_xp );
102
103        // get chdev type
104        chdev_func = hal_remote_lw( XPTR( chdev_cxy , &chdev_ptr->func ) );
105
106        if( chdev_func == DEV_FUNC_TXT ) retval = 1;
107        else                               retval = 0;
108    }
109
110#if DEBUG_SYS_ISATTY
111tm_end = hal_get_cycles();
112if( DEBUG_SYS_ISATTY < tm_end )
113printk("\n[DBG] %s : thread %x exit / process %x / cost %d / cycle %d\n",
114__FUNCTION__, this, process->pid, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
115#endif
116
117        return retval;
118
119}  // end sys_read()
Note: See TracBrowser for help on using the repository browser.