openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

fdl.c (3459B)


      1 /* $Id: //depot/blt/lib/libposix/fdl.c#1 $
      2 **
      3 ** Copyright 1999 Sidney Cammeresi
      4 ** All rights reserved.
      5 **
      6 ** Redistribution and use in source and binary forms, with or without
      7 ** modification, are permitted provided that the following conditions
      8 ** are met:
      9 ** 1. Redistributions of source code must retain the above copyright
     10 **    notice, this list of conditions, and the following disclaimer.
     11 ** 2. Redistributions in binary form must reproduce the above copyright
     12 **    notice, this list of conditions, and the following disclaimer in the
     13 **    documentation and/or other materials provided with the distribution.
     14 ** 3. The name of the author may not be used to endorse or promote products
     15 **    derived from this software without specific prior written permission.
     16 **
     17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 */
     28 
     29 #include <stdlib.h>
     30 #include <errno.h>
     31 #include <unistd.h>
     32 #include <sys/ioctl.h>
     33 #include <blt/qsem.h>
     34 #include <blt/libsyms.h>
     35 #include <blt/fdl.h>
     36 #include <blt/os.h>
     37 
     38 static __filedesc **fd_table;
     39 static qsem_t *fd_table_lock;
     40 
     41 weak_alias (_read, read)
     42 weak_alias (_write, write)
     43 weak_alias (_ioctl, ioctl)
     44 weak_alias (_close, close)
     45 
     46 void __libc_init_fdl (void)
     47 {
     48 	int i;
     49 
     50 	fd_table = malloc (sizeof (__filedesc *) * MAX_FDS);
     51 	for (i = 0; i < MAX_FDS; i++)
     52 		fd_table[i] = NULL;
     53 	fd_table_lock = qsem_create (1);
     54 }
     55 
     56 init_info __init_posix_fdl = {
     57 	&__libc_init_fdl,
     58 	1
     59 };
     60 
     61 void __libc_fini_fdl (void)
     62 {
     63 	free (fd_table);
     64 	qsem_destroy (fd_table_lock);
     65 }
     66 
     67 int _fdl_alloc_descriptor (fdl_type *handler, void *cookie)
     68 {
     69 	int i;
     70 
     71 	qsem_acquire (fd_table_lock);
     72 	for (i = 0; i < MAX_FDS; i++)
     73 		if (fd_table[i] == NULL)
     74 		{
     75 			fd_table[i] = malloc (sizeof (__filedesc));
     76 			fd_table[i]->imp = handler;
     77 			fd_table[i]->cookie = cookie;
     78 			qsem_release (fd_table_lock);
     79 			return i;
     80 		}
     81 	qsem_release (fd_table_lock);
     82 	return -1;
     83 }
     84 
     85 void _fdl_free_descriptor (int desc)
     86 {
     87 	fd_table[desc] = NULL;
     88 }
     89 
     90 ssize_t _read (int fd, void *buf, size_t count)
     91 {
     92 	if (fd_table[fd]->imp->read != NULL)
     93 		return fd_table[fd]->imp->read (fd_table[fd]->cookie, buf, count);
     94 	else
     95 	{
     96 		errno = ENOSYS;
     97 		return -1;
     98 	}
     99 }
    100 
    101 ssize_t _write (int fd, const void *buf, size_t count)
    102 {
    103 	if (fd_table[fd]->imp->write != NULL)
    104 		return fd_table[fd]->imp->write (fd_table[fd]->cookie, buf, count);
    105 	else
    106 	{
    107 		errno = ENOSYS;
    108 		return -1;
    109 	}
    110 }
    111 
    112 int _ioctl (int fd, unsigned long request, char *argp)
    113 {
    114 	if (fd_table[fd]->imp->ioctl != NULL)
    115 		return fd_table[fd]->imp->ioctl (fd_table[fd]->cookie, request, argp);
    116 	else
    117 	{
    118 		errno = ENOSYS;
    119 		return -1;
    120 	}
    121 }
    122 
    123 int _close (int fd)
    124 {
    125 	int res;
    126 
    127 	if (fd_table[fd]->imp->close != NULL)
    128 	{
    129 		res = fd_table[fd]->imp->close (fd_table[fd]->cookie);
    130 		fd_table[fd] = NULL;
    131 		return res;
    132 	}
    133 	else
    134 	{
    135 		errno = ENOSYS;
    136 		return -1;
    137 	}
    138 }
    139