* openQueryOutputFile --- attempt to open a query output file * * fname == NULL selects stdout, else an initial '|' selects a pipe, * else plain file. * * Returns output file pointer into *fout, and is-a-pipe flag into *is_pipe. * Caller is responsible for adjusting SIGPIPE state if it's a pipe. * * On error, reports suitable error message and returns false. */
| 47 | * On error, reports suitable error message and returns false. |
| 48 | */ |
| 49 | bool |
| 50 | openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe) |
| 51 | { |
| 52 | if (!fname || fname[0] == '\0') |
| 53 | { |
| 54 | *fout = stdout; |
| 55 | *is_pipe = false; |
| 56 | } |
| 57 | else if (*fname == '|') |
| 58 | { |
| 59 | *fout = popen(fname + 1, "w"); |
| 60 | *is_pipe = true; |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | *fout = fopen(fname, "w"); |
| 65 | *is_pipe = false; |
| 66 | } |
| 67 | |
| 68 | if (*fout == NULL) |
| 69 | { |
| 70 | pg_log_error("%s: %m", fname); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * setQFout |
no outgoing calls
no test coverage detected