| 276 | } |
| 277 | |
| 278 | static FILE* fopenRARed(const char* filename, const char* mode) |
| 279 | { |
| 280 | // check mode |
| 281 | if (mode[0] == 'r') |
| 282 | { |
| 283 | // create the pipe |
| 284 | int hPipe[2]; |
| 285 | if (_pipe(hPipe, 2048, ((mode[1] =='b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1) |
| 286 | { |
| 287 | laserror("could not create pipe"); |
| 288 | return NULL; |
| 289 | } |
| 290 | |
| 291 | // duplicate stdin/stdout handle so we can restore them later |
| 292 | int hStdOut = _dup(_fileno(stdout)); |
| 293 | |
| 294 | // make the write end of pipe go to stdout |
| 295 | if (_dup2(hPipe[WRITE_HANDLE], _fileno(stdout)) != 0) |
| 296 | { |
| 297 | laserror("could not set pipe output"); |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | // redirect read end of pipe to input file |
| 302 | if (_dup2(hPipe[READ_HANDLE], _fileno(stdin)) != 0) |
| 303 | { |
| 304 | laserror("could not redirect input file"); |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | // close original write end of pipe |
| 309 | _close(hPipe[WRITE_HANDLE]); |
| 310 | |
| 311 | // Spawn process |
| 312 | HANDLE hProcess = (HANDLE)_spawnlp(P_NOWAIT, "unrar", "unrar", "p", "-ierr", filename, NULL); |
| 313 | |
| 314 | // redirect stdout back into stdout |
| 315 | if (_dup2(hStdOut, _fileno(stdout)) != 0) |
| 316 | { |
| 317 | laserror("could not reconstruct stdout"); |
| 318 | return NULL; |
| 319 | } |
| 320 | |
| 321 | // return redirected stdin |
| 322 | return stdin; |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | return NULL; |
| 327 | } |
| 328 | } |
| 329 | #endif |
| 330 | |
| 331 | extern "C" |
no test coverage detected