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