| 98 | } |
| 99 | |
| 100 | static FILE* fopenZIPped(const char* filename, const char* mode) |
| 101 | { |
| 102 | // check mode |
| 103 | if (mode[0] == 'r') |
| 104 | { |
| 105 | // create the pipe |
| 106 | int hPipe[2]; |
| 107 | if (_pipe(hPipe, 2048, ((mode[1] =='b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1) |
| 108 | { |
| 109 | laserror("could not create pipe"); |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | // duplicate stdin/stdout handle so we can restore them later |
| 114 | int hStdOut = _dup(_fileno(stdout)); |
| 115 | |
| 116 | // make the write end of pipe go to stdout |
| 117 | if (_dup2(hPipe[WRITE_HANDLE], _fileno(stdout)) != 0) |
| 118 | { |
| 119 | laserror("could not set pipe output"); |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | // redirect read end of pipe to input file |
| 124 | if (_dup2(hPipe[READ_HANDLE], _fileno(stdin)) != 0) |
| 125 | { |
| 126 | laserror("could not redirect input file"); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | // close original write end of pipe |
| 131 | _close(hPipe[WRITE_HANDLE]); |
| 132 | |
| 133 | // Spawn process |
| 134 | HANDLE hProcess = (HANDLE)_spawnlp(P_NOWAIT, "unzip", "unzip", "-p", filename, NULL); |
| 135 | |
| 136 | // redirect stdout back into stdout |
| 137 | if (_dup2(hStdOut, _fileno(stdout)) != 0) |
| 138 | { |
| 139 | laserror("could not reconstruct stdout"); |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | // return redirected stdin |
| 144 | return stdin; |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | return NULL; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static FILE* fopenGzipped(const char* filename, const char* mode) |
| 153 | { |
no test coverage detected