magic-elimination.patch from patch #6230.
[pspp-builds.git] / src / data / sys-file-writer.c
index 12b276a518cbecf4d19efd8f418ff35d14175e5a..b21053c216a54c5fa3181d161d6cd795ec5ed0d0 100644 (file)
 
 #include <ctype.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <time.h>
-#include <unistd.h>
 
 #include <libpspp/alloc.h>
 #include <libpspp/float-format.h>
 #include <libpspp/integer-format.h>
-#include <libpspp/magic.h>
 #include <libpspp/message.h>
 #include <libpspp/misc.h>
 #include <libpspp/str.h>
@@ -42,6 +39,7 @@
 #include <data/casewriter.h>
 #include <data/dictionary.h>
 #include <data/file-handle-def.h>
+#include <data/file-name.h>
 #include <data/format.h>
 #include <data/missing-values.h>
 #include <data/settings.h>
@@ -152,7 +150,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
 {
   struct sfm_writer *w = NULL;
   mode_t mode;
-  int fd;
+  FILE *file;
   int idx;
   int i;
 
@@ -164,22 +162,27 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
       opts.version = 3;
     }
 
-  /* Create file. */
+  /* Open file handle as an exclusive writer. */
+  if (!fh_open (fh, FH_REF_FILE, "system file", "we"))
+    return NULL;
+
+  /* Create the file on disk. */
   mode = S_IRUSR | S_IRGRP | S_IROTH;
   if (opts.create_writeable)
     mode |= S_IWUSR | S_IWGRP | S_IWOTH;
-  fd = open (fh_get_file_name (fh), O_WRONLY | O_CREAT | O_TRUNC, mode);
-  if (fd < 0)
-    goto open_error;
-
-  /* Open file handle. */
-  if (!fh_open (fh, FH_REF_FILE, "system file", "we"))
-    goto error;
+  file = create_stream (fh_get_file_name (fh), "w", mode);
+  if (file == NULL)
+    {
+      msg (ME, _("Error opening \"%s\" for writing as a system file: %s."),
+           fh_get_file_name (fh), strerror (errno));
+      fh_close (fh, "system file", "we");
+      return NULL;
+    }
 
   /* Create and initialize writer. */
   w = xmalloc (sizeof *w);
   w->fh = fh;
-  w->file = fdopen (fd, "w");
+  w->file = file;
 
   w->compress = opts.compress;
   w->case_cnt = 0;
@@ -193,13 +196,6 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
   w->segment_cnt = sfm_dictionary_to_sfm_vars (d, &w->sfm_vars,
                                                &w->sfm_var_cnt);
 
-  /* Check that file create succeeded. */
-  if (w->file == NULL)
-    {
-      close (fd);
-      goto open_error;
-    }
-
   /* Write the file header. */
   write_header (w, d);
 
@@ -236,18 +232,13 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
   write_int (w, 0);
 
   if (write_error (w))
-    goto error;
-
-  return casewriter_create (&sys_file_casewriter_class, w);
-
- error:
-  close_writer (w);
-  return NULL;
+    {
+      close_writer (w);
+      return NULL;
+    }
 
- open_error:
-  msg (ME, _("Error opening \"%s\" for writing as a system file: %s."),
-       fh_get_file_name (fh), strerror (errno));
-  goto error;
+  return casewriter_create (dict_get_next_value_idx (d),
+                            &sys_file_casewriter_class, w);
 }
 
 /* Returns value of X truncated to two least-significant digits. */
@@ -547,8 +538,7 @@ write_variable_display_parameters (struct sfm_writer *w,
           int width_left = width - sfm_segment_effective_offset (width, i);
           write_int (w, measure);
           write_int (w, (i == 0 ? var_get_display_width (v)
-                         : sfm_width_to_segments (width_left) > 1 ? 32
-                         : width_left));
+                         : var_default_display_width (width_left)));
           write_int (w, alignment);
         }
     }