harden the crlf vs lf tests even more; use binary mode explicitly on OS/2

This commit is contained in:
tg 2015-07-09 20:52:43 +00:00
parent dcd8b6389b
commit edf76ec8e6
10 changed files with 80 additions and 43 deletions

View File

@ -1,5 +1,5 @@
#!/bin/sh #!/bin/sh
srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.687 2015/07/09 20:11:44 tg Exp $' srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.688 2015/07/09 20:52:34 tg Exp $'
#- #-
# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, # Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
# 2011, 2012, 2013, 2014, 2015 # 2011, 2012, 2013, 2014, 2015
@ -1673,6 +1673,7 @@ ac_header sys/select.h sys/types.h
ac_header sys/sysmacros.h ac_header sys/sysmacros.h
ac_header bstring.h ac_header bstring.h
ac_header grp.h sys/types.h ac_header grp.h sys/types.h
ac_header io.h
ac_header libgen.h ac_header libgen.h
ac_header libutil.h sys/types.h ac_header libutil.h sys/types.h
ac_header paths.h ac_header paths.h
@ -2340,7 +2341,7 @@ addsrcs '!' HAVE_STRLCPY strlcpy.c
addsrcs USE_PRINTF_BUILTIN printf.c addsrcs USE_PRINTF_BUILTIN printf.c
test 1 = "$USE_PRINTF_BUILTIN" && add_cppflags -DMKSH_PRINTF_BUILTIN test 1 = "$USE_PRINTF_BUILTIN" && add_cppflags -DMKSH_PRINTF_BUILTIN
test 1 = "$HAVE_CAN_VERB" && CFLAGS="$CFLAGS -verbose" test 1 = "$HAVE_CAN_VERB" && CFLAGS="$CFLAGS -verbose"
add_cppflags -DMKSH_BUILD_R=510 add_cppflags -DMKSH_BUILD_R=511
$e $bi$me: Finished configuration testing, now producing output.$ao $e $bi$me: Finished configuration testing, now producing output.$ao

View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/Makefile,v 1.141 2015/04/11 23:28:17 tg Exp $ # $MirOS: src/bin/mksh/Makefile,v 1.142 2015/07/09 20:52:36 tg Exp $
#- #-
# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, # Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
# 2011, 2012, 2013, 2014, 2015 # 2011, 2012, 2013, 2014, 2015
@ -43,21 +43,22 @@ CPPFLAGS+= -DMKSH_ASSUME_UTF8 -DMKSH_DISABLE_DEPRECATED \
-DHAVE_SYS_MKDEV_H=0 -DHAVE_SYS_MMAN_H=1 -DHAVE_SYS_PARAM_H=1 \ -DHAVE_SYS_MKDEV_H=0 -DHAVE_SYS_MMAN_H=1 -DHAVE_SYS_PARAM_H=1 \
-DHAVE_SYS_RESOURCE_H=1 -DHAVE_SYS_SELECT_H=1 \ -DHAVE_SYS_RESOURCE_H=1 -DHAVE_SYS_SELECT_H=1 \
-DHAVE_SYS_SYSMACROS_H=0 -DHAVE_BSTRING_H=0 -DHAVE_GRP_H=1 \ -DHAVE_SYS_SYSMACROS_H=0 -DHAVE_BSTRING_H=0 -DHAVE_GRP_H=1 \
-DHAVE_LIBGEN_H=1 -DHAVE_LIBUTIL_H=0 -DHAVE_PATHS_H=1 \ -DHAVE_IO_H=0 -DHAVE_LIBGEN_H=1 -DHAVE_LIBUTIL_H=0 \
-DHAVE_STDINT_H=1 -DHAVE_STRINGS_H=1 -DHAVE_TERMIOS_H=1 \ -DHAVE_PATHS_H=1 -DHAVE_STDINT_H=1 -DHAVE_STRINGS_H=1 \
-DHAVE_ULIMIT_H=0 -DHAVE_VALUES_H=0 -DHAVE_CAN_INTTYPES=1 \ -DHAVE_TERMIOS_H=1 -DHAVE_ULIMIT_H=0 -DHAVE_VALUES_H=0 \
-DHAVE_CAN_UCBINTS=1 -DHAVE_CAN_INT8TYPE=1 \ -DHAVE_CAN_INTTYPES=1 -DHAVE_CAN_UCBINTS=1 \
-DHAVE_CAN_UCBINT8=1 -DHAVE_RLIM_T=1 -DHAVE_SIG_T=1 \ -DHAVE_CAN_INT8TYPE=1 -DHAVE_CAN_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YS_SIGLIST_DECL=1 \
-DHAVE_PERSISTENT_HISTORY=1 -DMKSH_BUILD_R=510 -DHAVE_PERSISTENT_HISTORY=1 -DMKSH_BUILD_R=511
CPPFLAGS+= -D${${PROG:L}_tf:C/(Mir${MAN:E}{0,1}){2}/4/:S/x/mksh_BUILD/:U} CPPFLAGS+= -D${${PROG:L}_tf:C/(Mir${MAN:E}{0,1}){2}/4/:S/x/mksh_BUILD/:U}
CPPFLAGS+= -I. CPPFLAGS+= -I.
COPTS+= -std=c89 -Wall COPTS+= -std=c89 -Wall

16
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.701 2015/07/09 20:11:45 tg Exp $ # $MirOS: src/bin/mksh/check.t,v 1.702 2015/07/09 20:52:36 tg Exp $
# -*- mode: sh -*- # -*- mode: sh -*-
#- #-
# Copyright © 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, # Copyright © 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
@ -30,7 +30,7 @@
# (2013/12/02 20:39:44) http://openbsd.cs.toronto.edu/cgi-bin/cvsweb/src/regress/bin/ksh/?sortby=date # (2013/12/02 20:39:44) http://openbsd.cs.toronto.edu/cgi-bin/cvsweb/src/regress/bin/ksh/?sortby=date
expected-stdout: expected-stdout:
@(#)MIRBSD KSH R51 2015/07/06 @(#)MIRBSD KSH R51 2015/07/09
description: description:
Check version of shell. Check version of shell.
stdin: stdin:
@ -39,7 +39,7 @@ name: KSH_VERSION
category: shell:legacy-no category: shell:legacy-no
--- ---
expected-stdout: expected-stdout:
@(#)LEGACY KSH R51 2015/07/06 @(#)LEGACY KSH R51 2015/07/09
description: description:
Check version of legacy shell. Check version of legacy shell.
stdin: stdin:
@ -8613,6 +8613,7 @@ stdin:
cat >foo <<-'EOF' cat >foo <<-'EOF'
x='bar x='bar
' # ' #
echo .${#x} #
if test x"$KSH_VERSION" = x""; then # if test x"$KSH_VERSION" = x""; then #
printf '<%s>' "$x" # printf '<%s>' "$x" #
else # else #
@ -8624,8 +8625,10 @@ stdin:
print -r -- "{$line}" print -r -- "{$line}"
done done
expected-stdout: expected-stdout:
[<bar [.5
<bar
>] >]
{.5}
{<bar } {<bar }
--- ---
name: print-lf name: print-lf
@ -8635,6 +8638,7 @@ stdin:
cat >foo <<-'EOF' cat >foo <<-'EOF'
x='bar x='bar
' # ' #
echo .${#x} #
if test x"$KSH_VERSION" = x""; then # if test x"$KSH_VERSION" = x""; then #
printf '<%s>' "$x" # printf '<%s>' "$x" #
else # else #
@ -8646,8 +8650,10 @@ stdin:
print -r -- "{$line}" print -r -- "{$line}"
done done
expected-stdout: expected-stdout:
[<bar [.4
<bar
>] >]
{.4}
{<bar} {<bar}
--- ---
name: print-nul-chars name: print-nul-chars

8
exec.c
View File

@ -23,7 +23,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.158 2015/07/09 20:20:42 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/exec.c,v 1.159 2015/07/09 20:52:38 tg Exp $");
#ifndef MKSH_DEFAULT_EXECSHELL #ifndef MKSH_DEFAULT_EXECSHELL
#define MKSH_DEFAULT_EXECSHELL "/bin/sh" #define MKSH_DEFAULT_EXECSHELL "/bin/sh"
@ -925,7 +925,7 @@ scriptexec(struct op *tp, const char **ap)
*tp->args-- = tp->str; *tp->args-- = tp->str;
#ifndef MKSH_SMALL #ifndef MKSH_SMALL
if ((fd = open(tp->str, O_RDONLY | O_BINARY)) >= 0) { if ((fd = binopen2(tp->str, O_RDONLY)) >= 0) {
unsigned char *cp; unsigned char *cp;
unsigned short m; unsigned short m;
ssize_t n; ssize_t n;
@ -1445,7 +1445,7 @@ iosetup(struct ioword *iop, struct tbl *tp)
warningf(true, "%s: %s", cp, "restricted"); warningf(true, "%s: %s", cp, "restricted");
return (-1); return (-1);
} }
u = open(cp, flags | O_BINARY, 0666); u = binopen3(cp, flags, 0666);
} }
if (u < 0) { if (u < 0) {
/* herein() may already have printed message */ /* herein() may already have printed message */
@ -1578,7 +1578,7 @@ herein(struct ioword *iop, char **resbuf)
* so temp doesn't get removed too soon). * so temp doesn't get removed too soon).
*/ */
h = maketemp(ATEMP, TT_HEREDOC_EXP, &e->temps); h = maketemp(ATEMP, TT_HEREDOC_EXP, &e->temps);
if (!(shf = h->shf) || (fd = open(h->tffn, O_RDONLY | O_BINARY, 0)) < 0) { if (!(shf = h->shf) || (fd = binopen3(h->tffn, O_RDONLY, 0)) < 0) {
i = errno; i = errno;
warningf(true, "can't %s temporary file %s: %s", warningf(true, "can't %s temporary file %s: %s",
!shf ? "create" : "open", h->tffn, cstrerror(i)); !shf ? "create" : "open", h->tffn, cstrerror(i));

View File

@ -38,7 +38,7 @@
#endif #endif
#endif #endif
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.279 2015/07/09 20:20:42 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.280 2015/07/09 20:52:39 tg Exp $");
#if HAVE_KILLPG #if HAVE_KILLPG
/* /*
@ -3668,7 +3668,7 @@ c_cat(const char **wp)
fn = *wp++; fn = *wp++;
if (ksh_isdash(fn)) if (ksh_isdash(fn))
fd = STDIN_FILENO; fd = STDIN_FILENO;
else if ((fd = open(fn, O_RDONLY | O_BINARY)) < 0) { else if ((fd = binopen2(fn, O_RDONLY)) < 0) {
eno = errno; eno = errno;
bi_errorf("%s: %s", fn, cstrerror(eno)); bi_errorf("%s: %s", fn, cstrerror(eno));
rv = 1; rv = 1;

View File

@ -27,7 +27,7 @@
#include <sys/file.h> #include <sys/file.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.148 2015/07/05 19:53:45 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.149 2015/07/09 20:52:40 tg Exp $");
Trap sigtraps[ksh_NSIG + 1]; Trap sigtraps[ksh_NSIG + 1];
static struct sigaction Sigact_ign; static struct sigaction Sigact_ign;
@ -764,8 +764,7 @@ hist_init(Source *s)
retry: retry:
/* we have a file and are interactive */ /* we have a file and are interactive */
if ((fd = open(hname, O_RDWR | O_CREAT | O_APPEND | O_BINARY, if ((fd = binopen3(hname, O_RDWR | O_CREAT | O_APPEND, 0600)) < 0)
0600)) < 0)
return; return;
histfd = savefd(fd); histfd = savefd(fd);
@ -800,8 +799,8 @@ hist_init(Source *s)
/* create temporary file */ /* create temporary file */
nhname = shf_smprintf("%s.%d", hname, (int)procpid); nhname = shf_smprintf("%s.%d", hname, (int)procpid);
if ((fd = open(nhname, O_RDWR | O_CREAT | O_TRUNC | if ((fd = binopen3(nhname, O_RDWR | O_CREAT | O_TRUNC |
O_EXCL | O_BINARY, 0600)) < 0) { O_EXCL, 0600)) < 0) {
/* just don't truncate then, meh. */ /* just don't truncate then, meh. */
goto hist_trunc_dont; goto hist_trunc_dont;
} }

10
main.c
View File

@ -34,7 +34,7 @@
#include <locale.h> #include <locale.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.298 2015/07/09 19:46:42 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/main.c,v 1.299 2015/07/09 20:52:40 tg Exp $");
extern char **environ; extern char **environ;
@ -193,6 +193,12 @@ main_init(int argc, const char *argv[], Source **sp, struct block **lp)
ssize_t k; ssize_t k;
#endif #endif
#ifdef __OS2__
for (i = 0; i < 3; ++i)
if (!isatty(i))
setmode(i, O_BINARY);
#endif
/* do things like getpgrp() et al. */ /* do things like getpgrp() et al. */
chvt_reinit(); chvt_reinit();
@ -1637,7 +1643,7 @@ maketemp(Area *ap, Temp_type type, struct temp **tlist)
} while (len < 6); } while (len < 6);
/* check if this one works */ /* check if this one works */
if ((i = open(tp->tffn, O_CREAT | O_EXCL | O_RDWR | O_BINARY, if ((i = binopen3(tp->tffn, O_CREAT | O_EXCL | O_RDWR,
0600)) < 0 && errno != EEXIST) 0600)) < 0 && errno != EEXIST)
goto maketemp_out; goto maketemp_out;
} while (i < 0); } while (i < 0);

6
misc.c
View File

@ -30,7 +30,7 @@
#include <grp.h> #include <grp.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.236 2015/07/05 14:58:33 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/misc.c,v 1.237 2015/07/09 20:52:41 tg Exp $");
#define KSH_CHVT_FLAG #define KSH_CHVT_FLAG
#ifdef MKSH_SMALL #ifdef MKSH_SMALL
@ -2009,9 +2009,9 @@ chvt(const Getopt *go)
#endif #endif
} }
} }
if ((fd = open(dv, O_RDWR | O_BINARY)) < 0) { if ((fd = binopen2(dv, O_RDWR)) < 0) {
sleep(1); sleep(1);
if ((fd = open(dv, O_RDWR | O_BINARY)) < 0) { if ((fd = binopen2(dv, O_RDWR)) < 0) {
errorf("%s: %s %s", "chvt", "can't open", dv); errorf("%s: %s %s", "chvt", "can't open", dv);
} }
} }

30
sh.h
View File

@ -64,6 +64,9 @@
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#if HAVE_IO_H
#include <io.h>
#endif
#if HAVE_LIBGEN_H #if HAVE_LIBGEN_H
#include <libgen.h> #include <libgen.h>
#endif #endif
@ -169,9 +172,9 @@
#endif #endif
#ifdef EXTERN #ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.737 2015/07/09 20:20:45 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/sh.h,v 1.738 2015/07/09 20:52:42 tg Exp $");
#endif #endif
#define MKSH_VERSION "R51 2015/07/06" #define MKSH_VERSION "R51 2015/07/09"
/* arithmetic types: C implementation */ /* arithmetic types: C implementation */
#if !HAVE_CAN_INTTYPES #if !HAVE_CAN_INTTYPES
@ -560,7 +563,7 @@ char *ucstrstr(char *, const char *);
#define mkssert(e) do { } while (/* CONSTCOND */ 0) #define mkssert(e) do { } while (/* CONSTCOND */ 0)
#endif #endif
#if (!defined(MKSH_BUILDMAKEFILE4BSD) && !defined(MKSH_BUILDSH)) || (MKSH_BUILD_R != 510) #if (!defined(MKSH_BUILDMAKEFILE4BSD) && !defined(MKSH_BUILDSH)) || (MKSH_BUILD_R != 511)
#error Must run Build.sh to compile this. #error Must run Build.sh to compile this.
extern void thiswillneverbedefinedIhope(void); extern void thiswillneverbedefinedIhope(void);
int int
@ -2081,6 +2084,27 @@ EXTERN bool tty_hasstate; /* true if tty_state is valid */
extern int tty_init_fd(void); /* initialise tty_fd, tty_devtty */ extern int tty_init_fd(void); /* initialise tty_fd, tty_devtty */
#ifdef __OS2__
#ifndef __GNUC__
# error oops?
#endif
#define binopen2(path,flags) __extension__({ \
int binopen2_fd = open((path), (flags) | O_BINARY); \
if (binopen2_fd >= 0) \
setmode(binopen2_fd, O_BINARY); \
(binopen2_fd); \
})
#define binopen3(path,flags,mode) __extension__({ \
int binopen3_fd = open((path), (flags) | O_BINARY, (mode)); \
if (binopen3_fd >= 0) \
setmode(binopen3_fd, O_BINARY); \
(binopen3_fd); \
})
#else
#define binopen2(path,flags) open((path), (flags) | O_BINARY)
#define binopen3(path,flags,mode) open((path), (flags) | O_BINARY, (mode))
#endif
/* be sure not to interfere with anyone else's idea about EXTERN */ /* be sure not to interfere with anyone else's idea about EXTERN */
#ifdef EXTERN_DEFINED #ifdef EXTERN_DEFINED
# undef EXTERN_DEFINED # undef EXTERN_DEFINED

4
shf.c
View File

@ -25,7 +25,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.65 2015/04/29 20:07:35 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/shf.c,v 1.66 2015/07/09 20:52:43 tg Exp $");
/* flags to shf_emptybuf() */ /* flags to shf_emptybuf() */
#define EB_READSW 0x01 /* about to switch to reading */ #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; shf->flags = SHF_ALLOCS;
/* Rest filled in by reopen. */ /* Rest filled in by reopen. */
fd = open(name, oflags | O_BINARY, mode); fd = binopen3(name, oflags, mode);
if (fd < 0) { if (fd < 0) {
eno = errno; eno = errno;
afree(shf, shf->areap); afree(shf, shf->areap);