Provisional fix for bug #18692 and bug #20161. Reviewed by John
[pspp-builds.git] / src / data / file-name.c
index 1795bd1f6aca34eba9ade3e27e6cc7e810f9da40..cfe91932414e0c4bec928c89cb6efebd043f93ae 100644 (file)
@@ -1,20 +1,18 @@
-/* PSPP - computes sample statistics.
+/* PSPP - a program for statistical analysis.
    Copyright (C) 1997-9, 2000, 2006, 2007 Free Software Foundation, Inc.
 
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
 
@@ -89,7 +87,7 @@ fn_interp_vars (struct substring src, const char *(*getenv) (const char *),
             else if (ss_match_char (&src, '{'))
               ss_get_until (&src, '}', &var_name);
             else
-              ss_get_chars (&src, MIN (1, ss_span (src, ss_cstr (CC_ALNUM))),
+              ss_get_chars (&src, MAX (1, ss_span (src, ss_cstr (CC_ALNUM))),
                             &var_name);
 
             start = ds_length (&dst);
@@ -269,7 +267,7 @@ fn_open (const char *fn, const char *mode)
       if (get_safer_mode ())
        return safety_violation (fn);
 
-      return popen (&fn[1], mode);
+      return popen (&fn[1], mode[0] == 'r' ? "r" : "w");
     }
   else if (*fn && fn[strlen (fn) - 1] == '|')
     {
@@ -283,7 +281,7 @@ fn_open (const char *fn, const char *mode)
       memcpy (s, fn, strlen (fn) - 1);
       s[strlen (fn) - 1] = 0;
 
-      f = popen (s, mode);
+      f = popen (s, mode[0] == 'r' ? "r" : "w");
 
       local_free (s);
 
@@ -307,7 +305,9 @@ fn_open (const char *fn, const char *mode)
 int
 fn_close (const char *fn, FILE *f)
 {
-  if (!strcmp (fn, "-"))
+  if (fileno (f) == STDIN_FILENO
+      || fileno (f) == STDOUT_FILENO
+      || fileno (f) == STDERR_FILENO)
     return 0;
 #if HAVE_POPEN
   else if (fn[0] == '|' || (*fn && fn[strlen (fn) - 1] == '|'))