11#include <stdio.h>
22#include <stdlib.h>
33#include <string.h>
4+ #include <errno.h>
45#ifdef _WIN32
56#include <windows.h>
67#endif
@@ -100,7 +101,6 @@ int main(int argc, const char **argv)
100101 int rc , i ;
101102 char * str ;
102103 const char * game = NULL ;
103-
104104#ifdef _WIN32
105105 SetConsoleOutputCP (1251 );
106106 SetConsoleCP (1251 );
@@ -110,10 +110,19 @@ int main(int argc, const char **argv)
110110 opt_debug = 1 ;
111111 else if (!strncmp (argv [i ], "-w" , 2 ))
112112 opt_width = atoi (argv [i ] + 2 );
113- else if (!strncmp (argv [i ], "-i" , 2 ))
114- freopen (argv [i ] + 2 , "r" , stdin );
115- else if (!game )
113+ else if (!strncmp (argv [i ], "-i" , 2 )) {
114+ if (freopen (argv [i ] + 2 , "r" , stdin ) != stdin ) {
115+ fprintf (stderr , "Error opening '%s': %s\n" , argv [i ] + 2 , strerror (errno ));
116+ exit (1 );
117+ }
118+ } else if (!strncmp (argv [i ], "-e" , 2 )) {
119+ if (freopen (argv [i ] + 2 , "w" , stderr ) != stderr ) {
120+ fprintf (stderr , "Error opening '%s': %s\n" , argv [i ] + 2 , strerror (errno ));
121+ exit (1 );
122+ }
123+ } else if (!game ) {
116124 game = argv [i ];
125+ }
117126 }
118127
119128 if (!game ) {
@@ -126,6 +135,9 @@ int main(int argc, const char **argv)
126135 exit (1 );
127136 }
128137
138+ if (!opt_debug )
139+ fclose (stderr );
140+
129141 instead_set_debug (opt_debug );
130142
131143 if (instead_init (game )) {
0 commit comments