3 /* _PDCLIB_fvopen( _PDCLIB_fd_t fd, _PDCLIB_fileops_t * )
\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
14 #include <_PDCLIB_io.h>
\r
16 #include <threads.h>
\r
18 extern FILE * _PDCLIB_filelist;
\r
20 FILE * _PDCLIB_fvopen(
\r
22 const _PDCLIB_fileops_t *_PDCLIB_restrict ops,
\r
24 const char *_PDCLIB_restrict filename
\r
27 size_t filename_len;
\r
34 /* To reduce the number of malloc calls, all data fields are concatenated:
\r
35 * the FILE structure itself,
\r
39 Data buffer comes last because it might change in size ( setvbuf() ).
\r
41 filename_len = filename ? strlen( filename ) + 1 : 1;
\r
42 if ( ( rc = calloc( 1, sizeof( FILE ) + _PDCLIB_UNGETCBUFSIZE + filename_len + BUFSIZ ) ) == NULL )
\r
48 if(mtx_init(&rc->lock, mtx_recursive) != thrd_success) {
\r
56 /* Setting pointers into the memory block allocated above */
\r
57 rc->ungetbuf = (unsigned char *)rc + sizeof( FILE );
\r
58 rc->filename = (char *)rc->ungetbuf + _PDCLIB_UNGETCBUFSIZE;
\r
59 rc->buffer = rc->filename + filename_len;
\r
60 /* Copying filename to FILE structure */
\r
61 if(filename) strcpy( rc->filename, filename );
\r
62 /* Initializing the rest of the structure */
\r
63 rc->bufsize = BUFSIZ;
\r
66 /* Setting buffer to _IOLBF because "when opened, a stream is fully
\r
67 buffered if and only if it can be determined not to refer to an
\r
68 interactive device."
\r
70 rc->status |= _IOLBF;
\r
71 /* TODO: Setting mbstate */
\r
72 /* Adding to list of open files */
\r
73 rc->next = _PDCLIB_filelist;
\r
74 _PDCLIB_filelist = rc;
\r
81 #include <_PDCLIB_test.h>
\r
85 /* Some of the tests are not executed for regression tests, as the libc on
\r
86 my system is at once less forgiving (segfaults on mode NULL) and more
\r
87 forgiving (accepts undefined modes).
\r
91 TESTCASE_NOREG( fopen( NULL, NULL ) == NULL );
\r
92 TESTCASE( fopen( NULL, "w" ) == NULL );
\r
93 TESTCASE_NOREG( fopen( "", NULL ) == NULL );
\r
94 TESTCASE( fopen( "", "w" ) == NULL );
\r
95 TESTCASE( fopen( "foo", "" ) == NULL );
\r
96 TESTCASE_NOREG( fopen( testfile, "wq" ) == NULL ); /* Undefined mode */
\r
97 TESTCASE_NOREG( fopen( testfile, "wr" ) == NULL ); /* Undefined mode */
\r
98 TESTCASE( ( fh = fopen( testfile, "w" ) ) != NULL );
\r
99 TESTCASE( fclose( fh ) == 0 );
\r
100 TESTCASE( remove( testfile ) == 0 );
\r
101 return TEST_RESULTS;
\r