X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Ffile-name.c;h=edbabd5ed8f871a67374490109a48176f608409d;hb=refs%2Fheads%2Ffbuf;hp=afc99a88250f348d35b576e1a207031e6680e658;hpb=3db83b515247dca69abbf7ad05d3dbdfec4b524c;p=pspp diff --git a/src/data/file-name.c b/src/data/file-name.c index afc99a8825..edbabd5ed8 100644 --- a/src/data/file-name.c +++ b/src/data/file-name.c @@ -142,7 +142,7 @@ safety_violation (const char *fn) NULL on failure. If NULL is returned then errno is set to a sensible value. */ FILE * -fn_open (const struct file_handle *fh, const char *mode) +fn_fopen (const struct file_handle *fh, const char *mode) { const char *fn = fh_get_file_name (fh); @@ -204,7 +204,46 @@ fn_open (const struct file_handle *fh, const char *mode) #endif } -/* Counterpart to fn_open that closes file F with name FN; returns 0 +/* File open routine that understands `-' as stdin/stdout. Returns file + descriptor on success, otherwise a negative errno value. */ +int +fn_open (const struct file_handle *fh, int flags, mode_t mode) +{ + const char *fn = fh_get_file_name (fh); + + int orig_fd = -1; + if ((flags & O_ACCMODE) == O_RDONLY) + { + if (!strcmp (fn, "stdin") || !strcmp (fn, "-")) + orig_fd = STDIN_FILENO; + } + else + { + if (!strcmp (fn, "stdout") || !strcmp (fn, "-")) + orig_fd = STDOUT_FILENO; + else if (!strcmp (fn, "stderr")) + orig_fd = STDERR_FILENO; + } + if (orig_fd >= 0) + { + int fd = dup (orig_fd); + return fd >= 0 ? fd : -errno; + } + +#if WIN32 + wchar_t *ss = convert_to_filename_encoding (fn, strlen (fn), fh_get_file_name_encoding (fh)); + wchar_t *m = (wchar_t *) recode_string ("UTF-16LE", "ASCII", mode, strlen (mode)); + int fd = _wopen (fn, flags, mode); + free (m); + free (ss); +#else + int fd = open (fn, flags, mode); +#endif + + return fd >= 0 ? fd : -errno; +} + +/* Counterpart to fn_fopen that closes file F with name FN; returns 0 on success, EOF on failure. If EOF is returned, errno is set to a sensible value. */ int