add O_BINARY to all open() calls except tty_fd

cf. http://svn.netlabs.org/libc/wiki/Faq#Whydoesntreadfdbufsize_of_filereturnsize_of_file
This commit is contained in:
tg 2013-10-09 11:59:30 +00:00
parent 7f16464902
commit f920d94785
8 changed files with 28 additions and 21 deletions

View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.633 2013/09/24 20:19:40 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.634 2013/10/09 11:59:26 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -31,7 +31,7 @@
# http://www.freebsd.org/cgi/cvsweb.cgi/src/tools/regression/bin/test/regress.sh?rev=HEAD
expected-stdout:
@(#)MIRBSD KSH R48 2013/09/24
@(#)MIRBSD KSH R48 2013/10/08
description:
Check version of shell.
stdin:
@ -40,7 +40,7 @@ name: KSH_VERSION
category: shell:legacy-no
---
expected-stdout:
@(#)LEGACY KSH R48 2013/09/24
@(#)LEGACY KSH R48 2013/10/08
description:
Check version of legacy shell.
stdin:

8
exec.c
View File

@ -23,7 +23,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.126 2013/09/10 16:30:49 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.127 2013/10/09 11:59:27 tg Exp $");
#ifndef MKSH_DEFAULT_EXECSHELL
#define MKSH_DEFAULT_EXECSHELL "/bin/sh"
@ -869,7 +869,7 @@ scriptexec(struct op *tp, const char **ap)
*tp->args-- = tp->str;
#ifndef MKSH_SMALL
if ((fd = open(tp->str, O_RDONLY)) >= 0) {
if ((fd = open(tp->str, O_RDONLY | O_BINARY)) >= 0) {
/* read first MAXINTERP octets from file */
if (read(fd, buf, sizeof(buf)) <= 0)
/* read error -> no good */
@ -1374,7 +1374,7 @@ iosetup(struct ioword *iop, struct tbl *tp)
warningf(true, "%s: %s", cp, "restricted");
return (-1);
}
u = open(cp, flags, 0666);
u = open(cp, flags | O_BINARY, 0666);
}
if (u < 0) {
/* herein() may already have printed message */
@ -1507,7 +1507,7 @@ herein(struct ioword *iop, char **resbuf)
* so temp doesn't get removed too soon).
*/
h = maketemp(ATEMP, TT_HEREDOC_EXP, &e->temps);
if (!(shf = h->shf) || (fd = open(h->tffn, O_RDONLY, 0)) < 0) {
if (!(shf = h->shf) || (fd = open(h->tffn, O_RDONLY | O_BINARY, 0)) < 0) {
i = errno;
warningf(true, "can't %s temporary file %s: %s",
!shf ? "create" : "open", h->tffn, cstrerror(i));

View File

@ -38,7 +38,7 @@
#endif
#endif
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.246 2013/09/10 17:33:00 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.247 2013/10/09 11:59:27 tg Exp $");
#if HAVE_KILLPG
/*
@ -3743,7 +3743,7 @@ c_cat(const char **wp)
fn = *wp++;
if (fn[0] == '-' && fn[1] == '\0')
fd = STDIN_FILENO;
else if ((fd = open(fn, O_RDONLY)) < 0) {
else if ((fd = open(fn, O_RDONLY | O_BINARY)) < 0) {
eno = errno;
bi_errorf("%s: %s", fn, cstrerror(eno));
rv = 1;

View File

@ -27,7 +27,7 @@
#include <sys/file.h>
#endif
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.132 2013/09/24 20:19:44 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.133 2013/10/09 11:59:28 tg Exp $");
Trap sigtraps[NSIG + 1];
static struct sigaction Sigact_ign;
@ -720,7 +720,8 @@ hist_init(Source *s)
retry:
/* we have a file and are interactive */
if ((fd = open(hname, O_RDWR | O_CREAT | O_APPEND, 0600)) < 0)
if ((fd = open(hname, O_RDWR | O_CREAT | O_APPEND | O_BINARY,
0600)) < 0)
return;
histfd = savefd(fd);
@ -756,7 +757,7 @@ hist_init(Source *s)
/* create temporary file */
nhname = shf_smprintf("%s.%d", hname, (int)procpid);
if ((fd = open(nhname, O_RDWR | O_CREAT | O_TRUNC |
O_EXCL, 0600)) < 0) {
O_EXCL | O_BINARY, 0600)) < 0) {
/* just don't truncate then, meh. */
goto hist_trunc_dont;
}

5
main.c
View File

@ -34,7 +34,7 @@
#include <locale.h>
#endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.270 2013/09/10 17:33:02 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.271 2013/10/09 11:59:29 tg Exp $");
extern char **environ;
@ -1658,7 +1658,8 @@ maketemp(Area *ap, Temp_type type, struct temp **tlist)
} while (len < 5);
/* cyclically attempt to open a temporary file */
while ((i = open(tp->tffn, O_CREAT | O_EXCL | O_RDWR, 0600)) < 0) {
while ((i = open(tp->tffn, O_CREAT | O_EXCL | O_RDWR | O_BINARY,
0600)) < 0) {
if (errno != EEXIST)
goto maketemp_out;
/* count down from z to a then from 9 to 0 */

6
misc.c
View File

@ -30,7 +30,7 @@
#include <grp.h>
#endif
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.215 2013/08/23 14:07:36 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.216 2013/10/09 11:59:29 tg Exp $");
#define KSH_CHVT_FLAG
#ifdef MKSH_SMALL
@ -2016,9 +2016,9 @@ chvt(const Getopt *go)
#endif
}
}
if ((fd = open(dv, O_RDWR)) < 0) {
if ((fd = open(dv, O_RDWR | O_BINARY)) < 0) {
sleep(1);
if ((fd = open(dv, O_RDWR)) < 0) {
if ((fd = open(dv, O_RDWR | O_BINARY)) < 0) {
errorf("%s: %s %s", "chvt", "can't open", dv);
}
}

9
sh.h
View File

@ -164,9 +164,9 @@
#endif
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.671 2013/09/24 20:19:44 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.672 2013/10/09 11:59:29 tg Exp $");
#endif
#define MKSH_VERSION "R48 2013/09/24"
#define MKSH_VERSION "R48 2013/10/08"
/* arithmetic types: C implementation */
#if !HAVE_CAN_INTTYPES
@ -396,6 +396,10 @@ extern int __cdecl setegid(gid_t);
#endif
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifdef MKSH__NO_SYMLINK
#undef S_ISLNK
#define S_ISLNK(m) (/* CONSTCOND */ 0)
@ -2064,6 +2068,7 @@ Test_op test_isop(Test_meta, const char *);
int test_eval(Test_env *, Test_op, const char *, const char *, bool);
int test_parse(Test_env *);
/* tty_fd is not opened O_BINARY, it's thus never read/written */
EXTERN int tty_fd E_INIT(-1); /* dup'd tty file descriptor */
EXTERN bool tty_devtty; /* true if tty_fd is from /dev/tty */
EXTERN mksh_ttyst tty_state; /* saved tty state */

4
shf.c
View File

@ -25,7 +25,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.61 2013/07/21 18:36:03 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.62 2013/10/09 11:59:30 tg Exp $");
/* flags to shf_emptybuf() */
#define EB_READSW 0x01 /* about to switch to reading */
@ -62,7 +62,7 @@ shf_open(const char *name, int oflags, int mode, int sflags)
shf->flags = SHF_ALLOCS;
/* Rest filled in by reopen. */
fd = open(name, oflags, mode);
fd = open(name, oflags | O_BINARY, mode);
if (fd < 0) {
eno = errno;
afree(shf, shf->areap);