| 46 | |
| 47 | #ifdef _WIN32 |
| 48 | static FILE* fopen7zipped(const char* filename, const char* mode) |
| 49 | { |
| 50 | // check mode |
| 51 | if (mode[0] == 'r') |
| 52 | { |
| 53 | // create the pipe |
| 54 | int hPipe[2]; |
| 55 | if (_pipe(hPipe, 2048, ((mode[1] =='b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1) |
| 56 | { |
| 57 | laserror("could not create pipe"); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | // duplicate stdin/stdout handle so we can restore them later |
| 62 | int hStdOut = _dup(_fileno(stdout)); |
| 63 | |
| 64 | // make the write end of pipe go to stdout |
| 65 | if (_dup2(hPipe[WRITE_HANDLE], _fileno(stdout)) != 0) |
| 66 | { |
| 67 | laserror("could not set pipe output"); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | // redirect read end of pipe to input file |
| 72 | if (_dup2(hPipe[READ_HANDLE], _fileno(stdin)) != 0) |
| 73 | { |
| 74 | laserror("could not redirect input file"); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | // close original write end of pipe |
| 79 | _close(hPipe[WRITE_HANDLE]); |
| 80 | |
| 81 | // Spawn process |
| 82 | HANDLE hProcess = (HANDLE)_spawnlp(P_NOWAIT, "7z", "7z", "e", "-so", filename, NULL); |
| 83 | |
| 84 | // redirect stdout back into stdout |
| 85 | if (_dup2(hStdOut, _fileno(stdout)) != 0) |
| 86 | { |
| 87 | laserror("could not reconstruct stdout"); |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | // return redirected stdin |
| 92 | return stdin; |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | return NULL; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static FILE* fopenZIPped(const char* filename, const char* mode) |
| 101 | { |
no test coverage detected