From c0c70bfd993f9ece0b9dc5f1e849b85cd4bf8b7a Mon Sep 17 00:00:00 2001 From: Olaf Barthel Date: Sun, 27 Feb 2005 18:09:12 +0000 Subject: [PATCH] - Added stdio thread locking functions flockfile(), funlockfile(), and ftrylockfile(). - Modified the internal FILE structure to allow for thread locking. Note that this again requires that the library is rebuilt! - Added or modified macros for getc_unlocked(), getchar_unlocked(), putc_unlocked() and putchar_unlocked(). - Added rand_r(). - Added flockfile()/funlockfile() wrappers around all stdio functions. git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@14841 87f5fb63-7c3d-0410-a384-fd976d0f7a62 --- library/GNUmakefile.68k | 29 ++++++---- library/GNUmakefile.os4 | 29 ++++++---- library/changes | 14 +++++ library/include/stdio.h | 55 ++++++++++++++++--- library/include/stdlib.h | 6 ++- library/smakefile | 19 +++++-- library/stdio_clearerr.c | 12 +++-- library/stdio_fclose.c | 7 ++- library/stdio_feof.c | 12 +++-- library/stdio_ferror.c | 12 +++-- library/stdio_fflush.c | 6 ++- library/stdio_fgetc.c | 12 +++-- library/stdio_fgetpos.c | 12 +++-- library/stdio_fgets.c | 12 +++-- library/stdio_flockfile.c | 89 ++++++++++++++++++++++++++++++ library/stdio_flush.c | 12 +++-- library/stdio_fopen.c | 8 +-- library/stdio_fprintf.c | 12 +++-- library/stdio_fputc.c | 12 +++-- library/stdio_fputs.c | 10 ++-- library/stdio_fread.c | 12 +++-- library/stdio_fscanf.c | 12 +++-- library/stdio_fseek.c | 12 +++-- library/stdio_fsetpos.c | 12 +++-- library/stdio_ftell.c | 12 +++-- library/stdio_ftrylockfile.c | 93 ++++++++++++++++++++++++++++++++ library/stdio_funlockfile.c | 89 ++++++++++++++++++++++++++++++ library/stdio_fwrite.c | 12 +++-- library/stdio_getc_unlocked.c | 72 +++++++++++++++++++++++++ library/stdio_getchar_unlocked.c | 72 +++++++++++++++++++++++++ library/stdio_gets.c | 12 +++-- library/stdio_headers.h | 4 +- library/stdio_init_exit.c | 14 ++++- library/stdio_initializeiob.c | 20 +++---- library/stdio_openiob.c | 14 ++++- library/stdio_protos.h | 4 +- library/stdio_putc_unlocked.c | 72 +++++++++++++++++++++++++ library/stdio_putchar_unlocked.c | 72 +++++++++++++++++++++++++ library/stdio_puts.c | 12 +++-- library/stdio_rewind.c | 6 ++- library/stdio_setbuf.c | 8 +-- library/stdio_setvbuf.c | 12 +++-- library/stdio_sscanf.c | 5 +- library/stdio_ungetc.c | 12 +++-- library/stdio_unlockfile.c | 46 ++++++++++++++++ library/stdio_vasprintf.c | 5 +- library/stdio_vfprintf.c | 9 +++- library/stdio_vfscanf.c | 9 +++- library/stdio_vsnprintf.c | 5 +- library/stdio_vsprintf.c | 5 +- library/stdlib_headers.h | 4 +- library/stdlib_rand.c | 28 ++-------- library/stdlib_rand_r.c | 67 +++++++++++++++++++++++ library/stdlib_srand.c | 5 +- library/unistd_fileno.c | 12 +++-- 55 files changed, 1060 insertions(+), 180 deletions(-) create mode 100644 library/stdio_flockfile.c create mode 100644 library/stdio_ftrylockfile.c create mode 100644 library/stdio_funlockfile.c create mode 100644 library/stdio_getc_unlocked.c create mode 100644 library/stdio_getchar_unlocked.c create mode 100644 library/stdio_putc_unlocked.c create mode 100644 library/stdio_putchar_unlocked.c create mode 100644 library/stdio_unlockfile.c create mode 100644 library/stdlib_rand_r.c diff --git a/library/GNUmakefile.68k b/library/GNUmakefile.68k index 67bf272..88d2e52 100644 --- a/library/GNUmakefile.68k +++ b/library/GNUmakefile.68k @@ -1,5 +1,5 @@ # -# $Id: GNUmakefile.68k,v 1.29 2005-02-25 10:14:20 obarthel Exp $ +# $Id: GNUmakefile.68k,v 1.30 2005-02-27 18:09:07 obarthel Exp $ # # :ts=8 # @@ -130,23 +130,23 @@ C_LIB = \ ctype_isspace.o \ ctype_isupper.o \ ctype_isxdigit.o \ + ctype_table.o \ ctype_tolower.o \ ctype_toupper.o \ - ctype_table.o \ dirent_closedir.o \ dirent_data.o \ - dirent_rewinddir.o \ dirent_opendir.o \ dirent_readdir.o \ + dirent_rewinddir.o \ errno_data.o \ fcntl_close.o \ fcntl_creat.o \ fcntl_fcntl.o \ + fcntl_get_default_file.o \ fcntl_lseek.o \ fcntl_open.o \ fcntl_read.o \ fcntl_write.o \ - fcntl_get_default_file.o \ libgen_basename.o \ libgen_dirname.o \ locale_init_exit.o \ @@ -188,13 +188,14 @@ C_LIB = \ stdio_feof.o \ stdio_ferror.o \ stdio_fflush.o \ - stdio_flush.o \ stdio_fgetc.o \ stdio_fgetpos.o \ stdio_fgets.o \ stdio_filliobreadbuffer.o \ stdio_findvacantfdentry.o \ stdio_findvacantiobentry.o \ + stdio_flockfile.o \ + stdio_flush.o \ stdio_flushiobwritebuffer.o \ stdio_fopen.o \ stdio_fprintf.o \ @@ -206,11 +207,15 @@ C_LIB = \ stdio_fseek.o \ stdio_fsetpos.o \ stdio_ftell.o \ + stdio_ftrylockfile.o \ + stdio_funlockfile.o \ stdio_fwrite.o \ stdio_getc.o \ + stdio_getc_unlocked.o \ stdio_getchar.o \ - stdio_get_file_descriptor.o \ + stdio_getchar_unlocked.o \ stdio_gets.o \ + stdio_get_file_descriptor.o \ stdio_growfdtable.o \ stdio_growiobtable.o \ stdio_grow_file.o \ @@ -226,7 +231,9 @@ C_LIB = \ stdio_popen.o \ stdio_printf.o \ stdio_putc.o \ + stdio_putc_unlocked.o \ stdio_putchar.o \ + stdio_putchar_unlocked.o \ stdio_puts.o \ stdio_remove.o \ stdio_remove_fd_alias.o \ @@ -243,6 +250,7 @@ C_LIB = \ stdio_tmpnam.o \ stdio_translateioerror.o \ stdio_ungetc.o \ + stdio_unlockfile.o \ stdio_vasprintf.o \ stdio_vasprintf_hook_entry.o \ stdio_vfprintf.o \ @@ -277,10 +285,10 @@ C_LIB = \ stdlib_dosbase.o \ stdlib_exit.o \ stdlib_free.o \ - stdlib_get_errno.o \ stdlib_getdefstacksize.o \ stdlib_getenv.o \ stdlib_getsp.o \ + stdlib_get_errno.o \ stdlib_init_exit.o \ stdlib_isresident.o \ stdlib_labs.o \ @@ -301,11 +309,12 @@ C_LIB = \ stdlib_putenv.o \ stdlib_qsort.o \ stdlib_rand.o \ + stdlib_rand_r.o \ stdlib_realloc.o \ stdlib_red_black.o \ stdlib_setenv.o \ - stdlib_set_errno.o \ stdlib_setjmp.o \ + stdlib_set_errno.o \ stdlib_set_process_window.o \ stdlib_shell_escape.o \ stdlib_showerror.o \ @@ -318,9 +327,10 @@ C_LIB = \ stdlib_stacksize.o \ stdlib_stack_usage.o \ stdlib_startup.o \ + stdlib_stdio_window_spec.o \ stdlib_strtol.o \ - stdlib_strtoul.o \ stdlib_strtoll.o \ + stdlib_strtoul.o \ stdlib_strtoull.o \ stdlib_swapstack.o \ stdlib_sysbase.o \ @@ -332,7 +342,6 @@ C_LIB = \ stdlib_umodsi3.o \ stdlib_unsetenv.o \ stdlib_wildcard_expand.o \ - stdlib_stdio_window_spec.o \ strings_strcasecmp.o \ strings_strncasecmp.o \ string_bcmp.o \ diff --git a/library/GNUmakefile.os4 b/library/GNUmakefile.os4 index 0090e73..06cdbc7 100644 --- a/library/GNUmakefile.os4 +++ b/library/GNUmakefile.os4 @@ -1,5 +1,5 @@ # -# $Id: GNUmakefile.os4,v 1.29 2005-02-25 10:14:20 obarthel Exp $ +# $Id: GNUmakefile.os4,v 1.30 2005-02-27 18:09:10 obarthel Exp $ # # :ts=8 # @@ -134,23 +134,23 @@ C_LIB = \ ctype_isspace.o \ ctype_isupper.o \ ctype_isxdigit.o \ + ctype_table.o \ ctype_tolower.o \ ctype_toupper.o \ - ctype_table.o \ dirent_closedir.o \ dirent_data.o \ - dirent_rewinddir.o \ dirent_opendir.o \ dirent_readdir.o \ + dirent_rewinddir.o \ errno_data.o \ fcntl_close.o \ fcntl_creat.o \ fcntl_fcntl.o \ + fcntl_get_default_file.o \ fcntl_lseek.o \ fcntl_open.o \ fcntl_read.o \ fcntl_write.o \ - fcntl_get_default_file.o \ libgen_basename.o \ libgen_dirname.o \ locale_init_exit.o \ @@ -192,13 +192,14 @@ C_LIB = \ stdio_feof.o \ stdio_ferror.o \ stdio_fflush.o \ - stdio_flush.o \ stdio_fgetc.o \ stdio_fgetpos.o \ stdio_fgets.o \ stdio_filliobreadbuffer.o \ stdio_findvacantfdentry.o \ stdio_findvacantiobentry.o \ + stdio_flockfile.o \ + stdio_flush.o \ stdio_flushiobwritebuffer.o \ stdio_fopen.o \ stdio_fprintf.o \ @@ -210,11 +211,15 @@ C_LIB = \ stdio_fseek.o \ stdio_fsetpos.o \ stdio_ftell.o \ + stdio_ftrylockfile.o \ + stdio_funlockfile.o \ stdio_fwrite.o \ stdio_getc.o \ + stdio_getc_unlocked.o \ stdio_getchar.o \ - stdio_get_file_descriptor.o \ + stdio_getchar_unlocked.o \ stdio_gets.o \ + stdio_get_file_descriptor.o \ stdio_growfdtable.o \ stdio_growiobtable.o \ stdio_grow_file.o \ @@ -230,7 +235,9 @@ C_LIB = \ stdio_popen.o \ stdio_printf.o \ stdio_putc.o \ + stdio_putc_unlocked.o \ stdio_putchar.o \ + stdio_putchar_unlocked.o \ stdio_puts.o \ stdio_remove.o \ stdio_remove_fd_alias.o \ @@ -247,6 +254,7 @@ C_LIB = \ stdio_tmpnam.o \ stdio_translateioerror.o \ stdio_ungetc.o \ + stdio_unlockfile.o \ stdio_vasprintf.o \ stdio_vasprintf_hook_entry.o \ stdio_vfprintf.o \ @@ -282,9 +290,9 @@ C_LIB = \ stdlib_exit.o \ stdlib_free.o \ stdlib_getdefstacksize.o \ - stdlib_get_errno.o \ stdlib_getenv.o \ stdlib_getsp.o \ + stdlib_get_errno.o \ stdlib_init_exit.o \ stdlib_isresident.o \ stdlib_labs.o \ @@ -305,11 +313,12 @@ C_LIB = \ stdlib_putenv.o \ stdlib_qsort.o \ stdlib_rand.o \ + stdlib_rand_r.o \ stdlib_realloc.o \ stdlib_red_black.o \ - stdlib_set_errno.o \ stdlib_setenv.o \ stdlib_setjmp.o \ + stdlib_set_errno.o \ stdlib_set_process_window.o \ stdlib_shell_escape.o \ stdlib_showerror.o \ @@ -322,9 +331,10 @@ C_LIB = \ stdlib_stacksize.o \ stdlib_stack_usage.o \ stdlib_startup.o \ + stdlib_stdio_window_spec.o \ stdlib_strtol.o \ - stdlib_strtoul.o \ stdlib_strtoll.o \ + stdlib_strtoul.o \ stdlib_strtoull.o \ stdlib_swapstack.o \ stdlib_sysbase.o \ @@ -336,7 +346,6 @@ C_LIB = \ stdlib_umodsi3.o \ stdlib_unsetenv.o \ stdlib_wildcard_expand.o \ - stdlib_stdio_window_spec.o \ strings_strcasecmp.o \ strings_strncasecmp.o \ string_bcmp.o \ diff --git a/library/changes b/library/changes index 911cc24..7eabf62 100644 --- a/library/changes +++ b/library/changes @@ -48,6 +48,20 @@ qualifier. This was done in preparation for changes that will deal with global and local data and the issue of thread safety. +- Added stdio thread locking functions flockfile(), funlockfile(), + and ftrylockfile(). + +- Modified the internal FILE structure to allow for thread locking. + Note that this again requires that the library is rebuilt! + +- Added or modified macros for getc_unlocked(), getchar_unlocked(), + putc_unlocked() and putchar_unlocked(). + +- Added rand_r(). + +- Added flockfile()/funlockfile() wrappers around all stdio + functions. + c.lib 1.188 (7.2.2005) diff --git a/library/include/stdio.h b/library/include/stdio.h index d086592..8459371 100644 --- a/library/include/stdio.h +++ b/library/include/stdio.h @@ -1,5 +1,5 @@ /* - * $Id: stdio.h,v 1.5 2005-01-02 09:07:21 obarthel Exp $ + * $Id: stdio.h,v 1.6 2005-02-27 18:09:12 obarthel Exp $ * * :ts=4 * @@ -200,6 +200,21 @@ extern int putchar(int c); /****************************************************************************/ +/* The following four functions are not part of ISO 'C' (1994). */ + +/****************************************************************************/ + +extern int getc_unlocked(FILE *stream); +extern int getchar_unlocked(void); +extern int putc_unlocked(int c,FILE *stream); +extern int putchar_unlocked(int c); + +/****************************************************************************/ + +/* ISO 'C' (1994) functions continue below. */ + +/****************************************************************************/ + extern char *fgets(char *s,int n,FILE *stream); extern char *gets(char *s); @@ -252,30 +267,40 @@ extern char *tmpnam(char *buf); /* * A special buffer flush routine which returns the last character written * in case of success and EOF in case of failure. This is used by the - * putc() macro defined below. + * putc_unlocked() macro defined below. */ extern int __flush(FILE *stream); /****************************************************************************/ +/* + * A special function which returns the input character. This is used by + * the getc() macro defined below. + */ +extern int __unlockfile(FILE *stream,int c); + +/****************************************************************************/ + /* * fgetc() implemented as a "simple" macro; note that fgetc() does much more than * can be conveniently expressed as a macro! */ -#define getc(f) \ +#define __getc_unlocked(f) \ (((((FILE *)(f))->flags & (__FILE_IN_USE|__FILE_READABLE|__FILE_EOF)) == (__FILE_IN_USE|__FILE_READABLE) && \ (((FILE *)(f))->flags & __FILE_BUFFER_MASK) != _IONBF && \ ((FILE *)(f))->position < ((FILE *)(f))->num_read_bytes) ? \ ((FILE *)(f))->buffer[((FILE *)(f))->position++] : \ fgetc(f)) +#define getc_unlocked(f) + /****************************************************************************/ /* * fputc() implemented as a "simple" macro; note that fputc() does much more than * can be conveniently expressed as a macro! */ -#define putc(c,f) \ +#define __putc_unlocked(c,f) \ (((((FILE *)(f))->flags & (__FILE_IN_USE|__FILE_WRITABLE)) == (__FILE_IN_USE|__FILE_WRITABLE) && \ (((FILE *)(f))->flags & __FILE_BUFFER_MASK) != _IONBF && \ (((FILE *)(f))->num_write_bytes < ((FILE *)(f))->size)) ? \ @@ -286,6 +311,20 @@ extern int __flush(FILE *stream); (((FILE *)(f))->buffer[((FILE *)(f))->num_write_bytes-1]))) : \ fputc((c),(f))) +#define putc_unlocked(c,f) __putc((c),(f)) + +/****************************************************************************/ + +#define getchar_unlocked() __getc_unlocked(stdin) +#define putchar_unlocked(c) __putc_unlocked((c),stdout) + +/****************************************************************************/ + +#define getc(f) (flockfile(f), __unlockfile((f),__getc_unlocked(f))) +#define putc(c,f) (flockfile(f), __unlockfile((f),__putc_unlocked((c),(f)))) +#define getchar() getc(stdin) +#define putchar(c) putc((c),stdout) + /****************************************************************************/ /* @@ -296,8 +335,6 @@ extern int __flush(FILE *stream); /****************************************************************************/ -#define getchar() getc(stdin) -#define putchar(c) putc((c),stdout) #define clearerr(file) ((void)((file)->flags &= ~(__FILE_EOF|__FILE_ERROR))) #define feof(file) (((file)->flags & __FILE_EOF) != 0) #define ferror(file) (((file)->flags & __FILE_ERROR) != 0) @@ -326,6 +363,12 @@ extern FILE * popen(const char *command, const char *type); /****************************************************************************/ +extern void flockfile(FILE * file); +extern void funlockfile(FILE * file); +extern int ftrylockfile(FILE * file); + +/****************************************************************************/ + extern int vasprintf(char **ret,const char *format,va_list arg); #ifdef __MEM_DEBUG diff --git a/library/include/stdlib.h b/library/include/stdlib.h index 5df1ffb..abe5945 100644 --- a/library/include/stdlib.h +++ b/library/include/stdlib.h @@ -1,5 +1,5 @@ /* - * $Id: stdlib.h,v 1.6 2005-01-30 09:48:06 obarthel Exp $ + * $Id: stdlib.h,v 1.7 2005-02-27 18:09:12 obarthel Exp $ * * :ts=4 * @@ -149,6 +149,10 @@ extern long atol(const char *str); /****************************************************************************/ +extern int rand_r(unsigned int * seed); + +/****************************************************************************/ + /* These two functions are unavailable under SAS/C for lack of a "long long" data type. */ #if ! defined(__SASC) diff --git a/library/smakefile b/library/smakefile index d836b12..34c0f3e 100644 --- a/library/smakefile +++ b/library/smakefile @@ -1,5 +1,5 @@ _# -# $Id: smakefile,v 1.23 2005-02-25 10:14:21 obarthel Exp $ +# $Id: smakefile,v 1.24 2005-02-27 18:09:10 obarthel Exp $ # # :ts=8 # @@ -292,18 +292,17 @@ STDIO_OBJ = \ stdio_examine_fh.o \ stdio_fclose.o \ stdio_fdhookentry.o \ - stdio_record_locking.o \ - stdio_remove_fd_alias.o \ stdio_feof.o \ stdio_ferror.o \ stdio_fflush.o \ - stdio_flush.o \ stdio_fgetc.o \ stdio_fgetpos.o \ stdio_fgets.o \ stdio_filliobreadbuffer.o \ stdio_findvacantfdentry.o \ stdio_findvacantiobentry.o \ + stdio_flockfile.o \ + stdio_flush.o \ stdio_flushiobwritebuffer.o \ stdio_fopen.o \ stdio_fprintf.o \ @@ -315,11 +314,15 @@ STDIO_OBJ = \ stdio_fseek.o \ stdio_fsetpos.o \ stdio_ftell.o \ + stdio_ftrylockfile.o \ + stdio_funlockfile.o \ stdio_fwrite.o \ stdio_getc.o \ + stdio_getc_unlocked.o \ stdio_getchar.o \ - stdio_get_file_descriptor.o \ + stdio_getchar_unlocked.o \ stdio_gets.o \ + stdio_get_file_descriptor.o \ stdio_growfdtable.o \ stdio_growiobtable.o \ stdio_grow_file.o \ @@ -335,9 +338,13 @@ STDIO_OBJ = \ stdio_popen.o \ stdio_printf.o \ stdio_putc.o \ + stdio_putc_unlocked.o \ stdio_putchar.o \ + stdio_putchar_unlocked.o \ stdio_puts.o \ + stdio_record_locking.o \ stdio_remove.o \ + stdio_remove_fd_alias.o \ stdio_rename.o \ stdio_rewind.o \ stdio_scanf.o \ @@ -351,6 +358,7 @@ STDIO_OBJ = \ stdio_tmpnam.o \ stdio_translateioerror.o \ stdio_ungetc.o \ + stdio_unlockfile.o \ stdio_vasprintf.o \ stdio_vasprintf_hook_entry.o \ stdio_vfprintf.o \ @@ -412,6 +420,7 @@ STDLIB_OBJ = \ stdlib_putenv.o \ stdlib_qsort.o \ stdlib_rand.o \ + stdlib_rand_r.o \ stdlib_realloc.o \ stdlib_red_black.o \ stdlib_setenv.o \ diff --git a/library/stdio_clearerr.c b/library/stdio_clearerr.c index 6ee3ed3..78abfd3 100644 --- a/library/stdio_clearerr.c +++ b/library/stdio_clearerr.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_clearerr.c,v 1.3 2005-02-03 16:56:15 obarthel Exp $ + * $Id: stdio_clearerr.c,v 1.4 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -58,6 +58,9 @@ clearerr(FILE *stream) assert( stream != NULL ); + if(__check_abort_enabled) + __check_abort(); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL) @@ -68,14 +71,15 @@ clearerr(FILE *stream) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( __is_valid_iob(file) ); + flockfile(stream); + CLEAR_FLAG(file->iob_Flags,IOBF_ERROR); CLEAR_FLAG(file->iob_Flags,IOBF_EOF_REACHED); + funlockfile(stream); + out: LEAVE(); diff --git a/library/stdio_fclose.c b/library/stdio_fclose.c index cdd3336..e251f0d 100644 --- a/library/stdio_fclose.c +++ b/library/stdio_fclose.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fclose.c,v 1.5 2005-02-21 10:21:44 obarthel Exp $ + * $Id: stdio_fclose.c,v 1.6 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -62,6 +62,8 @@ fclose(FILE *stream) assert( stream != NULL ); + assert( file->iob_Lock == NULL || file->iob_Lock->ss_Owner == NULL ); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL) @@ -139,6 +141,9 @@ fclose(FILE *stream) if(file->iob_CustomBuffer != NULL) free(file->iob_CustomBuffer); + /* Free the lock semaphore now. */ + FreeVec(file->iob_Lock); + memset(file,0,sizeof(*file)); out: diff --git a/library/stdio_feof.c b/library/stdio_feof.c index c85bfa6..80a17fb 100644 --- a/library/stdio_feof.c +++ b/library/stdio_feof.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_feof.c,v 1.3 2005-02-03 16:56:16 obarthel Exp $ + * $Id: stdio_feof.c,v 1.4 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -59,6 +59,9 @@ feof(FILE *stream) assert( stream != NULL ); + if(__check_abort_enabled) + __check_abort(); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL) @@ -74,14 +77,15 @@ feof(FILE *stream) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( __is_valid_iob(file) ); assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); + flockfile(stream); + result = FLAG_IS_SET(file->iob_Flags,IOBF_EOF_REACHED); + funlockfile(stream); + out: RETURN(result); diff --git a/library/stdio_ferror.c b/library/stdio_ferror.c index 1b3baac..a4c0694 100644 --- a/library/stdio_ferror.c +++ b/library/stdio_ferror.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_ferror.c,v 1.3 2005-02-03 16:56:16 obarthel Exp $ + * $Id: stdio_ferror.c,v 1.4 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -59,6 +59,9 @@ ferror(FILE *stream) assert( stream != NULL ); + if(__check_abort_enabled) + __check_abort(); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL) @@ -74,13 +77,14 @@ ferror(FILE *stream) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( __is_valid_iob(file) ); + flockfile(stream); + result = FLAG_IS_SET(file->iob_Flags,IOBF_ERROR); + funlockfile(stream); + out: RETURN(result); diff --git a/library/stdio_fflush.c b/library/stdio_fflush.c index a58777b..4dc66b1 100644 --- a/library/stdio_fflush.c +++ b/library/stdio_fflush.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fflush.c,v 1.4 2005-02-21 10:21:48 obarthel Exp $ + * $Id: stdio_fflush.c,v 1.5 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -55,6 +55,8 @@ fflush(FILE *stream) if(__check_abort_enabled) __check_abort(); + flockfile(stream); + #if defined(UNIX_PATH_SEMANTICS) { /* Flush a particular stream? */ @@ -114,6 +116,8 @@ fflush(FILE *stream) out: + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_fgetc.c b/library/stdio_fgetc.c index bb471a0..582b17d 100644 --- a/library/stdio_fgetc.c +++ b/library/stdio_fgetc.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fgetc.c,v 1.4 2005-02-21 10:21:48 obarthel Exp $ + * $Id: stdio_fgetc.c,v 1.5 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -95,9 +95,6 @@ __fgetc_check(FILE * stream) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); assert( file->iob_BufferSize > 0 ); @@ -138,6 +135,11 @@ fgetc(FILE *stream) assert( stream != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL) @@ -155,5 +157,7 @@ fgetc(FILE *stream) out: + funlockfile(stream); + return(result); } diff --git a/library/stdio_fgetpos.c b/library/stdio_fgetpos.c index c6196a8..a7f73d0 100644 --- a/library/stdio_fgetpos.c +++ b/library/stdio_fgetpos.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fgetpos.c,v 1.3 2005-02-03 16:56:16 obarthel Exp $ + * $Id: stdio_fgetpos.c,v 1.4 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -56,6 +56,11 @@ fgetpos(FILE *stream, fpos_t *pos) assert( stream != NULL && pos != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL || pos == NULL) @@ -68,9 +73,6 @@ fgetpos(FILE *stream, fpos_t *pos) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - position = ftell(stream); if(position == -1) { @@ -85,6 +87,8 @@ fgetpos(FILE *stream, fpos_t *pos) out: + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_fgets.c b/library/stdio_fgets.c index 101de7b..3ad8eee 100644 --- a/library/stdio_fgets.c +++ b/library/stdio_fgets.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fgets.c,v 1.4 2005-02-21 10:21:48 obarthel Exp $ + * $Id: stdio_fgets.c,v 1.5 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -57,6 +57,11 @@ fgets(char *s,int n,FILE *stream) assert( s != NULL && stream != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(s == NULL || stream == NULL) @@ -70,9 +75,6 @@ fgets(char *s,int n,FILE *stream) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - if(n <= 0) { SHOWMSG("no work to be done"); @@ -130,6 +132,8 @@ fgets(char *s,int n,FILE *stream) out: + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_flockfile.c b/library/stdio_flockfile.c new file mode 100644 index 0000000..ab94333 --- /dev/null +++ b/library/stdio_flockfile.c @@ -0,0 +1,89 @@ +/* + * $Id: stdio_flockfile.c,v 1.1 2005-02-27 18:09:10 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2005 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 _STDLIB_NULL_POINTER_CHECK_H +#include "stdlib_null_pointer_check.h" +#endif /* _STDLIB_NULL_POINTER_CHECK_H */ + +/****************************************************************************/ + +#ifndef _STDIO_HEADERS_H +#include "stdio_headers.h" +#endif /* _STDIO_HEADERS_H */ + +/****************************************************************************/ + +void +flockfile(FILE *stream) +{ + struct iob * file = (struct iob *)stream; + + ENTER(); + + SHOWPOINTER(stream); + + assert( stream != NULL ); + + #if defined(CHECK_FOR_NULL_POINTERS) + { + if(stream == NULL) + { + SHOWMSG("invalid stream parameter"); + + __set_errno(EFAULT); + goto out; + } + } + #endif /* CHECK_FOR_NULL_POINTERS */ + + if(__check_abort_enabled) + __check_abort(); + + assert( __is_valid_iob(file) ); + assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); + + if(FLAG_IS_CLEAR(file->iob_Flags,IOBF_IN_USE)) + { + SHOWMSG("this file is not even in use"); + + __set_errno(EBADF); + goto out; + } + + if(file->iob_Lock != NULL) + ObtainSemaphore(file->iob_Lock); + + out: + + LEAVE(); +} diff --git a/library/stdio_flush.c b/library/stdio_flush.c index 803e3c0..4cc1bb7 100644 --- a/library/stdio_flush.c +++ b/library/stdio_flush.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_flush.c,v 1.3 2005-02-03 16:56:16 obarthel Exp $ + * $Id: stdio_flush.c,v 1.4 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -62,6 +62,11 @@ __flush(FILE *stream) assert( stream != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL) @@ -74,9 +79,6 @@ __flush(FILE *stream) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( __is_valid_iob(iob) ); assert( iob->iob_BufferWriteBytes > 0 ); assert( iob->iob_BufferSize > 0 ); @@ -95,6 +97,8 @@ __flush(FILE *stream) out: + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_fopen.c b/library/stdio_fopen.c index d2ceafc..11619ac 100644 --- a/library/stdio_fopen.c +++ b/library/stdio_fopen.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fopen.c,v 1.3 2005-02-03 16:56:16 obarthel Exp $ + * $Id: stdio_fopen.c,v 1.4 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -56,6 +56,9 @@ fopen(const char *filename, const char *mode) assert( filename != NULL && mode != NULL ); + if(__check_abort_enabled) + __check_abort(); + #if defined(CHECK_FOR_NULL_POINTERS) { if(filename == NULL || mode == NULL) @@ -68,9 +71,6 @@ fopen(const char *filename, const char *mode) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - slot_number = __find_vacant_iob_entry(); if(slot_number < 0) { diff --git a/library/stdio_fprintf.c b/library/stdio_fprintf.c index 9d1f7f7..82bb4f4 100644 --- a/library/stdio_fprintf.c +++ b/library/stdio_fprintf.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fprintf.c,v 1.3 2005-02-03 16:56:16 obarthel Exp $ + * $Id: stdio_fprintf.c,v 1.4 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -56,6 +56,11 @@ fprintf(FILE *stream,const char *format,...) assert( stream != NULL && format != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL || format == NULL) @@ -66,15 +71,14 @@ fprintf(FILE *stream,const char *format,...) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - va_start(arg,format); result = vfprintf(stream,format,arg); va_end(arg); out: + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_fputc.c b/library/stdio_fputc.c index d6a5537..5e6ceb7 100644 --- a/library/stdio_fputc.c +++ b/library/stdio_fputc.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fputc.c,v 1.4 2005-02-21 10:21:48 obarthel Exp $ + * $Id: stdio_fputc.c,v 1.5 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -61,9 +61,6 @@ __fputc_check(FILE *stream) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( __is_valid_iob(file) ); assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); assert( file->iob_BufferSize > 0 ); @@ -148,8 +145,13 @@ fputc(int c,FILE *stream) assert( stream != NULL ); + if(__check_abort_enabled) + __check_abort(); + assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); + flockfile(stream); + if(__fputc_check(stream) < 0) goto out; @@ -157,5 +159,7 @@ fputc(int c,FILE *stream) out: + funlockfile(stream); + return(result); } diff --git a/library/stdio_fputs.c b/library/stdio_fputs.c index e894f23..d77ad0c 100644 --- a/library/stdio_fputs.c +++ b/library/stdio_fputs.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fputs.c,v 1.4 2005-02-21 10:21:49 obarthel Exp $ + * $Id: stdio_fputs.c,v 1.5 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -58,6 +58,9 @@ fputs(const char *s, FILE *stream) assert( s != NULL && stream != NULL ); + if(__check_abort_enabled) + __check_abort(); + #if defined(CHECK_FOR_NULL_POINTERS) { if(s == NULL || stream == NULL) @@ -68,8 +71,7 @@ fputs(const char *s, FILE *stream) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); + flockfile(stream); assert( __is_valid_iob(file) ); assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); @@ -105,6 +107,8 @@ fputs(const char *s, FILE *stream) } } + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_fread.c b/library/stdio_fread.c index 98653f4..5314e95 100644 --- a/library/stdio_fread.c +++ b/library/stdio_fread.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fread.c,v 1.4 2005-02-21 10:21:49 obarthel Exp $ + * $Id: stdio_fread.c,v 1.5 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -59,6 +59,11 @@ fread(void *ptr,size_t element_size,size_t count,FILE *stream) assert( ptr != NULL && stream != NULL ); assert( (int)element_size >= 0 && (int)count >= 0 ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(ptr == NULL || stream == NULL) @@ -71,9 +76,6 @@ fread(void *ptr,size_t element_size,size_t count,FILE *stream) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( __is_valid_iob(file) ); assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); assert( file->iob_BufferSize > 0 ); @@ -141,6 +143,8 @@ fread(void *ptr,size_t element_size,size_t count,FILE *stream) out: + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_fscanf.c b/library/stdio_fscanf.c index b00c56d..f07ba4d 100644 --- a/library/stdio_fscanf.c +++ b/library/stdio_fscanf.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fscanf.c,v 1.3 2005-02-03 16:56:16 obarthel Exp $ + * $Id: stdio_fscanf.c,v 1.4 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -56,6 +56,11 @@ fscanf(FILE *stream, const char *format, ...) assert(stream != NULL && format != NULL); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL || format == NULL) @@ -68,15 +73,14 @@ fscanf(FILE *stream, const char *format, ...) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - va_start(arg,format); result = __vfscanf(stream,format,arg); va_end(arg); out: + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_fseek.c b/library/stdio_fseek.c index 50f463c..6279025 100644 --- a/library/stdio_fseek.c +++ b/library/stdio_fseek.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fseek.c,v 1.6 2005-02-21 10:21:49 obarthel Exp $ + * $Id: stdio_fseek.c,v 1.7 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -57,6 +57,11 @@ fseek(FILE *stream, long int offset, int wherefrom) assert(stream != NULL); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL) @@ -69,9 +74,6 @@ fseek(FILE *stream, long int offset, int wherefrom) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( __is_valid_iob(file) ); assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); assert( file->iob_BufferSize > 0 ); @@ -188,6 +190,8 @@ fseek(FILE *stream, long int offset, int wherefrom) out: + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_fsetpos.c b/library/stdio_fsetpos.c index 87a84e7..6d8899d 100644 --- a/library/stdio_fsetpos.c +++ b/library/stdio_fsetpos.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fsetpos.c,v 1.4 2005-02-21 10:21:49 obarthel Exp $ + * $Id: stdio_fsetpos.c,v 1.5 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -55,6 +55,11 @@ fsetpos(FILE *stream, fpos_t *pos) assert( stream != NULL && pos != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL || pos == NULL) @@ -67,9 +72,6 @@ fsetpos(FILE *stream, fpos_t *pos) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - if(fseek(stream,(long int)(*pos),SEEK_SET) != 0) { SHOWMSG("fseek failed"); @@ -80,6 +82,8 @@ fsetpos(FILE *stream, fpos_t *pos) out: + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_ftell.c b/library/stdio_ftell.c index 3ae10ad..4b175e4 100644 --- a/library/stdio_ftell.c +++ b/library/stdio_ftell.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_ftell.c,v 1.6 2005-02-21 10:21:49 obarthel Exp $ + * $Id: stdio_ftell.c,v 1.7 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -53,6 +53,11 @@ ftell(FILE *stream) assert( stream != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL) @@ -63,9 +68,6 @@ ftell(FILE *stream) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( __is_valid_iob(file) ); assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); assert( file->iob_BufferSize > 0 ); @@ -123,5 +125,7 @@ ftell(FILE *stream) out: + funlockfile(stream); + return(result); } diff --git a/library/stdio_ftrylockfile.c b/library/stdio_ftrylockfile.c new file mode 100644 index 0000000..e998c50 --- /dev/null +++ b/library/stdio_ftrylockfile.c @@ -0,0 +1,93 @@ +/* + * $Id: stdio_ftrylockfile.c,v 1.1 2005-02-27 18:09:10 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2005 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 _STDLIB_NULL_POINTER_CHECK_H +#include "stdlib_null_pointer_check.h" +#endif /* _STDLIB_NULL_POINTER_CHECK_H */ + +/****************************************************************************/ + +#ifndef _STDIO_HEADERS_H +#include "stdio_headers.h" +#endif /* _STDIO_HEADERS_H */ + +/****************************************************************************/ + +int +ftrylockfile(FILE *stream) +{ + struct iob * file = (struct iob *)stream; + int result = -1; + + ENTER(); + + SHOWPOINTER(stream); + + assert( stream != NULL ); + + #if defined(CHECK_FOR_NULL_POINTERS) + { + if(stream == NULL) + { + SHOWMSG("invalid stream parameter"); + + __set_errno(EFAULT); + goto out; + } + } + #endif /* CHECK_FOR_NULL_POINTERS */ + + if(__check_abort_enabled) + __check_abort(); + + assert( __is_valid_iob(file) ); + assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); + + if(FLAG_IS_CLEAR(file->iob_Flags,IOBF_IN_USE)) + { + SHOWMSG("this file is not even in use"); + + __set_errno(EBADF); + goto out; + } + + if(file->iob_Lock != NULL && CANNOT AttemptSemaphore(file->iob_Lock)) + goto out; + + result = 0; + + out: + + RETURN(result); + return(result); +} diff --git a/library/stdio_funlockfile.c b/library/stdio_funlockfile.c new file mode 100644 index 0000000..b8ac728 --- /dev/null +++ b/library/stdio_funlockfile.c @@ -0,0 +1,89 @@ +/* + * $Id: stdio_funlockfile.c,v 1.1 2005-02-27 18:09:10 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2005 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 _STDLIB_NULL_POINTER_CHECK_H +#include "stdlib_null_pointer_check.h" +#endif /* _STDLIB_NULL_POINTER_CHECK_H */ + +/****************************************************************************/ + +#ifndef _STDIO_HEADERS_H +#include "stdio_headers.h" +#endif /* _STDIO_HEADERS_H */ + +/****************************************************************************/ + +void +funlockfile(FILE *stream) +{ + struct iob * file = (struct iob *)stream; + + ENTER(); + + SHOWPOINTER(stream); + + assert( stream != NULL ); + + #if defined(CHECK_FOR_NULL_POINTERS) + { + if(stream == NULL) + { + SHOWMSG("invalid stream parameter"); + + __set_errno(EFAULT); + goto out; + } + } + #endif /* CHECK_FOR_NULL_POINTERS */ + + if(__check_abort_enabled) + __check_abort(); + + assert( __is_valid_iob(file) ); + assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); + + if(FLAG_IS_CLEAR(file->iob_Flags,IOBF_IN_USE)) + { + SHOWMSG("this file is not even in use"); + + __set_errno(EBADF); + goto out; + } + + if(file->iob_Lock != NULL) + ReleaseSemaphore(file->iob_Lock); + + out: + + LEAVE(); +} diff --git a/library/stdio_fwrite.c b/library/stdio_fwrite.c index de02b85..82ab99c 100644 --- a/library/stdio_fwrite.c +++ b/library/stdio_fwrite.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fwrite.c,v 1.5 2005-02-21 10:21:49 obarthel Exp $ + * $Id: stdio_fwrite.c,v 1.6 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -59,6 +59,11 @@ fwrite(const void *ptr,size_t element_size,size_t count,FILE *stream) assert( ptr != NULL && stream != NULL ); assert( (int)element_size >= 0 && (int)count >= 0 ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(ptr == NULL || stream == NULL) @@ -71,9 +76,6 @@ fwrite(const void *ptr,size_t element_size,size_t count,FILE *stream) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( __is_valid_iob(file) ); assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); assert( file->iob_BufferSize > 0 ); @@ -156,6 +158,8 @@ fwrite(const void *ptr,size_t element_size,size_t count,FILE *stream) out: + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_getc_unlocked.c b/library/stdio_getc_unlocked.c new file mode 100644 index 0000000..4cfae8b --- /dev/null +++ b/library/stdio_getc_unlocked.c @@ -0,0 +1,72 @@ +/* + * $Id: stdio_getc_unlocked.c,v 1.1 2005-02-27 18:09:10 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2005 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 _STDLIB_NULL_POINTER_CHECK_H +#include "stdlib_null_pointer_check.h" +#endif /* _STDLIB_NULL_POINTER_CHECK_H */ + +/****************************************************************************/ + +#ifndef _STDIO_HEADERS_H +#include "stdio_headers.h" +#endif /* _STDIO_HEADERS_H */ + +/****************************************************************************/ + +#undef getc_unlocked + +/****************************************************************************/ + +int +getc_unlocked(FILE *stream) +{ + int result = -1; + + assert( stream != NULL ); + + #if defined(CHECK_FOR_NULL_POINTERS) + { + if(stream == NULL) + { + __set_errno(EFAULT); + goto out; + } + } + #endif /* CHECK_FOR_NULL_POINTERS */ + + result = __getc_unlocked(stream); + + out: + + return(result); +} diff --git a/library/stdio_getchar_unlocked.c b/library/stdio_getchar_unlocked.c new file mode 100644 index 0000000..41d7114 --- /dev/null +++ b/library/stdio_getchar_unlocked.c @@ -0,0 +1,72 @@ +/* + * $Id: stdio_getchar_unlocked.c,v 1.1 2005-02-27 18:09:10 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2005 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 _STDLIB_NULL_POINTER_CHECK_H +#include "stdlib_null_pointer_check.h" +#endif /* _STDLIB_NULL_POINTER_CHECK_H */ + +/****************************************************************************/ + +#ifndef _STDIO_HEADERS_H +#include "stdio_headers.h" +#endif /* _STDIO_HEADERS_H */ + +/****************************************************************************/ + +#undef getchar_unlocked + +/****************************************************************************/ + +int +getchar_unlocked(void) +{ + int result = -1; + + assert( stdin != NULL ); + + #if defined(CHECK_FOR_NULL_POINTERS) + { + if(stdin == NULL) + { + __set_errno(EFAULT); + goto out; + } + } + #endif /* CHECK_FOR_NULL_POINTERS */ + + result = __getc_unlocked(stdin); + + out: + + return(result); +} diff --git a/library/stdio_gets.c b/library/stdio_gets.c index e2c9cf1..70e39ab 100644 --- a/library/stdio_gets.c +++ b/library/stdio_gets.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_gets.c,v 1.4 2005-02-21 10:21:49 obarthel Exp $ + * $Id: stdio_gets.c,v 1.5 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -55,6 +55,11 @@ gets(char *s) assert( s != NULL && stdin != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stdin); + #if defined(CHECK_FOR_NULL_POINTERS) { if(s == NULL || stdin == NULL) @@ -69,9 +74,6 @@ gets(char *s) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - /* Take care of the checks and data structure changes that * need to be handled only once for this stream. */ @@ -118,6 +120,8 @@ gets(char *s) out: + funlockfile(stdin); + RETURN(result); return(result); } diff --git a/library/stdio_headers.h b/library/stdio_headers.h index 2ff2c6d..38b6a2c 100644 --- a/library/stdio_headers.h +++ b/library/stdio_headers.h @@ -1,5 +1,5 @@ /* - * $Id: stdio_headers.h,v 1.15 2005-02-20 15:46:52 obarthel Exp $ + * $Id: stdio_headers.h,v 1.16 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -262,6 +262,8 @@ struct iob UBYTE iob_SingleByte; /* Fall-back buffer for 'unbuffered' files */ + + struct SignalSemaphore * iob_Lock; /* For thread locking */ }; /****************************************************************************/ diff --git a/library/stdio_init_exit.c b/library/stdio_init_exit.c index 28dd04c..47e2bc0 100644 --- a/library/stdio_init_exit.c +++ b/library/stdio_init_exit.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_init_exit.c,v 1.17 2005-02-20 13:19:40 obarthel Exp $ + * $Id: stdio_init_exit.c,v 1.18 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -111,6 +111,7 @@ __stdio_init(void) { const int num_standard_files = (STDERR_FILENO-STDIN_FILENO+1); + struct SignalSemaphore * lock; BPTR default_file; ULONG fd_flags,iob_flags; int result = ERROR; @@ -187,6 +188,14 @@ __stdio_init(void) if(buffer == NULL) goto out; + /* Allocate memory for an arbitration mechanism, then + initialize it. */ + lock = AllocVec(sizeof(*lock),MEMF_ANY|MEMF_PUBLIC); + if(lock == NULL) + goto out; + + InitSemaphore(lock); + /* Check if this stream is attached to a console window. */ if(default_file != ZERO) { @@ -208,7 +217,8 @@ __stdio_init(void) aligned_buffer,BUFSIZ, i, i, - iob_flags); + iob_flags, + lock); } /* If the program was launched from Workbench, we continue by diff --git a/library/stdio_initializeiob.c b/library/stdio_initializeiob.c index 73a6a19..95bd4a5 100644 --- a/library/stdio_initializeiob.c +++ b/library/stdio_initializeiob.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_initializeiob.c,v 1.3 2005-02-20 13:19:40 obarthel Exp $ + * $Id: stdio_initializeiob.c,v 1.4 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -39,14 +39,15 @@ void __initialize_iob( - struct iob * iob, - file_action_iob_t action_function, - STRPTR custom_buffer, - STRPTR buffer, - int buffer_size, - int file_descriptor, - int slot_number, - ULONG flags) + struct iob * iob, + file_action_iob_t action_function, + STRPTR custom_buffer, + STRPTR buffer, + int buffer_size, + int file_descriptor, + int slot_number, + ULONG flags, + struct SignalSemaphore * lock) { assert( iob != NULL && action_function != NULL ); @@ -59,4 +60,5 @@ __initialize_iob( iob->iob_SlotNumber = slot_number; iob->iob_Flags = flags; iob->iob_Action = action_function; + iob->iob_Lock = lock; } diff --git a/library/stdio_openiob.c b/library/stdio_openiob.c index 47615a2..8e1b560 100644 --- a/library/stdio_openiob.c +++ b/library/stdio_openiob.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_openiob.c,v 1.7 2005-02-20 13:19:40 obarthel Exp $ + * $Id: stdio_openiob.c,v 1.8 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -46,6 +46,7 @@ int __open_iob(const char *filename, const char *mode, int file_descriptor, int slot_number) { + struct SignalSemaphore * lock; ULONG file_flags; int result = -1; int open_mode; @@ -162,6 +163,14 @@ __open_iob(const char *filename, const char *mode, int file_descriptor, int slot CLEAR_FLAG(fd->fd_Flags,FDF_APPEND); } + /* Allocate memory for an arbitration mechanism, then + initialize it. */ + lock = AllocVec(sizeof(*lock),MEMF_ANY|MEMF_PUBLIC); + if(lock == NULL) + goto out; + + InitSemaphore(lock); + /* Figure out the buffered file access mode by looking at the open mode. */ file_flags = IOBF_IN_USE | IOBF_NO_NUL; @@ -176,7 +185,8 @@ __open_iob(const char *filename, const char *mode, int file_descriptor, int slot aligned_buffer,BUFSIZ, file_descriptor, slot_number, - file_flags); + file_flags, + lock); buffer = NULL; diff --git a/library/stdio_protos.h b/library/stdio_protos.h index 670eaac..e6e0854 100644 --- a/library/stdio_protos.h +++ b/library/stdio_protos.h @@ -1,5 +1,5 @@ /* - * $Id: stdio_protos.h,v 1.7 2005-02-20 13:19:40 obarthel Exp $ + * $Id: stdio_protos.h,v 1.8 2005-02-27 18:09:10 obarthel Exp $ * * :ts=4 * @@ -103,7 +103,7 @@ extern int __find_vacant_fd_entry(void); /****************************************************************************/ /* stdio_initializeiob.c */ -extern void __initialize_iob(struct iob * iob,file_action_iob_t action_function,STRPTR custom_buffer,STRPTR buffer,int buffer_size,int file_descriptor,int slot_number,ULONG flags); +extern void __initialize_iob(struct iob * iob,file_action_iob_t action_function,STRPTR custom_buffer,STRPTR buffer,int buffer_size,int file_descriptor,int slot_number,ULONG flags,struct SignalSemaphore * lock); /****************************************************************************/ diff --git a/library/stdio_putc_unlocked.c b/library/stdio_putc_unlocked.c new file mode 100644 index 0000000..b53bf02 --- /dev/null +++ b/library/stdio_putc_unlocked.c @@ -0,0 +1,72 @@ +/* + * $Id: stdio_putc_unlocked.c,v 1.1 2005-02-27 18:09:10 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2005 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 _STDLIB_NULL_POINTER_CHECK_H +#include "stdlib_null_pointer_check.h" +#endif /* _STDLIB_NULL_POINTER_CHECK_H */ + +/****************************************************************************/ + +#ifndef _STDIO_HEADERS_H +#include "stdio_headers.h" +#endif /* _STDIO_HEADERS_H */ + +/****************************************************************************/ + +#undef putc_unlocked + +/****************************************************************************/ + +int +putc_unlocked(int c,FILE *stream) +{ + int result = -1; + + assert( stream != NULL ); + + #if defined(CHECK_FOR_NULL_POINTERS) + { + if(stream == NULL) + { + __set_errno(EFAULT); + goto out; + } + } + #endif /* CHECK_FOR_NULL_POINTERS */ + + result = __putc_unlocked(c,stream); + + out: + + return(result); +} diff --git a/library/stdio_putchar_unlocked.c b/library/stdio_putchar_unlocked.c new file mode 100644 index 0000000..f6660c3 --- /dev/null +++ b/library/stdio_putchar_unlocked.c @@ -0,0 +1,72 @@ +/* + * $Id: stdio_putchar_unlocked.c,v 1.1 2005-02-27 18:09:11 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2005 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 _STDLIB_NULL_POINTER_CHECK_H +#include "stdlib_null_pointer_check.h" +#endif /* _STDLIB_NULL_POINTER_CHECK_H */ + +/****************************************************************************/ + +#ifndef _STDIO_HEADERS_H +#include "stdio_headers.h" +#endif /* _STDIO_HEADERS_H */ + +/****************************************************************************/ + +#undef putchar_unlocked + +/****************************************************************************/ + +int +putchar_unlocked(int c) +{ + int result = -1; + + assert( stdout != NULL ); + + #if defined(CHECK_FOR_NULL_POINTERS) + { + if(stdout == NULL) + { + __set_errno(EFAULT); + goto out; + } + } + #endif /* CHECK_FOR_NULL_POINTERS */ + + result = __putc_unlocked(c,stdout); + + out: + + return(result); +} diff --git a/library/stdio_puts.c b/library/stdio_puts.c index ceca796..48a69d7 100644 --- a/library/stdio_puts.c +++ b/library/stdio_puts.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_puts.c,v 1.5 2005-02-21 10:22:00 obarthel Exp $ + * $Id: stdio_puts.c,v 1.6 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -57,6 +57,11 @@ puts(const char *s) assert( s != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stdout); + #if defined(CHECK_FOR_NULL_POINTERS) { if(s == NULL) @@ -67,9 +72,6 @@ puts(const char *s) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( __is_valid_iob(file) ); assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); assert( file->iob_BufferSize > 0 ); @@ -107,6 +109,8 @@ puts(const char *s) } } + funlockfile(stdout); + RETURN(result); return(result); } diff --git a/library/stdio_rewind.c b/library/stdio_rewind.c index 94c4cce..c68343c 100644 --- a/library/stdio_rewind.c +++ b/library/stdio_rewind.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_rewind.c,v 1.2 2005-01-02 09:07:08 obarthel Exp $ + * $Id: stdio_rewind.c,v 1.3 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -45,7 +45,11 @@ rewind(FILE *stream) if(__check_abort_enabled) __check_abort(); + flockfile(stream); + clearerr(stream); fseek(stream,0,SEEK_SET); + + funlockfile(stream); } diff --git a/library/stdio_setbuf.c b/library/stdio_setbuf.c index 857e383..25a3cad 100644 --- a/library/stdio_setbuf.c +++ b/library/stdio_setbuf.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_setbuf.c,v 1.3 2005-02-03 16:56:16 obarthel Exp $ + * $Id: stdio_setbuf.c,v 1.4 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -53,6 +53,9 @@ setbuf(FILE *stream,char *buf) assert(stream != NULL); + if(__check_abort_enabled) + __check_abort(); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL) @@ -65,9 +68,6 @@ setbuf(FILE *stream,char *buf) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - if(buf == NULL) setvbuf(stream,NULL,IOBF_BUFFER_MODE_NONE,0); else diff --git a/library/stdio_setvbuf.c b/library/stdio_setvbuf.c index 010b167..f7a2ed2 100644 --- a/library/stdio_setvbuf.c +++ b/library/stdio_setvbuf.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_setvbuf.c,v 1.5 2005-02-21 10:22:00 obarthel Exp $ + * $Id: stdio_setvbuf.c,v 1.6 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -65,6 +65,11 @@ setvbuf(FILE *stream,char *buf,int bufmode,size_t size) assert( stream != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL) @@ -77,9 +82,6 @@ setvbuf(FILE *stream,char *buf,int bufmode,size_t size) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - if(bufmode < IOBF_BUFFER_MODE_FULL || bufmode > IOBF_BUFFER_MODE_NONE) { @@ -188,6 +190,8 @@ setvbuf(FILE *stream,char *buf,int bufmode,size_t size) out: + funlockfile(stream); + if(new_buffer != NULL) free(new_buffer); diff --git a/library/stdio_sscanf.c b/library/stdio_sscanf.c index c4a17eb..aeb12bd 100644 --- a/library/stdio_sscanf.c +++ b/library/stdio_sscanf.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_sscanf.c,v 1.4 2005-02-20 13:19:40 obarthel Exp $ + * $Id: stdio_sscanf.c,v 1.5 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -78,7 +78,8 @@ sscanf(const char *s,const char *format, ...) local_buffer,sizeof(local_buffer), -1, -1, - IOBF_IN_USE | IOBF_READ | IOBF_BUFFER_MODE_FULL | IOBF_INTERNAL); + IOBF_IN_USE | IOBF_READ | IOBF_BUFFER_MODE_FULL | IOBF_INTERNAL, + NULL); string_iob.iob_String = (STRPTR)s; string_iob.iob_StringLength = strlen(s); diff --git a/library/stdio_ungetc.c b/library/stdio_ungetc.c index 86b3576..057a21c 100644 --- a/library/stdio_ungetc.c +++ b/library/stdio_ungetc.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_ungetc.c,v 1.4 2005-02-21 10:22:01 obarthel Exp $ + * $Id: stdio_ungetc.c,v 1.5 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -51,6 +51,11 @@ ungetc(int c,FILE *stream) assert( stream != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL) @@ -63,9 +68,6 @@ ungetc(int c,FILE *stream) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( __is_valid_iob(file) ); assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) ); assert( file->iob_BufferSize > 0 ); @@ -140,5 +142,7 @@ ungetc(int c,FILE *stream) out: + funlockfile(stream); + return(result); } diff --git a/library/stdio_unlockfile.c b/library/stdio_unlockfile.c new file mode 100644 index 0000000..6a1fa77 --- /dev/null +++ b/library/stdio_unlockfile.c @@ -0,0 +1,46 @@ +/* + * $Id: stdio_unlockfile.c,v 1.1 2005-02-27 18:09:11 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2005 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 +__unlockfile(FILE *stream,int c) +{ + funlockfile(stream); + + return(c); +} diff --git a/library/stdio_vasprintf.c b/library/stdio_vasprintf.c index 3dc83d7..cf7c507 100644 --- a/library/stdio_vasprintf.c +++ b/library/stdio_vasprintf.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_vasprintf.c,v 1.7 2005-02-20 13:19:40 obarthel Exp $ + * $Id: stdio_vasprintf.c,v 1.8 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -93,7 +93,8 @@ __vasprintf(const char *file,int line,char **ret,const char *format,va_list arg) local_buffer,sizeof(local_buffer), -1, -1, - IOBF_IN_USE | IOBF_WRITE | IOBF_BUFFER_MODE_NONE | IOBF_INTERNAL); + IOBF_IN_USE | IOBF_WRITE | IOBF_BUFFER_MODE_NONE | IOBF_INTERNAL, + NULL); string_iob.iob_String = NULL; string_iob.iob_StringSize = 0; diff --git a/library/stdio_vfprintf.c b/library/stdio_vfprintf.c index 91c226a..eeb0490 100644 --- a/library/stdio_vfprintf.c +++ b/library/stdio_vfprintf.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_vfprintf.c,v 1.11 2005-02-25 10:14:21 obarthel Exp $ + * $Id: stdio_vfprintf.c,v 1.12 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -151,6 +151,11 @@ vfprintf(FILE * stream,const char * format, va_list arg) assert( stream != NULL && format != NULL && arg != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL || format == NULL) @@ -1512,6 +1517,8 @@ vfprintf(FILE * stream,const char * format, va_list arg) result = EOF; } + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_vfscanf.c b/library/stdio_vfscanf.c index 8cc1e36..5a7a0e0 100644 --- a/library/stdio_vfscanf.c +++ b/library/stdio_vfscanf.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_vfscanf.c,v 1.9 2005-02-21 10:22:01 obarthel Exp $ + * $Id: stdio_vfscanf.c,v 1.10 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -88,6 +88,11 @@ __vfscanf(FILE *stream, const char *format, va_list arg) assert( stream != NULL && format != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(stream); + #if defined(CHECK_FOR_NULL_POINTERS) { if(stream == NULL || format == NULL) @@ -1572,6 +1577,8 @@ __vfscanf(FILE *stream, const char *format, va_list arg) out: + funlockfile(stream); + RETURN(result); return(result); } diff --git a/library/stdio_vsnprintf.c b/library/stdio_vsnprintf.c index 5c31eef..9d1fc93 100644 --- a/library/stdio_vsnprintf.c +++ b/library/stdio_vsnprintf.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_vsnprintf.c,v 1.5 2005-02-20 13:19:40 obarthel Exp $ + * $Id: stdio_vsnprintf.c,v 1.6 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -83,7 +83,8 @@ vsnprintf(char *buffer,size_t size,const char *format,va_list arg) local_buffer,sizeof(local_buffer), -1, -1, - IOBF_IN_USE | IOBF_WRITE | IOBF_NO_NUL | IOBF_BUFFER_MODE_NONE | IOBF_INTERNAL); + IOBF_IN_USE | IOBF_WRITE | IOBF_NO_NUL | IOBF_BUFFER_MODE_NONE | IOBF_INTERNAL, + NULL); /* Store up to 'size-1' characters in the output buffer. This does not include the terminating NUL character, which we diff --git a/library/stdio_vsprintf.c b/library/stdio_vsprintf.c index e909040..dc7714c 100644 --- a/library/stdio_vsprintf.c +++ b/library/stdio_vsprintf.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_vsprintf.c,v 1.4 2005-02-20 13:19:40 obarthel Exp $ + * $Id: stdio_vsprintf.c,v 1.5 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -70,7 +70,8 @@ vsprintf(char *s,const char *format,va_list arg) buffer,sizeof(buffer), -1, -1, - IOBF_IN_USE | IOBF_WRITE | IOBF_BUFFER_MODE_NONE | IOBF_INTERNAL); + IOBF_IN_USE | IOBF_WRITE | IOBF_BUFFER_MODE_NONE | IOBF_INTERNAL, + NULL); string_iob.iob_String = (STRPTR)s; diff --git a/library/stdlib_headers.h b/library/stdlib_headers.h index 6b5a99f..4940999 100644 --- a/library/stdlib_headers.h +++ b/library/stdlib_headers.h @@ -1,5 +1,5 @@ /* - * $Id: stdlib_headers.h,v 1.8 2005-02-25 10:14:21 obarthel Exp $ + * $Id: stdlib_headers.h,v 1.9 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -194,7 +194,7 @@ struct MemoryTree /****************************************************************************/ -extern unsigned NOCOMMON __random_seed; +extern unsigned int NOCOMMON __random_seed; /****************************************************************************/ diff --git a/library/stdlib_rand.c b/library/stdlib_rand.c index be2d9c4..86dc0a6 100644 --- a/library/stdlib_rand.c +++ b/library/stdlib_rand.c @@ -1,5 +1,5 @@ /* - * $Id: stdlib_rand.c,v 1.3 2005-01-02 09:07:18 obarthel Exp $ + * $Id: stdlib_rand.c,v 1.4 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -37,34 +37,16 @@ /****************************************************************************/ -/* Parameters of a pseudo-random-number generator from Knuth's - * "The Art of Computer Programming, Volume 2: Seminumerical - * Algorithms" (3rd edition), pp. 185-186. - */ -#define MM 2147483647 /* a Mersenne prime */ -#define AA 48271 /* this does well in the spectral test */ -#define QQ 44488 /* (long)(MM/AA) */ -#define RR 3399 /* MM % AA; it is important that RR < QQ */ - -/****************************************************************************/ - -/* NOTE: for Knuth's algorithm below the seed must not be zero. */ -unsigned __random_seed = 1; +unsigned int __random_seed = 1; /****************************************************************************/ int rand(void) { - int X; + int result; - X = (int)__random_seed; + result = rand_r(&__random_seed); - X = AA * (X % QQ) - RR * (long)(X / QQ); - if(X < 0) - X += MM; - - __random_seed = (unsigned)X; - - return(X); + return(result); } diff --git a/library/stdlib_rand_r.c b/library/stdlib_rand_r.c new file mode 100644 index 0000000..90328a8 --- /dev/null +++ b/library/stdlib_rand_r.c @@ -0,0 +1,67 @@ +/* + * $Id: stdlib_rand_r.c,v 1.1 2005-02-27 18:09:11 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2005 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 _STDLIB_HEADERS_H +#include "stdlib_headers.h" +#endif /* _STDLIB_HEADERS_H */ + +/****************************************************************************/ + +/* Parameters of a pseudo-random-number generator from Knuth's + "The Art of Computer Programming, Volume 2: Seminumerical Algorithms" + (3rd edition), pp. 185-186. */ + +#define MM 2147483647 /* a Mersenne prime */ +#define AA 48271 /* this does well in the spectral test */ +#define QQ 44488 /* (long)(MM/AA) */ +#define RR 3399 /* MM % AA; it is important that RR < QQ */ + +/****************************************************************************/ + +int +rand_r(unsigned int * seed) +{ + int X; + + X = (int)((*seed) & 0x7fffffff); + if(X == 0) + X = 1; /* NOTE: for Knuth's algorithm the seed must not be zero. */ + + X = AA * (X % QQ) - RR * (long)(X / QQ); + if(X < 0) + X += MM; + + (*seed) = (unsigned int)X; + + return(X); +} diff --git a/library/stdlib_srand.c b/library/stdlib_srand.c index 3694222..e14bfc5 100644 --- a/library/stdlib_srand.c +++ b/library/stdlib_srand.c @@ -1,5 +1,5 @@ /* - * $Id: stdlib_srand.c,v 1.2 2005-01-02 09:07:18 obarthel Exp $ + * $Id: stdlib_srand.c,v 1.3 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -40,6 +40,5 @@ void srand(unsigned seed) { - /* We have to make sure that the seed is never zero. */ - __random_seed = (seed & 0x7FFFFFFF) | 1; + __random_seed = seed; } diff --git a/library/unistd_fileno.c b/library/unistd_fileno.c index 7de10f0..8cec5ff 100644 --- a/library/unistd_fileno.c +++ b/library/unistd_fileno.c @@ -1,5 +1,5 @@ /* - * $Id: unistd_fileno.c,v 1.4 2005-02-03 16:56:17 obarthel Exp $ + * $Id: unistd_fileno.c,v 1.5 2005-02-27 18:09:11 obarthel Exp $ * * :ts=4 * @@ -59,6 +59,11 @@ fileno(FILE * file) assert( file != NULL ); + if(__check_abort_enabled) + __check_abort(); + + flockfile(file); + #if defined(CHECK_FOR_NULL_POINTERS) { if(file == NULL) @@ -71,9 +76,6 @@ fileno(FILE * file) } #endif /* CHECK_FOR_NULL_POINTERS */ - if(__check_abort_enabled) - __check_abort(); - assert( __is_valid_iob(iob) ); assert( FLAG_IS_SET(iob->iob_Flags,IOBF_IN_USE) ); @@ -91,6 +93,8 @@ fileno(FILE * file) out: + funlockfile(file); + RETURN(result); return(result); }