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().

  • 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().

댓글

이 블로그의 인기 게시물

토렌트: < 왕좌의 게임 > 시즌 1 ~ 시즌 8 완결편 마그넷

토렌트: < 스타워즈 > Ep.1 ~ Ep.6 마그넷

토렌트: NGC < 코스모스 > 우리말 더빙 전편(1편~13편) 마그넷