+++ /dev/null
-/* $Id$ */\r
-\r
-/* _PDCLIB_fdopen( _PDCLIB_fd_t fd, const char * )\r
-\r
- This file is part of the Public Domain C Library (PDCLib).\r
- Permission is granted to use, modify, and / or redistribute at will.\r
-*/\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-\r
-#ifndef REGTEST\r
-#include <_PDCLIB_glue.h>\r
-#include <string.h>\r
-#include <threads.h>\r
-\r
-extern struct _PDCLIB_file_t * _PDCLIB_filelist;\r
-\r
-struct _PDCLIB_file_t * _PDCLIB_fdopen( _PDCLIB_fd_t fd, \r
- int mode,\r
- const char * _PDCLIB_restrict filename )\r
-{\r
- size_t filename_len;\r
- struct _PDCLIB_file_t * rc;\r
- if ( mode == NULL )\r
- {\r
- /* Mode invalid */\r
- return NULL;\r
- }\r
- /* To reduce the number of malloc calls, all data fields are concatenated:\r
- * the FILE structure itself,\r
- * ungetc buffer,\r
- * filename buffer,\r
- * data buffer.\r
- Data buffer comes last because it might change in size ( setvbuf() ).\r
- */\r
- filename_len = filename ? strlen( filename ) + 1 : 1;\r
- if ( ( rc = calloc( 1, sizeof( struct _PDCLIB_file_t ) + _PDCLIB_UNGETCBUFSIZE + filename_len + BUFSIZ ) ) == NULL )\r
- {\r
- /* no memory */\r
- return NULL;\r
- }\r
-\r
- if(mtx_init(&rc->lock, mtx_recursive) != thrd_success) {\r
- free(rc);\r
- return NULL;\r
- }\r
-\r
- rc->status = mode;\r
- rc->handle = fd;\r
- /* Setting pointers into the memory block allocated above */\r
- rc->ungetbuf = (unsigned char *)rc + sizeof( struct _PDCLIB_file_t );\r
- rc->filename = (char *)rc->ungetbuf + _PDCLIB_UNGETCBUFSIZE;\r
- rc->buffer = rc->filename + filename_len;\r
- /* Copying filename to FILE structure */\r
- if(filename) strcpy( rc->filename, filename );\r
- /* Initializing the rest of the structure */\r
- rc->bufsize = BUFSIZ;\r
- rc->bufidx = 0;\r
- rc->ungetidx = 0;\r
- /* Setting buffer to _IOLBF because "when opened, a stream is fully\r
- buffered if and only if it can be determined not to refer to an\r
- interactive device."\r
- */\r
- rc->status |= _IOLBF;\r
- /* TODO: Setting mbstate */\r
- /* Adding to list of open files */\r
- rc->next = _PDCLIB_filelist;\r
- _PDCLIB_filelist = rc;\r
- return rc;\r
-}\r
-\r
-#endif\r
-\r
-#ifdef TEST\r
-#include <_PDCLIB_test.h>\r
-\r
-int main( void )\r
-{\r
- /* Some of the tests are not executed for regression tests, as the libc on\r
- my system is at once less forgiving (segfaults on mode NULL) and more\r
- forgiving (accepts undefined modes).\r
- */\r
- FILE * fh;\r
- remove( testfile );\r
- TESTCASE_NOREG( fopen( NULL, NULL ) == NULL );\r
- TESTCASE( fopen( NULL, "w" ) == NULL );\r
- TESTCASE_NOREG( fopen( "", NULL ) == NULL );\r
- TESTCASE( fopen( "", "w" ) == NULL );\r
- TESTCASE( fopen( "foo", "" ) == NULL );\r
- TESTCASE_NOREG( fopen( testfile, "wq" ) == NULL ); /* Undefined mode */\r
- TESTCASE_NOREG( fopen( testfile, "wr" ) == NULL ); /* Undefined mode */\r
- TESTCASE( ( fh = fopen( testfile, "w" ) ) != NULL );\r
- TESTCASE( fclose( fh ) == 0 );\r
- TESTCASE( remove( testfile ) == 0 );\r
- return TEST_RESULTS;\r
-}\r
-\r
-#endif\r