| 984 | however when the files are unlinked. */ |
| 985 | |
| 986 | static FILE * |
| 987 | stream_open (char const *file, char const *how) |
| 988 | { |
| 989 | FILE *fp; |
| 990 | |
| 991 | if (*how == 'r') |
| 992 | { |
| 993 | if (streq (file, "-")) |
| 994 | { |
| 995 | have_read_stdin = true; |
| 996 | fp = stdin; |
| 997 | } |
| 998 | else |
| 999 | { |
| 1000 | int fd = open (file, O_RDONLY | O_CLOEXEC); |
| 1001 | fp = fd < 0 ? NULL : fdopen (fd, how); |
| 1002 | } |
| 1003 | fadvise (fp, FADVISE_SEQUENTIAL); |
| 1004 | } |
| 1005 | else if (*how == 'w') |
| 1006 | { |
| 1007 | if (file && ftruncate (STDOUT_FILENO, 0) != 0) |
| 1008 | { |
| 1009 | int ftruncate_errno = errno; |
| 1010 | struct stat *outst = get_outstatus (); |
| 1011 | if (!outst || S_ISREG (outst->st_mode) || S_TYPEISSHM (outst)) |
| 1012 | error (SORT_FAILURE, ftruncate_errno, _("%s: error truncating"), |
| 1013 | quotef (file)); |
| 1014 | } |
| 1015 | fp = stdout; |
| 1016 | } |
| 1017 | else |
| 1018 | affirm (!"unexpected mode passed to stream_open"); |
| 1019 | |
| 1020 | return fp; |
| 1021 | } |
| 1022 | |
| 1023 | /* Same as stream_open, except always return a non-null value; die on |
| 1024 | failure. */ |
no test coverage detected