From 7d3643d7b4a5891b49e004f67390cfdad5e84290 Mon Sep 17 00:00:00 2001 From: tg Date: Sat, 2 Apr 2011 10:30:11 +0000 Subject: [PATCH] =?UTF-8?q?skip=20the=20UTF-8=20BOM=20early,=20then=20chec?= =?UTF-8?q?k=20the=20magic=20(ELF,=20a.out,=20COFF,=20=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- check.t | 4 ++-- exec.c | 19 +++++++++++++------ sh.h | 4 ++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/check.t b/check.t index 7ff6112..c34bea2 100644 --- a/check.t +++ b/check.t @@ -1,4 +1,4 @@ -# $MirOS: src/bin/mksh/check.t,v 1.442 2011/03/28 21:58:06 tg Exp $ +# $MirOS: src/bin/mksh/check.t,v 1.443 2011/04/02 10:30:09 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 $ @@ -25,7 +25,7 @@ # http://www.research.att.com/~gsf/public/ifs.sh expected-stdout: - @(#)MIRBSD KSH R39 2011/03/28 + @(#)MIRBSD KSH R39 2011/04/01 description: Check version of shell. stdin: diff --git a/exec.c b/exec.c index c6590fa..1660055 100644 --- a/exec.c +++ b/exec.c @@ -22,7 +22,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.88 2011/03/13 01:20:18 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.89 2011/04/02 10:30:10 tg Exp $"); #ifndef MKSH_DEFAULT_EXECSHELL #define MKSH_DEFAULT_EXECSHELL "/bin/sh" @@ -847,8 +847,15 @@ scriptexec(struct op *tp, const char **ap) /* read error -> no good */ buf[0] = '\0'; close(fd); - /* scan for newline (or CR) or NUL _before_ end of buffer */ + + /* skip UTF-8 Byte Order Mark, if present */ cp = (unsigned char *)buf; + if ((cp[0] == 0xEF) && (cp[1] == 0xBB) && (cp[2] == 0xBF)) + cp += 3; + /* save begin of shebang for later */ + fd = (char *)cp - buf; /* either 0 or (if BOM) 3 */ + + /* scan for newline (or CR) or NUL _before_ end of buffer */ while ((char *)cp < (buf + sizeof(buf))) if (*cp == '\0' || *cp == '\n' || *cp == '\r') { *cp = '\0'; @@ -858,13 +865,13 @@ scriptexec(struct op *tp, const char **ap) /* if the shebang line is longer than MAXINTERP, bail out */ if ((char *)cp >= (buf + sizeof(buf))) goto noshebang; - /* skip UTF-8 Byte Order Mark, if present */ - cp = (unsigned char *)buf; - if ((cp[0] == 0xEF) && (cp[1] == 0xBB) && (cp[2] == 0xBF)) - cp += 3; + + /* restore begin of shebang position (buf+0 or buf+3) */ + cp = (unsigned char *)(buf + fd); /* bail out if read error (above) or no shebang */ if ((cp[0] != '#') || (cp[1] != '!')) goto noshebang; + cp += 2; /* skip whitespace before shell name */ while (*cp == ' ' || *cp == '\t') diff --git a/sh.h b/sh.h index 0e2480d..5d62db0 100644 --- a/sh.h +++ b/sh.h @@ -154,9 +154,9 @@ #endif #ifdef EXTERN -__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.457 2011/03/28 08:40:42 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.458 2011/04/02 10:30:11 tg Exp $"); #endif -#define MKSH_VERSION "R39 2011/03/28" +#define MKSH_VERSION "R39 2011/04/01" #ifndef MKSH_INCLUDES_ONLY