3 /* _PDCLIB_fdopen( _PDCLIB_fd_t fd, const char * )
\r
5 This file is part of the Public Domain C Library (PDCLib).
\r
6 Permission is granted to use, modify, and / or redistribute at will.
\r
13 #include <_PDCLIB_glue.h>
\r
16 extern struct _PDCLIB_file_t * _PDCLIB_filelist;
\r
18 struct _PDCLIB_file_t * _PDCLIB_fdopen( _PDCLIB_fd_t fd,
\r
20 const char * _PDCLIB_restrict filename )
\r
22 size_t filename_len;
\r
23 struct _PDCLIB_file_t * rc;
\r
29 /* To reduce the number of malloc calls, all data fields are concatenated:
\r
30 * the FILE structure itself,
\r
34 Data buffer comes last because it might change in size ( setvbuf() ).
\r
36 filename_len = filename ? strlen( filename ) + 1 : 1;
\r
37 if ( ( rc = calloc( 1, sizeof( struct _PDCLIB_file_t ) + _PDCLIB_UNGETCBUFSIZE + filename_len + BUFSIZ ) ) == NULL )
\r
44 /* Setting pointers into the memory block allocated above */
\r
45 rc->ungetbuf = (unsigned char *)rc + sizeof( struct _PDCLIB_file_t );
\r
46 rc->filename = (char *)rc->ungetbuf + _PDCLIB_UNGETCBUFSIZE;
\r
47 rc->buffer = rc->filename + filename_len;
\r
48 /* Copying filename to FILE structure */
\r
49 if(filename) strcpy( rc->filename, filename );
\r
50 /* Initializing the rest of the structure */
\r
51 rc->bufsize = BUFSIZ;
\r
54 /* Setting buffer to _IOLBF because "when opened, a stream is fully
\r
55 buffered if and only if it can be determined not to refer to an
\r
56 interactive device."
\r
58 rc->status |= _IOLBF;
\r
59 /* TODO: Setting mbstate */
\r
60 /* Adding to list of open files */
\r
61 rc->next = _PDCLIB_filelist;
\r
62 _PDCLIB_filelist = rc;
\r
69 #include <_PDCLIB_test.h>
\r
73 /* Some of the tests are not executed for regression tests, as the libc on
\r
74 my system is at once less forgiving (segfaults on mode NULL) and more
\r
75 forgiving (accepts undefined modes).
\r
79 TESTCASE_NOREG( fopen( NULL, NULL ) == NULL );
\r
80 TESTCASE( fopen( NULL, "w" ) == NULL );
\r
81 TESTCASE_NOREG( fopen( "", NULL ) == NULL );
\r
82 TESTCASE( fopen( "", "w" ) == NULL );
\r
83 TESTCASE( fopen( "foo", "" ) == NULL );
\r
84 TESTCASE_NOREG( fopen( testfile, "wq" ) == NULL ); /* Undefined mode */
\r
85 TESTCASE_NOREG( fopen( testfile, "wr" ) == NULL ); /* Undefined mode */
\r
86 TESTCASE( ( fh = fopen( testfile, "w" ) ) != NULL );
\r
87 TESTCASE( fclose( fh ) == 0 );
\r
88 TESTCASE( remove( testfile ) == 0 );
\r
89 return TEST_RESULTS;
\r