Allow output files to overwrite input files (bug #21280). Thanks to
[pspp-builds.git] / src / language / data-io / file-handle.q
index 2e3d8a346c84848b584e16d71cb557e7b1e06a0f..d53017735259eeec6b6cb2635fa4dd5734e9f17e 100644 (file)
@@ -19,7 +19,6 @@
 #include <libpspp/message.h>
 #include <errno.h>
 #include <stdlib.h>
-#include <libpspp/alloc.h>
 #include <data/file-name.h>
 #include <language/command.h>
 #include <language/lexer/lexer.h>
@@ -29,6 +28,8 @@
 #include <data/variable.h>
 #include <data/file-handle-def.h>
 
+#include "xalloc.h"
+
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
@@ -95,12 +96,12 @@ cmd_file_handle (struct lexer *lexer, struct dataset *ds)
       if (cmd.n_lrecl[0] == LONG_MIN)
         msg (SE, _("Fixed-length records were specified on /RECFORM, but "
                    "record length was not specified on /LRECL.  "
-                   "Assuming %u-character records."),
-             (unsigned int) properties.record_width);
+                   "Assuming %zu-character records."),
+             properties.record_width);
       else if (cmd.n_lrecl[0] < 1)
         msg (SE, _("Record length (%ld) must be at least one byte.  "
-                   "Assuming %u-character records."),
-             cmd.n_lrecl[0], (unsigned int) properties.record_width);
+                   "Assuming %zu-character records."),
+             cmd.n_lrecl[0], properties.record_width);
       else
         properties.record_width = cmd.n_lrecl[0];
       break;
@@ -132,8 +133,7 @@ cmd_close_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
   if (handle == NULL)
     return CMD_CASCADING_FAILURE;
 
-  fh_free (handle);
-
+  fh_unname (handle);
   return CMD_SUCCESS;
 }
 
@@ -157,7 +157,10 @@ referent_name (enum fh_referent referent)
 /* Parses a file handle name, which may be a file name as a string
    or a file handle name as an identifier.  The allowed types of
    file handle are restricted to those in REFERENT_MASK.  Returns
-   the file handle when successful, a null pointer on failure. */
+   the file handle when successful, a null pointer on failure.
+
+   The caller is responsible for fh_unref()'ing the returned
+   file handle when it is no longer needed. */
 struct file_handle *
 fh_parse (struct lexer *lexer, enum fh_referent referent_mask)
 {
@@ -176,8 +179,6 @@ fh_parse (struct lexer *lexer, enum fh_referent referent_mask)
       handle = NULL;
       if (lex_token (lexer) == T_ID)
         handle = fh_from_id (lex_tokid (lexer));
-      if (handle == NULL)
-        handle = fh_from_file_name (ds_cstr (lex_tokstr (lexer)));
       if (handle == NULL)
         {
           if (lex_token (lexer) != T_ID || lex_tokid (lexer)[0] != '#' || get_syntax () != ENHANCED)
@@ -193,6 +194,7 @@ fh_parse (struct lexer *lexer, enum fh_referent referent_mask)
     {
       msg (SE, _("Handle for %s not allowed here."),
            referent_name (fh_get_referent (handle)));
+      fh_unref (handle);
       return NULL;
     }