Porting to OS/2: Case #11 freopen()
freopen() is used to reopen a file with a pre-existed stream pointer. kLIBC supports freopen() well. However, it has a problem. First, let's see the prototype of freopen().
If filename is NULL, freopen() changes the mode of stream. That is, read/write, text/binary and so on. And if freopen() fails, then it returns NULL.
By the way, kLIBC freopen() returns NULL even if it succeeds when filename is NULL. For examples, the following code returns NULL.
However, stdout is changed to binary mode correctly. So in this case, the return value should be ignored. Nevertheless, you should not always ignore NULL. Then what is the way to distinguish them ? freopen() sets 'errno' variable to a proper error value if it fails. So if you check errno, then it is possible to distinguish them. Use the following macro.
By this, it is possible to workaround a bug of kLIBC freopen().
- FILE *freopen(const char *filename, const char *mode, FILE *stream);
If filename is NULL, freopen() changes the mode of stream. That is, read/write, text/binary and so on. And if freopen() fails, then it returns NULL.
By the way, kLIBC freopen() returns NULL even if it succeeds when filename is NULL. For examples, the following code returns NULL.
- freopen( NULL, "wb", stdout );
However, stdout is changed to binary mode correctly. So in this case, the return value should be ignored. Nevertheless, you should not always ignore NULL. Then what is the way to distinguish them ? freopen() sets 'errno' variable to a proper error value if it fails. So if you check errno, then it is possible to distinguish them. Use the following macro.
1 2 | #define freopen(f, m, s) ((freopen)( f, m, s ) ? \ ( s ) : ( !errno ? ( s ) : NULL )) |
By this, it is possible to workaround a bug of kLIBC freopen().
댓글
댓글 쓰기