diff --git a/library/GNUmakefile.68k b/library/GNUmakefile.68k index 90518a3..31487a1 100644 --- a/library/GNUmakefile.68k +++ b/library/GNUmakefile.68k @@ -1,5 +1,5 @@ # -# $Id: GNUmakefile.68k,v 1.98 2006-11-13 09:25:28 obarthel Exp $ +# $Id: GNUmakefile.68k,v 1.99 2006-11-15 09:17:04 obarthel Exp $ # # :ts=8 # @@ -189,6 +189,8 @@ C_LIB = \ stat_stat.o \ stat_umask.o \ stdio_asprintf.o \ + stdio_change_fd_action.o \ + stdio_change_fd_user_data.o \ stdio_clearerr.o \ stdio_dropiobreadbuffer.o \ stdio_duplicate_fd.o \ @@ -228,6 +230,7 @@ C_LIB = \ stdio_getchar.o \ stdio_getchar_unlocked.o \ stdio_gets.o \ + stdio_get_fd.o \ stdio_get_file_descriptor.o \ stdio_growfdtable.o \ stdio_growiobtable.o \ diff --git a/library/changes b/library/changes index 46eec2d..b9e9bea 100644 --- a/library/changes +++ b/library/changes @@ -1,3 +1,7 @@ +- Added functions which modify the callback function and the userdata pointer + stored in a low level unbuffered file/socket data structure. These function + perform the proper locking and are thus safe to use in a thread-safe environment. + - The low level unbuffered file/socket now has a public equivalent, which is defined (along with the typedefs and flags) in . Functions for tinkering with it are still to come. diff --git a/library/include/sys/clib2_io.h b/library/include/sys/clib2_io.h index 91149e8..53dcb63 100644 --- a/library/include/sys/clib2_io.h +++ b/library/include/sys/clib2_io.h @@ -1,5 +1,5 @@ /* - * $Id: clib2_io.h,v 1.1 2006-11-15 08:51:07 obarthel Exp $ + * $Id: clib2_io.h,v 1.2 2006-11-15 09:17:04 obarthel Exp $ * * :ts=4 * @@ -153,6 +153,27 @@ struct _fd /****************************************************************************/ +/* Obtain a pointer to the _fd data structure associated with a file + descriptor number. Note that this does not perform any locking, which + means that you have to be absolutely certain that the file will not be + closed while you are still looking at it. This function can return + NULL if the file descriptor you provided is not valid. */ +extern struct _fd * __get_fd(int file_descriptor); + +/* Replaces the action callback function and (optionally) returns the old + function pointer; returns 0 for success and -1 for failure if you + provided an invalid file descriptor. This function performs proper locking + and is thus safe to use in a thread-safe environment. */ +extern int __change_fd_action(int file_descriptor,_file_action_fd_t new_action,_file_action_fd_t * old_action_ptr); + +/* Replaces the user data pointer and (optionally) returns the old user + data pointer; returns 0 for success and -1 for failure if you + provided an invalid file descriptor. This function performs proper locking + and is thus safe to use in a thread-safe environment. */ +extern int __change_fd_user_data(int file_descriptor,void * new_user_data,void ** old_user_data_ptr); + +/****************************************************************************/ + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/library/libc.gmk b/library/libc.gmk index 527314e..290c009 100755 --- a/library/libc.gmk +++ b/library/libc.gmk @@ -1,5 +1,5 @@ # -# $Id: libc.gmk,v 1.2 2006-11-13 09:25:28 obarthel Exp $ +# $Id: libc.gmk,v 1.3 2006-11-15 09:17:04 obarthel Exp $ # # :ts=8 # @@ -90,6 +90,8 @@ C_LIB := \ stat_stat.o \ stat_umask.o \ stdio_asprintf.o \ + stdio_change_fd_action.o \ + stdio_change_fd_user_data.o \ stdio_clearerr.o \ stdio_dropiobreadbuffer.o \ stdio_duplicate_fd.o \ @@ -129,6 +131,7 @@ C_LIB := \ stdio_getchar.o \ stdio_getchar_unlocked.o \ stdio_gets.o \ + stdio_get_fd.o \ stdio_get_file_descriptor.o \ stdio_growfdtable.o \ stdio_growiobtable.o \ diff --git a/library/stdio_change_fd_action.c b/library/stdio_change_fd_action.c new file mode 100644 index 0000000..c4247fe --- /dev/null +++ b/library/stdio_change_fd_action.c @@ -0,0 +1,72 @@ +/* + * $Id: stdio_change_fd_action.c,v 1.1 2006-11-15 09:17:04 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2006 by Olaf Barthel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Neither the name of Olaf Barthel nor the names of contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _STDIO_HEADERS_H +#include "stdio_headers.h" +#endif /* _STDIO_HEADERS_H */ + +/****************************************************************************/ + +int +__change_fd_action( + int file_descriptor, + _file_action_fd_t new_action, + _file_action_fd_t * old_action_ptr) +{ + int result = -1; + struct fd * fd; + + if(old_action_ptr != NULL) + (*old_action_ptr) = NULL; + + __stdio_lock(); + + fd = __get_file_descriptor(file_descriptor); + if(fd != NULL) + { + __fd_lock(fd); + + if(old_action_ptr != NULL) + (*old_action_ptr) = (_file_action_fd_t)fd->fd_Action; + + fd->fd_Action = (file_action_fd_t)new_action; + + __fd_unlock(fd); + + result = 0; + } + + __stdio_unlock(); + + return(result); +} diff --git a/library/stdio_change_fd_user_data.c b/library/stdio_change_fd_user_data.c new file mode 100644 index 0000000..9ef611c --- /dev/null +++ b/library/stdio_change_fd_user_data.c @@ -0,0 +1,72 @@ +/* + * $Id: stdio_change_fd_user_data.c,v 1.1 2006-11-15 09:17:04 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2006 by Olaf Barthel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Neither the name of Olaf Barthel nor the names of contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _STDIO_HEADERS_H +#include "stdio_headers.h" +#endif /* _STDIO_HEADERS_H */ + +/****************************************************************************/ + +int +__change_fd_user_data( + int file_descriptor, + void * new_user_data, + void ** old_user_data_ptr) +{ + int result = -1; + struct fd * fd; + + if(old_user_data_ptr != NULL) + (*old_user_data_ptr) = NULL; + + __stdio_lock(); + + fd = __get_file_descriptor(file_descriptor); + if(fd != NULL) + { + __fd_lock(fd); + + if(old_user_data_ptr != NULL) + (*old_user_data_ptr) = fd->fd_UserData; + + fd->fd_UserData = new_user_data; + + __fd_unlock(fd); + + result = 0; + } + + __stdio_unlock(); + + return(result); +} diff --git a/library/stdio_get_fd.c b/library/stdio_get_fd.c new file mode 100644 index 0000000..6e0b540 --- /dev/null +++ b/library/stdio_get_fd.c @@ -0,0 +1,48 @@ +/* + * $Id: stdio_get_fd.c,v 1.1 2006-11-15 09:17:04 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2006 by Olaf Barthel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Neither the name of Olaf Barthel nor the names of contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _STDIO_HEADERS_H +#include "stdio_headers.h" +#endif /* _STDIO_HEADERS_H */ + +/****************************************************************************/ + +struct _fd * +__get_fd(int file_descriptor) +{ + struct _fd * result; + + result = (struct _fd *)__get_file_descriptor(file_descriptor); + + return(result); +}