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