[SV 41983] Support omitting the text argument to $(file ...)

Reported by Tim Murphy <tnmurphy@gmail.com>
* function.c (func_file): Only write TEXT if it is not NULL.
* NEWS, doc/make.texi: Document the new feature
* tests/scripts/functions/file: Verify that the no-text version of
  $(file ...) works and doesn't add a newline.
diff --git a/NEWS b/NEWS
index 05f4e8d..ae725fc 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,10 @@
 
 http://sv.gnu.org/bugs/index.php?group=make&report_id=111&fix_release_id=105&set=custom
 
+* Allow a no-text-argument form of the $(file ...) function.  Without a text
+  argument nothing is written to the file: it is simply opened in the
+  requested mode, then closed again.
+
 * Change the fatal error for mixed explicit and implicit rules, that was
   introduced in GNU make 3.82, to a non-fatal error.  However, this syntax is
   still deprecated and may return to being illegal in a future version of GNU
@@ -771,7 +775,7 @@
   have reversed the change made in version 3.68 because it turned out to
   cause a paradoxical situation in cases like:
 
-	export variable = $(shell echo value)
+        export variable = $(shell echo value)
 
   When Make attempted to put this variable in the environment for a
   recipe, it would try expand the value by running the shell command
@@ -794,8 +798,8 @@
   foo.a(b.o) bar.a(c.o) bar.a(d.o)'.
 
 * A suffix rule `.X.a' now produces two pattern rules:
-	(%.o): %.X	# Previous versions produced only this.
-	%.a: %.X	# Now produces this as well, just like other suffixes.
+        (%.o): %.X	# Previous versions produced only this.
+        %.a: %.X	# Now produces this as well, just like other suffixes.
 
 * The new flag `--warn-undefined-variables' says to issue a warning message
   whenever Make expands a reference to an undefined variable.
@@ -865,15 +869,15 @@
   no longer automatically put into the environments of the recipe lines that
   Make runs.  Instead, only variables specified on the command line or in
   the environment are exported by default.  To export others, use:
-	export VARIABLE
+        export VARIABLE
   or you can define variables with:
-	export VARIABLE = VALUE
+        export VARIABLE = VALUE
   or:
-	export VARIABLE := VALUE
+        export VARIABLE := VALUE
   You can use just:
-	export
+        export
   or:
-	.EXPORT_ALL_VARIABLES:
+        .EXPORT_ALL_VARIABLES:
   to get the old behavior.  See the node `Variables/Recursion' in the manual
   for a full description.
 
@@ -893,9 +897,9 @@
 
 * A single `include' directive can now specify more than one makefile to
   include, like this:
-	include file1 file2
+        include file1 file2
   You can also use shell file name patterns in an `include' directive:
-	include *.mk
+        include *.mk
 
 * The default directories to search for included makefiles, and for
   libraries specified with `-lNAME', are now set by configuration.
@@ -1031,7 +1035,7 @@
 * The `wildcard' variable expansion function now expands ~ and ~USER.
 
 * Messages indicating failed recipe lines now contain the target name:
-	make: *** [target] Error 1
+        make: *** [target] Error 1
 
 * The `-p' output format has been changed somewhat to look more like
   makefile rules and to give all information that Make has about files.
diff --git a/doc/make.texi b/doc/make.texi
index 8fbdb61..21e32de 100644
--- a/doc/make.texi
+++ b/doc/make.texi
@@ -1528,7 +1528,7 @@
 
 @example
 @var{immediate} : @var{immediate} ; @var{deferred}
-	@var{deferred}
+        @var{deferred}
 @end example
 
 That is, the target and prerequisite sections are expanded immediately,
@@ -7516,7 +7516,7 @@
 The syntax of the @code{file} function is:
 
 @example
-$(file @var{op} @var{filename},@var{text})
+$(file @var{op} @var{filename}[,@var{text}])
 @end example
 
 The operator @var{op} can be either @code{>} which indicates overwrite
@@ -7528,8 +7528,9 @@
 expanded first, then the file indicated by @var{filename} will be
 opened in the mode described by @var{op}.  Finally @var{text} will be
 written to the file.  If @var{text} does not already end in a newline,
-a final newline will be written.  The result of evaluating the
-@code{file} function is always the empty string.
+even if empty, a final newline will be written.  If the @var{text}
+argument is not given, nothing will be written.  The result of
+evaluating the @code{file} function is always the empty string.
 
 It is a fatal error if the file cannot be opened for writing, or if
 the write operation fails.
@@ -7556,7 +7557,7 @@
 @example
 @group
 program: $(OBJECTS)
-        $(file >$@@.in,) $(foreach O,$^,$(file >>$@@.in,$O))
+        $(file >$@@.in) $(foreach O,$^,$(file >>$@@.in,$O))
         $(CMD) $(CMDFLAGS) @@$@@.in
         @@rm $@@.in
 @end group
diff --git a/function.c b/function.c
index 72ecb7f..bb62187 100644
--- a/function.c
+++ b/function.c
@@ -2154,18 +2154,18 @@
           const char *err = strerror (errno);
           OSS (fatal, reading_file, _("open: %s: %s"), fn, err);
         }
-      else
+      if (argv[1])
         {
           int l = strlen (argv[1]);
-          int nl = (l == 0 || argv[1][l-1] != '\n');
+          int nl = l == 0 || argv[1][l-1] != '\n';
 
           if (fputs (argv[1], fp) == EOF || (nl && fputc ('\n', fp) == EOF))
             {
               const char *err = strerror (errno);
               OSS (fatal, reading_file, _("write: %s: %s"), fn, err);
             }
-          fclose (fp);
         }
+      fclose (fp);
     }
   else
     OS (fatal, reading_file, _("Invalid file operation: %s"), fn);
diff --git a/tests/scripts/functions/file b/tests/scripts/functions/file
index 9a4cd02..55eb58a 100644
--- a/tests/scripts/functions/file
+++ b/tests/scripts/functions/file
@@ -30,6 +30,23 @@
 
 unlink('file.out');
 
+# Test > with no content
+run_make_test(q!
+$(file >4touch)
+.PHONY:x
+x:;@cat 4touch
+!,
+              '', '');
+
+# Test >> with no content
+run_make_test(q!
+$(file >>4touch)
+.PHONY:x
+x:;@cat 4touch
+!,
+              '', '');
+unlink('4touch');
+
 # Test > to a read-only file
 touch('file.out');
 chmod(0444, 'file.out');