0e99013f689126a2ccb6e0da4f7930ee4a599c78
[pspp-builds.git] / src / sfm-write.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 /* AIX requires this to be the first thing in the file.  */
21 #include <config.h>
22 #if __GNUC__
23 #define alloca __builtin_alloca
24 #else
25 #if HAVE_ALLOCA_H
26 #include <alloca.h>
27 #else
28 #ifdef _AIX
29 #pragma alloca
30 #else
31 #ifndef alloca                  /* predefined by HP cc +Olibcalls */
32 char *alloca ();
33 #endif
34 #endif
35 #endif
36 #endif
37
38 #include <assert.h>
39 #include <stdlib.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <time.h>
43 #if HAVE_UNISTD_H
44 #include <unistd.h>     /* Required by SunOS4. */
45 #endif
46 #include "alloc.h"
47 #include "approx.h"
48 #include "avl.h"
49 #include "error.h"
50 #include "file-handle.h"
51 #include "getline.h"
52 #include "magic.h"
53 #include "misc.h"
54 #include "sfm.h"
55 #include "sfmP.h"
56 #include "str.h"
57 #include "var.h"
58 #include "version.h"
59
60 #undef DEBUGGING
61 /*#define DEBUGGING 1 */
62 #include "debug-print.h"
63
64 /* PORTME: This file may require substantial revision for those
65    systems that don't meet the typical 32-bit integer/64-bit double
66    model.  It's kinda hard to tell without having one of them on my
67    desk.  */
68
69 /* Compression bias used by PSPP.  Values between (1 -
70    COMPRESSION_BIAS) and (251 - COMPRESSION_BIAS) inclusive can be
71    compressed. */
72 #define COMPRESSION_BIAS 100
73
74 /* sfm writer file_handle extension. */
75 struct sfm_fhuser_ext
76   {
77     FILE *file;                 /* Actual file. */
78
79     int compressed;             /* 1=compressed, 0=not compressed. */
80     flt64 *buf;                 /* Buffered data. */
81     flt64 *end;                 /* Buffer end. */
82     flt64 *ptr;                 /* Current location in buffer. */
83     unsigned char *x;           /* Location in current instruction octet. */
84     unsigned char *y;           /* End of instruction octet. */
85     int n_cases;                /* Number of cases written so far. */
86
87     char *elem_type;            /* ALPHA or NUMERIC for each flt64 element. */
88   };
89
90 static struct fh_ext_class sfm_w_class;
91
92 static char *append_string_max (char *, const char *, const char *);
93 static int write_header (struct sfm_write_info *inf);
94 static int bufwrite (struct file_handle *h, const void *buf, size_t nbytes);
95 static int write_variable (struct sfm_write_info *inf, struct variable *v);
96 static int write_value_labels (struct sfm_write_info *inf, struct variable * s, int index);
97 static int write_rec_7_34 (struct sfm_write_info *inf);
98 static int write_documents (struct sfm_write_info *inf);
99
100 /* Writes the dictionary INF->dict to system file INF->h.  The system
101    file is compressed if INF->compress is nonzero.  INF->case_size is
102    set to the number of flt64 elements in a single case.  Returns
103    nonzero only if successful. */
104 int
105 sfm_write_dictionary (struct sfm_write_info *inf)
106 {
107   struct dictionary *d = inf->dict;
108   struct sfm_fhuser_ext *ext;
109   int i;
110   int index;
111
112   if (inf->h->class != NULL)
113     {
114       msg (ME, _("Cannot write file %s as system file: already opened for %s."),
115            fh_handle_name (inf->h), inf->h->class->name);
116       return 0;
117     }
118
119   msg (VM (1), _("%s: Opening system-file handle %s for writing."),
120        fh_handle_filename (inf->h), fh_handle_name (inf->h));
121   
122   /* Open the physical disk file. */
123   inf->h->class = &sfm_w_class;
124   inf->h->ext = ext = xmalloc (sizeof (struct sfm_fhuser_ext));
125   ext->file = fopen (inf->h->norm_fn, "wb");
126   ext->elem_type = NULL;
127   if (ext->file == NULL)
128     {
129       msg (ME, _("An error occurred while opening \"%s\" for writing "
130            "as a system file: %s."), inf->h->fn, strerror (errno));
131       err_cond_fail ();
132       free (ext);
133       return 0;
134     }
135
136   /* Initialize the sfm_fhuser_ext structure. */
137   ext->compressed = inf->compress;
138   ext->buf = ext->ptr = NULL;
139   ext->x = ext->y = NULL;
140   ext->n_cases = 0;
141
142   /* Write the file header. */
143   if (!write_header (inf))
144     goto lossage;
145
146   /* Write basic variable info. */
147   for (i = 0; i < d->nvar; i++)
148     write_variable (inf, d->var[i]);
149
150   /* Write out value labels. */
151   for (index = i = 0; i < d->nvar; i++)
152     {
153       struct variable *v = d->var[i];
154
155       if (!write_value_labels (inf, v, index))
156         goto lossage;
157       index += (v->type == NUMERIC ? 1
158                 : DIV_RND_UP (v->width, sizeof (flt64)));
159     }
160
161   if (d->documents != NULL && !write_documents (inf))
162     goto lossage;
163   if (!write_rec_7_34 (inf))
164     goto lossage;
165
166   /* Write record 999. */
167   {
168     struct
169       {
170         int32 rec_type P;
171         int32 filler P;
172       }
173     rec_999;
174
175     rec_999.rec_type = 999;
176     rec_999.filler = 0;
177
178     if (!bufwrite (inf->h, &rec_999, sizeof rec_999))
179       goto lossage;
180   }
181
182   msg (VM (2), _("Wrote system-file header successfully."));
183   
184   return 1;
185
186 lossage:
187   msg (VM (1), _("Error writing system-file header."));
188   fclose (ext->file);
189   inf->h->class = NULL;
190   inf->h->ext = NULL;
191   free (ext->elem_type);
192   ext->elem_type = NULL;
193   return 0;
194 }
195
196 /* Returns value of X truncated to two least-significant digits. */
197 static int
198 rerange (int x)
199 {
200   if (x < 0)
201     x = -x;
202   if (x >= 100)
203     x %= 100;
204   return x;
205 }
206
207 /* Write the sysfile_header header to the system file represented by
208    INF. */
209 static int
210 write_header (struct sfm_write_info *inf)
211 {
212   struct dictionary *d = inf->dict;
213   struct sfm_fhuser_ext *ext = inf->h->ext;
214   struct sysfile_header hdr;
215   char *p;
216   int i;
217
218   time_t t;
219
220   memcpy (hdr.rec_type, "$FL2", 4);
221
222   p = stpcpy (hdr.prod_name, "@(#) SPSS DATA FILE ");
223   p = append_string_max (p, version, &hdr.prod_name[60]);
224   p = append_string_max (p, " - ", &hdr.prod_name[60]);
225   p = append_string_max (p, host_system, &hdr.prod_name[60]);
226   memset (p, ' ', &hdr.prod_name[60] - p);
227
228   hdr.layout_code = 2;
229
230   hdr.case_size = 0;
231   for (i = 0; i < d->nvar; i++)
232     {
233       struct variable *v = d->var[i];
234       hdr.case_size += (v->type == NUMERIC ? 1
235                         : DIV_RND_UP (v->width, sizeof (flt64)));
236     }
237   inf->case_size = hdr.case_size;
238
239   p = ext->elem_type = xmalloc (inf->case_size);
240   for (i = 0; i < d->nvar; i++)
241     {
242       struct variable *v = d->var[i];
243       int count = (v->type == NUMERIC ? 1
244                    : DIV_RND_UP (v->width, sizeof (flt64)));
245       while (count--)
246         *p++ = v->type;
247     }
248
249   hdr.compressed = inf->compress;
250
251   update_weighting (d);
252   if (d->weight_index != -1)
253     {
254       int recalc_weight_index = 1;
255
256       for (i = 0; i < d->weight_index; i++)
257         {
258           struct variable *v = d->var[i];
259           recalc_weight_index += (v->type == NUMERIC ? 1
260                                   : DIV_RND_UP (v->width, sizeof (flt64)));
261         }
262       hdr.weight_index = recalc_weight_index;
263     }
264   else
265     hdr.weight_index = 0;
266
267   hdr.ncases = -1;
268   hdr.bias = COMPRESSION_BIAS;
269
270   if ((time_t) - 1 == time (&t))
271     {
272       memcpy (hdr.creation_date, "01 Jan 70", 9);
273       memcpy (hdr.creation_time, "00:00:00", 8);
274     }
275   else
276     {
277       static const char *month_name[12] =
278       {
279         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
280         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
281       };
282       struct tm *tmp = localtime (&t);
283       int day = rerange (tmp->tm_mday);
284       int mon = rerange (tmp->tm_mon + 1);
285       int year = rerange (tmp->tm_year);
286       int hour = rerange (tmp->tm_hour + 1);
287       int min = rerange (tmp->tm_min + 1);
288       int sec = rerange (tmp->tm_sec + 1);
289       char buf[10];
290
291       sprintf (buf, "%02d %s %02d", day, month_name[mon - 1], year);
292       memcpy (hdr.creation_date, buf, sizeof hdr.creation_date);
293       sprintf (buf, "%02d:%02d:%02d", hour - 1, min - 1, sec - 1);
294       memcpy (hdr.creation_time, buf, sizeof hdr.creation_time);
295     }
296
297   st_bare_pad_copy (hdr.file_label, d->label ? d->label : "",
298                     sizeof hdr.file_label);
299   memset (hdr.padding, 0, sizeof hdr.padding);
300
301   if (!bufwrite (inf->h, &hdr, sizeof hdr))
302     return 0;
303   return 1;
304 }
305
306 /* Translates format spec from internal form in SRC to system file
307    format in DEST. */
308 static inline void
309 write_format_spec (struct fmt_spec *src, int32 *dest)
310 {
311   *dest = (formats[src->type].spss << 16) | (src->w << 8) | src->d;
312 }
313
314 /* Write the variable record(s) for primary variable P and secondary
315    variable S to the system file represented by INF. */
316 static int
317 write_variable (struct sfm_write_info *inf, struct variable *v)
318 {
319   struct sysfile_variable sv;
320
321   /* Missing values. */
322   flt64 m[3];                   /* Missing value values. */
323   int nm;                       /* Number of missing values, possibly negative. */
324
325   sv.rec_type = 2;
326   sv.type = (v->type == NUMERIC ? 0 : v->width);
327   sv.has_var_label = (v->label != NULL);
328
329   switch (v->miss_type)
330     {
331     case MISSING_NONE:
332       nm = 0;
333       break;
334     case MISSING_1:
335     case MISSING_2:
336     case MISSING_3:
337       for (nm = 0; nm < v->miss_type; nm++)
338         m[nm] = v->missing[nm].f;
339       break;
340     case MISSING_RANGE:
341       m[0] = v->missing[0].f;
342       m[1] = v->missing[1].f;
343       nm = -2;
344       break;
345     case MISSING_LOW:
346       m[0] = second_lowest_flt64;
347       m[1] = v->missing[0].f;
348       nm = -2;
349       break;
350     case MISSING_HIGH:
351       m[0] = v->missing[0].f;
352       m[1] = FLT64_MAX;
353       nm = -2;
354       break;
355     case MISSING_RANGE_1:
356       m[0] = v->missing[0].f;
357       m[1] = v->missing[1].f;
358       m[2] = v->missing[2].f;
359       nm = -3;
360       break;
361     case MISSING_LOW_1:
362       m[0] = second_lowest_flt64;
363       m[1] = v->missing[0].f;
364       m[2] = v->missing[1].f;
365       nm = -3;
366       break;
367     case MISSING_HIGH_1:
368       m[0] = v->missing[0].f;
369       m[1] = second_lowest_flt64;
370       m[2] = v->missing[1].f;
371       nm = -3;
372       break;
373     default:
374       assert (0);
375     }
376
377   sv.n_missing_values = nm;
378   write_format_spec (&v->print, &sv.print);
379   write_format_spec (&v->write, &sv.write);
380   memcpy (sv.name, v->name, strlen (v->name));
381   memset (&sv.name[strlen (v->name)], ' ', 8 - strlen (v->name));
382   if (!bufwrite (inf->h, &sv, sizeof sv))
383     return 0;
384
385   if (v->label)
386     {
387       struct label
388         {
389           int32 label_len P;
390           char label[120] P;
391         }
392       l;
393
394       int ext_len;
395
396       l.label_len = min (strlen (v->label), 120);
397       ext_len = ROUND_UP (l.label_len, sizeof l.label_len);
398       memcpy (l.label, v->label, l.label_len);
399       memset (&l.label[l.label_len], ' ', ext_len - l.label_len);
400
401       if (!bufwrite (inf->h, &l, offsetof (struct label, label) + ext_len))
402           return 0;
403     }
404
405   if (nm && !bufwrite (inf->h, m, sizeof *m * nm))
406     return 0;
407
408   if (v->type == ALPHA && v->width > (int) sizeof (flt64))
409     {
410       int i;
411       int pad_count;
412
413       sv.type = -1;
414       sv.has_var_label = 0;
415       sv.n_missing_values = 0;
416       memset (&sv.print, 0, sizeof sv.print);
417       memset (&sv.write, 0, sizeof sv.write);
418       memset (&sv.name, 0, sizeof sv.name);
419
420       pad_count = DIV_RND_UP (v->width, (int) sizeof (flt64)) - 1;
421       for (i = 0; i < pad_count; i++)
422         if (!bufwrite (inf->h, &sv, sizeof sv))
423           return 0;
424     }
425
426   return 1;
427 }
428
429 /* Writes the value labels for variable V having system file variable
430    index INDEX to the system file associated with INF.  Returns
431    nonzero only if successful. */
432 static int
433 write_value_labels (struct sfm_write_info * inf, struct variable *v, int index)
434 {
435   struct value_label_rec
436     {
437       int32 rec_type P;
438       int32 n_labels P;
439       flt64 labels[1] P;
440     };
441
442   struct variable_index_rec
443     {
444       int32 rec_type P;
445       int32 n_vars P;
446       int32 vars[1] P;
447     };
448
449   avl_traverser i;
450   struct value_label_rec *vlr;
451   struct variable_index_rec vir;
452   struct value_label *vl;
453   size_t vlr_size;
454   flt64 *loc;
455   avl_traverser_init (i);
456
457   if (v->val_lab == NULL || avl_count (v->val_lab) == 0)
458     return 1;
459
460   /* Pass 1: Count bytes. */
461   vlr_size = (sizeof (struct value_label_rec)
462               + sizeof (flt64) * (avl_count (v->val_lab) - 1));
463   while (NULL != (vl = avl_traverse (v->val_lab, &i)))
464     vlr_size += ROUND_UP (strlen (vl->s) + 1, sizeof (flt64));
465
466   /* Pass 2: Copy bytes. */
467   vlr = local_alloc (vlr_size);
468   vlr->rec_type = 3;
469   vlr->n_labels = avl_count (v->val_lab);
470   loc = vlr->labels;
471   while (NULL != (vl = avl_traverse (v->val_lab, &i)))
472     {
473       int len = strlen (vl->s);
474
475       *loc++ = vl->v.f;
476       *(unsigned char *) loc = len;
477       memcpy (&((unsigned char *) loc)[1], vl->s, len);
478       memset (&((unsigned char *) loc)[1 + len], ' ',
479               REM_RND_UP (len + 1, sizeof (flt64)));
480       loc += DIV_RND_UP (len + 1, sizeof (flt64));
481     }
482   
483   if (!bufwrite (inf->h, vlr, vlr_size))
484     {
485       local_free (vlr);
486       return 0;
487     }
488   local_free (vlr);
489
490   vir.rec_type = 4;
491   vir.n_vars = 1;
492   vir.vars[0] = index + 1;
493   if (!bufwrite (inf->h, &vir, sizeof vir))
494     return 0;
495
496   return 1;
497 }
498
499 /* Writes record type 6, document record. */
500 static int
501 write_documents (struct sfm_write_info * inf)
502 {
503   struct dictionary *d = inf->dict;
504   struct
505   {
506     int32 rec_type P;           /* Always 6. */
507     int32 n_lines P;            /* Number of lines of documents. */
508   }
509   rec_6;
510
511   rec_6.rec_type = 6;
512   rec_6.n_lines = d->n_documents;
513   if (!bufwrite (inf->h, &rec_6, sizeof rec_6))
514     return 0;
515   if (!bufwrite (inf->h, d->documents, 80 * d->n_documents))
516     return 0;
517
518   return 1;
519 }
520
521 /* Writes record type 7, subtypes 3 and 4. */
522 static int
523 write_rec_7_34 (struct sfm_write_info * inf)
524 {
525   struct
526     {
527       int32 rec_type_3 P;
528       int32 subtype_3 P;
529       int32 data_type_3 P;
530       int32 n_elem_3 P;
531       int32 elem_3[8] P;
532       int32 rec_type_4 P;
533       int32 subtype_4 P;
534       int32 data_type_4 P;
535       int32 n_elem_4 P;
536       flt64 elem_4[3] P;
537     }
538   rec_7;
539
540   /* Components of the version number, from major to minor. */
541   int version_component[3];
542   
543   /* Used to step through the version string. */
544   char *p;
545
546   /* Parses the version string, which is assumed to be of the form
547      #.#x, where each # is a string of digits, and x is a single
548      letter. */
549   version_component[0] = strtol (bare_version, &p, 10);
550   if (*p == '.')
551     p++;
552   version_component[1] = strtol (bare_version, &p, 10);
553   version_component[2] = (isalpha ((unsigned char) *p)
554                           ? tolower ((unsigned char) *p) - 'a' : 0);
555     
556   rec_7.rec_type_3 = 7;
557   rec_7.subtype_3 = 3;
558   rec_7.data_type_3 = sizeof (int32);
559   rec_7.n_elem_3 = 8;
560   rec_7.elem_3[0] = version_component[0];
561   rec_7.elem_3[1] = version_component[1];
562   rec_7.elem_3[2] = version_component[2];
563   rec_7.elem_3[3] = -1;
564
565   /* PORTME: 1=IEEE754, 2=IBM 370, 3=DEC VAX E. */
566 #if FPREP==FPREP_IEEE754
567   rec_7.elem_3[4] = 1;
568 #else
569 #error Floating-point representation unknown here.
570 #endif
571
572   rec_7.elem_3[5] = 1;
573
574   /* PORTME: 1=big-endian, 2=little-endian. */
575   if (endian == BIG)
576     rec_7.elem_3[6] = 1;
577   else if (endian == LITTLE)
578     rec_7.elem_3[6] = 2;
579   else
580     rec_7.elem_3[6] = 0;
581
582   /* PORTME: 1=EBCDIC, 2=7-bit ASCII, 3=8-bit ASCII, 4=DEC Kanji. */
583   rec_7.elem_3[7] = 2;
584
585   rec_7.rec_type_4 = 7;
586   rec_7.subtype_4 = 4;
587   rec_7.data_type_4 = sizeof (flt64);
588   rec_7.n_elem_4 = 3;
589   rec_7.elem_4[0] = -FLT64_MAX;
590   rec_7.elem_4[1] = FLT64_MAX;
591   rec_7.elem_4[2] = second_lowest_flt64;
592
593   if (!bufwrite (inf->h, &rec_7, sizeof rec_7))
594     return 0;
595   return 1;
596 }
597
598 /* Write NBYTES starting at BUF to the system file represented by
599    H. */
600 static int
601 bufwrite (struct file_handle * h, const void *buf, size_t nbytes)
602 {
603   struct sfm_fhuser_ext *ext = h->ext;
604
605   assert (buf);
606   if (1 != fwrite (buf, nbytes, 1, ext->file))
607     {
608       msg (ME, _("%s: Writing system file: %s."), h->fn, strerror (errno));
609       return 0;
610     }
611   return 1;
612 }
613
614 /* Copies string DEST to SRC with the proviso that DEST does not reach
615    byte END; no null terminator is copied.  Returns a pointer to the
616    byte after the last byte copied. */
617 static char *
618 append_string_max (char *dest, const char *src, const char *end)
619 {
620   int nbytes = min (end - dest, (int) strlen (src));
621   memcpy (dest, src, nbytes);
622   return dest + nbytes;
623 }
624
625 /* Makes certain that the compression buffer of H has room for another
626    element.  If there's not room, pads out the current instruction
627    octet with zero and dumps out the buffer. */
628 static inline int
629 ensure_buf_space (struct file_handle *h)
630 {
631   struct sfm_fhuser_ext *ext = h->ext;
632
633   if (ext->ptr >= ext->end)
634     {
635       memset (ext->x, 0, ext->y - ext->x);
636       ext->x = ext->y;
637       ext->ptr = ext->buf;
638       if (!bufwrite (h, ext->buf, sizeof *ext->buf * 128))
639         return 0;
640     }
641   return 1;
642 }
643
644 /* Writes case ELEM consisting of N_ELEM flt64 elements to the system
645    file represented by H.  Return success. */
646 int
647 sfm_write_case (struct file_handle * h, const flt64 *elem, int n_elem)
648 {
649   struct sfm_fhuser_ext *ext = h->ext;
650   const flt64 *end_elem = &elem[n_elem];
651   char *elem_type = ext->elem_type;
652
653   ext->n_cases++;
654
655   if (ext->compressed == 0)
656     return bufwrite (h, elem, sizeof *elem * n_elem);
657
658   if (ext->buf == NULL)
659     {
660       ext->buf = xmalloc (sizeof *ext->buf * 128);
661       ext->ptr = ext->buf;
662       ext->end = &ext->buf[128];
663       ext->x = (unsigned char *) (ext->ptr++);
664       ext->y = (unsigned char *) (ext->ptr);
665     }
666   for (; elem < end_elem; elem++, elem_type++)
667     {
668       if (ext->x >= ext->y)
669         {
670           if (!ensure_buf_space (h))
671             return 0;
672           ext->x = (unsigned char *) (ext->ptr++);
673           ext->y = (unsigned char *) (ext->ptr);
674         }
675
676       if (*elem_type == NUMERIC)
677         {
678           if (*elem == -FLT64_MAX)
679             {
680               *ext->x++ = 255;
681               continue;
682             }
683           else
684             {
685               int value = *elem < 0 ? *elem - EPSILON : *elem + EPSILON;
686
687               if (value >= 1 - COMPRESSION_BIAS
688                   && value <= 251 - COMPRESSION_BIAS
689                   && approx_eq (value, *elem))
690                 {
691                   *ext->x++ = value + COMPRESSION_BIAS;
692                   continue;
693                 }
694             }
695         }
696       else
697         {
698           if (0 == memcmp ((char *) elem,
699                            "                                           ",
700                            sizeof (flt64)))
701             {
702               *ext->x++ = 254;
703               continue;
704             }
705         }
706       
707       *ext->x++ = 253;
708       if (!ensure_buf_space (h))
709         return 0;
710       *ext->ptr++ = *elem;
711     }
712
713   return 1;
714 }
715
716 /* Closes a system file after we're done with it. */
717 static void
718 sfm_close (struct file_handle * h)
719 {
720   struct sfm_fhuser_ext *ext = h->ext;
721
722   if (ext->buf != NULL && ext->ptr > ext->buf)
723     {
724       memset (ext->x, 0, ext->y - ext->x);
725       bufwrite (h, ext->buf, (ext->ptr - ext->buf) * sizeof *ext->buf);
726     }
727
728   /* Attempt to seek back to the beginning in order to write the
729      number of cases.  If that's not possible (i.e., we're writing to
730      a tty or a pipe), then it's not a big deal because we wrote the
731      code that indicates an unknown number of cases. */
732   if (0 == fseek (ext->file, offsetof (struct sysfile_header, ncases),
733                   SEEK_SET))
734     {
735       int32 n_cases = ext->n_cases;
736
737       /* I don't really care about the return value: it doesn't matter
738          whether this data is written.  This is the only situation in
739          which you will see me fail to check a return value. */
740       fwrite (&n_cases, sizeof n_cases, 1, ext->file);
741     }
742
743   if (EOF == fclose (ext->file))
744     msg (ME, _("%s: Closing system file: %s."), h->fn, strerror (errno));
745   free (ext->buf);
746
747   free (ext->elem_type);
748   free (ext);
749 }
750
751 static struct fh_ext_class sfm_w_class =
752 {
753   4,
754   N_("writing as a system file"),
755   sfm_close,
756 };