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