diff options
Diffstat (limited to 'io.c')
-rw-r--r-- | io.c | 56 |
1 files changed, 41 insertions, 15 deletions
@@ -1551,12 +1551,8 @@ nextrres: } #endif /* HAVE_SOCKETS */ -/* devopen --- handle /dev/std{in,out,err}, /dev/fd/N, regular files */ -/* - * Strictly speaking, "name" is not a "const char *" because we temporarily - * change the string. - */ +/* devopen_simple --- handle "-", /dev/std{in,out,err}, /dev/fd/N */ /* * 9/2014: Flow here is a little messy. @@ -1570,22 +1566,25 @@ nextrres: */ int -devopen(const char *name, const char *mode) +devopen_simple(const char *name, const char *mode, bool try_real_open) { int openfd; char *cp; char *ptr; int flag = 0; - struct inet_socket_info isi; - if (strcmp(name, "-") == 0) - return fileno(stdin); + if (strcmp(name, "-") == 0) { + if (mode[0] == 'r') + return fileno(stdin); + else + return fileno(stdout); + } flag = str2mode(mode); openfd = INVALID_HANDLE; if (do_posix) - goto strictopen; + goto done; if ((openfd = os_devopen(name, flag)) != INVALID_HANDLE) { os_close_on_exec(openfd, name, "file", ""); @@ -1602,7 +1601,7 @@ devopen(const char *name, const char *mode) else if (strcmp(cp, "stderr") == 0 && (flag & O_ACCMODE) == O_WRONLY) openfd = fileno(stderr); else if (do_traditional) - goto strictopen; + goto done; else if (strncmp(cp, "fd/", 3) == 0) { struct stat sbuf; @@ -1613,9 +1612,36 @@ devopen(const char *name, const char *mode) openfd = INVALID_HANDLE; } /* do not set close-on-exec for inherited fd's */ - if (openfd != INVALID_HANDLE) - return openfd; - } else if (do_traditional) { + } +done: + if (try_real_open) + openfd = open(name, flag, 0666); + + return openfd; +} + +/* devopen --- handle /dev/std{in,out,err}, /dev/fd/N, /inet, regular files */ + +/* + * Strictly speaking, "name" is not a "const char *" because we temporarily + * change the string. + */ + +int +devopen(const char *name, const char *mode) +{ + int openfd; + char *cp; + int flag; + struct inet_socket_info isi; + + openfd = devopen_simple(name, mode, false); + if (openfd != INVALID_HANDLE) + return openfd; + + flag = str2mode(mode); + + if (do_traditional) { goto strictopen; } else if (inetfile(name, & isi)) { #ifdef HAVE_SOCKETS @@ -1683,7 +1709,7 @@ strictopen: not permitted. */ struct stat buf; - if (!inetfile(name, NULL) + if (! inetfile(name, NULL) && stat(name, & buf) == 0 && S_ISDIR(buf.st_mode)) errno = EISDIR; } |