46 static FILE* fopen7zipped(
const char*
filename,
const char* mode)
53 if (_pipe(hPipe, 2048, ((mode[1] ==
'b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1)
55 fprintf(stderr,
"could not create pipe\n");
60 int hStdOut = _dup(_fileno(stdout));
65 fprintf(stderr,
"could not set pipe output\n");
72 fprintf(stderr,
"could not redirect input file\n");
77 close(hPipe[WRITE_HANDLE]);
80 HANDLE hProcess = (HANDLE) spawnlp(P_NOWAIT,
"7z",
"7z",
"e",
"-so",
filename,
NULL);
83 if (_dup2(hStdOut, _fileno(stdout)) != 0)
85 fprintf(stderr,
"could not reconstruct stdout\n");
98 static FILE* fopenZIPped(
const char*
filename,
const char* mode)
105 if (_pipe(hPipe, 2048, ((mode[1] ==
'b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1)
107 fprintf(stderr,
"could not create pipe\n");
112 int hStdOut = _dup(_fileno(stdout));
117 fprintf(stderr,
"could not set pipe output\n");
122 if (_dup2(hPipe[
READ_HANDLE], _fileno(stdin)) != 0)
124 fprintf(stderr,
"could not redirect input file\n");
129 close(hPipe[WRITE_HANDLE]);
132 HANDLE hProcess = (HANDLE) spawnlp(P_NOWAIT,
"unzip",
"unzip",
"-p",
filename,
NULL);
135 if (_dup2(hStdOut, _fileno(stdout)) != 0)
137 fprintf(stderr,
"could not reconstruct stdout\n");
150 static FILE* fopenGzipped(
const char*
filename,
const char* mode)
156 FILE* gzipInput = fopen(
filename, mode);
157 if (!gzipInput)
return NULL;
161 if (_pipe(hPipe, 2048, ((mode[1] ==
'b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1)
163 fprintf(stderr,
"could not create pipe\n");
168 int hStdIn = _dup(_fileno(stdin));
170 if (_dup2(_fileno(gzipInput), _fileno(stdin)) != 0)
172 fprintf(stderr,
"could not redirect stdin\n");
177 int hStdOut = _dup(_fileno(stdout));
181 fprintf(stderr,
"could not set pipe output\n");
186 close(hPipe[WRITE_HANDLE]);
189 if (_dup2(hPipe[
READ_HANDLE], _fileno(gzipInput)) != 0)
191 fprintf(stderr,
"could not redirect input file\n");
196 close(hPipe[READ_HANDLE]);
199 HANDLE hProcess = (HANDLE) spawnlp(P_NOWAIT,
"gzip",
"gzip",
"-d",
NULL);
202 if (_dup2(hStdIn, _fileno(stdin)) != 0)
204 fprintf(stderr,
"could not reconstruct stdin\n");
209 if (_dup2(hStdOut, _fileno(stdout)) != 0)
211 fprintf(stderr,
"could not reconstruct stdout\n");
224 static FILE* fopenGzippedNew(
const char*
filename,
const char* mode)
231 if (_pipe(hPipe, 2048, ((mode[1] ==
'b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1)
233 fprintf(stderr,
"could not create pipe\n");
238 int hStdOut = _dup(_fileno(stdout));
243 fprintf(stderr,
"could not set pipe output\n");
248 if (_dup2(hPipe[
READ_HANDLE], _fileno(stdin)) != 0)
250 fprintf(stderr,
"could not redirect input file\n");
255 close(hPipe[WRITE_HANDLE]);
258 HANDLE hProcess = (HANDLE) spawnlp(P_NOWAIT,
"gzip",
"gzip",
"-dc",
filename,
NULL);
261 if (_dup2(hStdOut, _fileno(stdout)) != 0)
263 fprintf(stderr,
"could not reconstruct stdout\n");
276 static FILE* fopenRARed(
const char*
filename,
const char* mode)
283 if (_pipe(hPipe, 2048, ((mode[1] ==
'b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1)
285 fprintf(stderr,
"could not create pipe\n");
290 int hStdOut = _dup(_fileno(stdout));
295 fprintf(stderr,
"could not set pipe output\n");
300 if (_dup2(hPipe[
READ_HANDLE], _fileno(stdin)) != 0)
302 fprintf(stderr,
"could not redirect input file\n");
307 close(hPipe[WRITE_HANDLE]);
310 HANDLE hProcess = (HANDLE) spawnlp(P_NOWAIT,
"unrar",
"unrar",
"p",
"-ierr",
filename,
NULL);
313 if (_dup2(hStdOut, _fileno(stdout)) != 0)
315 fprintf(stderr,
"could not reconstruct stdout\n");
335 if (strstr(filename,
".gz"))
338 file = fopenGzipped(filename, mode);
339 if (piped) *piped =
true;
341 fprintf(stderr,
"ERROR: no support for gzipped input\n");
345 else if (strstr(filename,
".zip"))
348 file = fopenZIPped(filename, mode);
349 if (piped) *piped =
true;
351 fprintf(stderr,
"ERROR: no support for ZIPped input\n");
355 else if (strstr(filename,
".7z"))
358 file = fopen7zipped(filename, mode);
359 if (piped) *piped =
true;
361 fprintf(stderr,
"ERROR: no support for 7zipped input\n");
365 else if (strstr(filename,
".rar"))
368 file = fopenRARed(filename, mode);
369 if (piped) *piped =
true;
371 fprintf(stderr,
"ERROR: no support for RARed input\n");
377 file = fopen(filename, mode);
378 if (piped) *piped =
false;
FILE * fopen_compressed(const char *filename, const char *mode, bool *piped)