diff --git a/src/function.c b/src/function.c index d36b7951..396f1297 100644 --- a/src/function.c +++ b/src/function.c @@ -2303,7 +2303,7 @@ func_file (char *o, char **argv, const char *funcname UNUSED) } else if (fn[0] == '<') { - char *preo = o; + size_t n = 0; FILE *fp; ++fn; @@ -2327,8 +2327,10 @@ func_file (char *o, char **argv, const char *funcname UNUSED) char buf[1024]; size_t l = fread (buf, 1, sizeof (buf), fp); if (l > 0) - o = variable_buffer_output (o, buf, l); - + { + o = variable_buffer_output (o, buf, l); + n += l; + } if (ferror (fp)) if (errno != EINTR) OSS (fatal, reading_file, _("read: %s: %s"), fn, strerror (errno)); @@ -2339,9 +2341,8 @@ func_file (char *o, char **argv, const char *funcname UNUSED) OSS (fatal, reading_file, _("close: %s: %s"), fn, strerror (errno)); /* Remove trailing newline. */ - if (o > preo && o[-1] == '\n') - if (--o > preo && o[-1] == '\r') - --o; + if (n && o[-1] == '\n') + o -= 1 + (n > 1 && o[-2] == '\r'); } else OS (fatal, *expanding_var, _("file: invalid file operation: %s"), fn); diff --git a/tests/scripts/functions/file b/tests/scripts/functions/file index eaabd3ac..a9bf1119 100644 --- a/tests/scripts/functions/file +++ b/tests/scripts/functions/file @@ -118,6 +118,76 @@ x:;@echo '$(X1)'; echo '$(A)'; echo '$(B)' unlink('file.out'); +# Read an empty file. +touch("file.out"); +run_make_test(q!# empty file +X1 := x$(file +X1 := x$(file +X1 := x$(file +X1 := x$(file +X1 := x$(file : echo to stdout +# out : echo to stdout with a newline +# raw : echo to stdout without adding anything # file : echo to stdout AND create the file # dir : echo to stdout AND create the directory # rm : echo to stdout AND delete the file/directory @@ -34,6 +35,10 @@ sub op { print "$nm\n"; return 1; } + if ($op eq 'raw') { + print "$nm"; + return 1; + } # Show the output before creating the file if ($op eq 'file') {