From e8066f09dbee5ff4ed4c5acc7c5d5e18201e22a4 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 13 Feb 2012 15:46:14 +0100 Subject: [PATCH] do not open input files with "rt" mode, do not assume "rt" is supported 2012-02-13 Paolo Bonzini * configure.ac: Test whether fopen("foo", "rt") works. * sed/compile.c: Do not open scripts with "rt" mode if it doesn't. * sed/sed.c: Never open input files with "rt" mode. --- ChangeLog | 6 ++++++ configure.ac | 24 ++++++++++++++++++++++++ sed/compile.c | 8 +++++++- sed/sed.c | 4 ++-- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index bc660cf..8d7d540 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2012-02-13 Paolo Bonzini + + * configure.ac: Test whether fopen("foo", "rt") works. + * sed/compile.c: Do not open scripts with "rt" mode if it doesn't. + * sed/sed.c: Never open input files with "rt" mode. + 2012-02-05 Paolo Bonzini Jim Hill diff --git a/configure.ac b/configure.ac index d45ce74..e685d52 100644 --- a/configure.ac +++ b/configure.ac @@ -15,6 +15,30 @@ AC_PROG_CC gl_EARLY gl_INIT gl_DISABLE_THREADS +AC_CACHE_CHECK([whether "rt" can be used with fopen], [sed_cv_fopen_rt], [ +AC_TRY_RUN([ +#include +#include + +int main() +{ + FILE *fp; + int result; + errno = 0; + fp = fopen ("conftest.c", "rt"); + if (fp) fclose (fp); + return fp ? 0 : 1; +}], [sed_cv_fopen_rt=yes], + [sed_cv_fopen_rt=no], + [case $host in + *cygwin* | *mingw*) sed_cv_fopen_rt=yes ;; + *) sed_cv_fopen_rt='assuming no' ;; + esac])]) +if test "$sed_cv_fopen_rt" = yes; then + AC_DEFINE([HAVE_FOPEN_RT], [1], + [Defined if "rt" can be used as a mode to fopen.]) +fi + AC_CACHE_CHECK([whether -lcP is needed], [sed_cv_libcp_needed], [ AC_TRY_RUN([ #include diff --git a/sed/compile.c b/sed/compile.c index 9994a63..d71e67d 100644 --- a/sed/compile.c +++ b/sed/compile.c @@ -1610,7 +1610,13 @@ compile_file(cur_program, cmdfile) prog.file = stdin; if (cmdfile[0] != '-' || cmdfile[1] != '\0') - prog.file = ck_fopen(cmdfile, "rt", true); + { +#ifdef HAVE_FOPEN_RT + prog.file = ck_fopen(cmdfile, "rt", true); +#else + prog.file = ck_fopen(cmdfile, "r", true); +#endif + } cur_input.line = 1; cur_input.name = cmdfile; diff --git a/sed/sed.c b/sed/sed.c index 5167a55..7d366bf 100644 --- a/sed/sed.c +++ b/sed/sed.c @@ -86,8 +86,8 @@ bool follow_symlinks = false; /* How do we edit files in-place? (we don't if NULL) */ char *in_place_extension = NULL; -/* The mode to use to read/write files, either "rt"/"w" or "rb"/"wb". */ -char *read_mode = "rt"; +/* The mode to use to read/write files, either "r"/"w" or "rb"/"wb". */ +char *read_mode = "r"; char *write_mode = "w"; /* Do we need to be pedantically POSIX compliant? */