b674c96997de9202acb6e909dcba75b4d7a01cc4
[platform/upstream/coreutils.git] / src / join.c
1 /* join - join lines of two files on a common field
2    Copyright (C) 1991, 1995 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18    Written by Mike Haertel, mike@gnu.ai.mit.edu.  */
19
20 #include <config.h>
21
22 #ifdef __GNUC__
23 #define alloca __builtin_alloca
24 #else /* not __GNUC__ */
25 #if HAVE_ALLOCA_H
26 #include <alloca.h>
27 #else /* not HAVE_ALLOCA_H */
28 #ifdef _AIX
29  #pragma alloca
30 #else /* not _AIX */
31 char *alloca ();
32 #endif /* not _AIX */
33 #endif /* not HAVE_ALLOCA_H */
34 #endif /* not __GNUC__ */
35
36 /* Get isblank from GNU libc.  */
37 #define _GNU_SOURCE
38
39 #include <stdio.h>
40 #define NDEBUG
41 #include <assert.h>
42 #include <sys/types.h>
43 #include <getopt.h>
44
45 #if HAVE_LIMITS_H
46 # include <limits.h>
47 #endif
48
49 #ifndef UINT_MAX
50 # define UINT_MAX ((unsigned int) ~(unsigned int) 0)
51 #endif
52
53 #ifndef INT_MAX
54 # define INT_MAX ((int) (UINT_MAX >> 1))
55 #endif
56
57 #include "system.h"
58 #include "version.h"
59 #include "long-options.h"
60 #include "xstrtol.h"
61 #include "error.h"
62
63 #define join system_join
64
65 char *xmalloc ();
66 char *xrealloc ();
67
68 /* Undefine, to avoid warning about redefinition on some systems.  */
69 #undef min
70 #undef max
71 #define min(A, B) ((A) < (B) ? (A) : (B))
72 #define max(A, B) ((A) > (B) ? (A) : (B))
73
74 /* An element of the list identifying which fields to print for each
75    output line.  */
76 struct outlist
77   {
78     /* File number: 0, 1, or 2.  0 means use the join field.
79        1 means use the first file argument, 2 the second.  */
80     int file;
81
82     /* Field index (zero-based), specified only when FILE is 1 or 2.  */
83     int field;
84
85     struct outlist *next;
86   };
87
88 /* A field of a line.  */
89 struct field
90   {
91     const char *beg;            /* First character in field.  */
92     size_t len;                 /* The length of the field.  */
93   };
94
95 /* A line read from an input file.  Newlines are not stored.  */
96 struct line
97   {
98     char *beg;                  /* First character in line.  */
99     char *lim;                  /* Character after last character in line.  */
100     int nfields;                /* Number of elements in `fields'.  */
101     int nfields_allocated;      /* Number of elements in `fields'.  */
102     struct field *fields;
103   };
104
105 /* One or more consecutive lines read from a file that all have the
106    same join field value.  */
107 struct seq
108   {
109     int count;                  /* Elements used in `lines'.  */
110     int alloc;                  /* Elements allocated in `lines'.  */
111     struct line *lines;
112   };
113
114 /* The name this program was run with.  */
115 char *program_name;
116
117 /* If nonzero, print unpairable lines in file 1 or 2.  */
118 static int print_unpairables_1, print_unpairables_2;
119
120 /* If nonzero, print pairable lines.  */
121 static int print_pairables;
122
123 /* Empty output field filler.  */
124 static char *empty_filler;
125
126 /* Field to join on.  */
127 static int join_field_1, join_field_2;
128
129 /* List of fields to print.  */
130 static struct outlist outlist_head;
131
132 /* Last element in `outlist', where a new element can be added.  */
133 static struct outlist *outlist_end = &outlist_head;
134
135 /* Tab character separating fields; if this is NUL fields are separated
136    by any nonempty string of white space, otherwise by exactly one
137    tab character.  */
138 static char tab;
139
140 /* When using getopt_long_only, no long option can start with
141    a character that is a short option.  */
142 static struct option const longopts[] =
143 {
144   {"j", required_argument, NULL, 'j'},
145   {"j1", required_argument, NULL, '1'},
146   {"j2", required_argument, NULL, '2'},
147   {NULL, 0, NULL, 0}
148 };
149
150 /* Used to print non-joining lines */
151 static struct line uni_blank;
152
153 static void
154 usage (int status)
155 {
156   if (status != 0)
157     fprintf (stderr, _("Try `%s --help' for more information.\n"),
158              program_name);
159   else
160     {
161       printf (_("\
162 Usage: %s [OPTION]... FILE1 FILE2\n\
163 "),
164               program_name);
165       printf (_("\
166 For each pair of input lines with identical join fields, write a line to\n\
167 standard output.  The default join field is the first, delimited\n\
168 by whitespace.  When FILE1 or FILE2 (not both) is -, read standard input.\n\
169 \n\
170   -a SIDE          print unpairable lines coming from file SIDE\n\
171   -e EMPTY         replace missing input fields with EMPTY\n\
172   -j FIELD         (Obsolescent) equivalent to `-1 FIELD -2 FIELD'\n\
173   -j1 FIELD        (Obsolescent) equivalent to `-1 FIELD'\n\
174   -j2 FIELD        (Obsolescent) equivalent to `-2 FIELD'\n\
175   -1 FIELD         join on this FIELD of file 1\n\
176   -2 FIELD         join on this FIELD of file 2\n\
177   -o FORMAT        obey FORMAT while constructing output line\n\
178   -t CHAR          use CHAR as input and output field separator\n\
179   -v SIDE          like -a SIDE, but suppress joined output lines\n\
180   --help           display this help and exit\n\
181   --version        output version information and exit\n\
182 \n\
183 Unless -t CHAR is given, leading blanks separate fields and are ignored,\n\
184 else fields are separated by CHAR.  Any FIELD is a field number counted\n\
185 from 1.  FORMAT is one or more comma or blank separated specifications,\n\
186 each being `SIDE.FIELD' or `0'.  Default FORMAT outputs the join field,\n\
187 the remaining fields from FILE1, the remaining fields from FILE2, all\n\
188 separated by CHAR.\n\
189 "));
190     }
191   exit (status);
192 }
193
194 static void
195 ADD_FIELD (struct line *line, const char *field, size_t len)
196 {
197   if (line->nfields >= line->nfields_allocated)
198     {
199       line->nfields_allocated = (3 * line->nfields_allocated) / 2 + 1;
200       line->fields = (struct field *) xrealloc ((char *) line->fields,
201                                                 (line->nfields_allocated
202                                                  * sizeof (struct field)));
203     }
204   line->fields[line->nfields].beg = field;
205   line->fields[line->nfields].len = len;
206   ++(line->nfields);
207 }
208
209 /* Fill in the `fields' structure in LINE.  */
210
211 static void
212 xfields (struct line *line)
213 {
214   int i;
215   register char *ptr, *lim;
216
217   ptr = line->beg;
218   lim = line->lim;
219
220   for (i = 0; ptr < lim; ++i)
221     {
222       if (tab)
223         {
224           char *beg;
225
226           beg = ptr;
227           while (ptr < lim && *ptr != tab)
228             ++ptr;
229           ADD_FIELD (line, beg, ptr - beg);
230           if (ptr < lim)
231             ++ptr;
232         }
233       else
234         {
235           char *beg;
236
237           beg = ptr;
238           while (ptr < lim && !ISSPACE (*ptr))
239             ++ptr;
240           ADD_FIELD (line, beg, ptr - beg);
241           while (ptr < lim && ISSPACE (*ptr))
242             ++ptr;
243         }
244     }
245
246   if (ptr > line->beg && ((tab && ISSPACE (ptr[-1])) || ptr[-1] == tab))
247     {
248       /* Add one more (empty) field because the last character of the
249          line was a delimiter.  */
250       ADD_FIELD (line, NULL, 0);
251     }
252 }
253
254 /* Read a line from FP into LINE and split it into fields.
255    Return 0 if EOF, 1 otherwise.  */
256
257 static int
258 get_line (FILE *fp, struct line *line)
259 {
260   static int linesize = 80;
261   int c, i;
262   char *ptr;
263
264   if (feof (fp))
265     return 0;
266
267   ptr = xmalloc (linesize);
268
269   for (i = 0; (c = getc (fp)) != EOF && c != '\n'; ++i)
270     {
271       if (i == linesize)
272         {
273           linesize *= 2;
274           ptr = xrealloc (ptr, linesize);
275         }
276       ptr[i] = c;
277     }
278
279   if (c == EOF && i == 0)
280     {
281       free (ptr);
282       return 0;
283     }
284
285   line->beg = ptr;
286   line->lim = line->beg + i;
287   line->nfields_allocated = 0;
288   line->nfields = 0;
289   line->fields = NULL;
290   xfields (line);
291   return 1;
292 }
293
294 static void
295 freeline (struct line *line)
296 {
297   free ((char *) line->fields);
298   free (line->beg);
299   line->beg = NULL;
300 }
301
302 static void
303 initseq (struct seq *seq)
304 {
305   seq->count = 0;
306   seq->alloc = 1;
307   seq->lines = (struct line *) xmalloc (seq->alloc * sizeof (struct line));
308 }
309
310 /* Read a line from FP and add it to SEQ.  Return 0 if EOF, 1 otherwise.  */
311
312 static int
313 getseq (FILE *fp, struct seq *seq)
314 {
315   if (seq->count == seq->alloc)
316     {
317       seq->alloc *= 2;
318       seq->lines = (struct line *)
319         xrealloc ((char *) seq->lines, seq->alloc * sizeof (struct line));
320     }
321
322   if (get_line (fp, &seq->lines[seq->count]))
323     {
324       ++seq->count;
325       return 1;
326     }
327   return 0;
328 }
329
330 static void
331 delseq (struct seq *seq)
332 {
333   int i;
334   for (i = 0; i < seq->count; i++)
335     if (seq->lines[i].beg)
336       freeline (&seq->lines[i]);
337   free ((char *) seq->lines);
338 }
339
340 /* Return <0 if the join field in LINE1 compares less than the one in LINE2;
341    >0 if it compares greater; 0 if it compares equal.  */
342
343 static int
344 keycmp (struct line *line1, struct line *line2)
345 {
346   const char *beg1, *beg2;      /* Start of field to compare in each file.  */
347   int len1, len2;               /* Length of fields to compare.  */
348   int diff;
349
350   if (join_field_1 < line1->nfields)
351     {
352       beg1 = line1->fields[join_field_1].beg;
353       len1 = line1->fields[join_field_1].len;
354     }
355   else
356     {
357       beg1 = NULL;
358       len1 = 0;
359     }
360
361   if (join_field_2 < line2->nfields)
362     {
363       beg2 = line2->fields[join_field_2].beg;
364       len2 = line2->fields[join_field_2].len;
365     }
366   else
367     {
368       beg2 = NULL;
369       len2 = 0;
370     }
371
372   if (len1 == 0)
373     return len2 == 0 ? 0 : -1;
374   if (len2 == 0)
375     return 1;
376   diff = memcmp (beg1, beg2, min (len1, len2));
377   if (diff)
378     return diff;
379   return len1 - len2;
380 }
381
382 /* Print field N of LINE if it exists and is nonempty, otherwise
383    `empty_filler' if it is nonempty.  */
384
385 static void
386 prfield (int n, struct line *line)
387 {
388   int len;
389
390   if (n < line->nfields)
391     {
392       len = line->fields[n].len;
393       if (len)
394         fwrite (line->fields[n].beg, 1, len, stdout);
395       else if (empty_filler)
396         fputs (empty_filler, stdout);
397     }
398   else if (empty_filler)
399     fputs (empty_filler, stdout);
400 }
401
402 /* Print the join of LINE1 and LINE2.  */
403
404 static void
405 prjoin (struct line *line1, struct line *line2)
406 {
407   const struct outlist *outlist;
408
409   outlist = outlist_head.next;
410   if (outlist)
411     {
412       const struct outlist *o;
413
414       o = outlist;
415       while (1)
416         {
417           int field;
418           struct line *line;
419
420           if (o->file == 0)
421             {
422               if (line1 == &uni_blank)
423                 {
424                   line = line2;
425                   field = join_field_2;
426                 }
427               else
428                 {
429                   line = line1;
430                   field = join_field_1;
431                 }
432             }
433           else
434             {
435               line = (o->file == 1 ? line1 : line2);
436               field = o->field;
437             }
438           prfield (field, line);
439           o = o->next;
440           if (o == NULL)
441             break;
442           putchar (tab ? tab : ' ');
443         }
444       putchar ('\n');
445     }
446   else
447     {
448       int i;
449
450       if (line1 == &uni_blank)
451         {
452           struct line *t;
453           t = line1;
454           line1 = line2;
455           line2 = t;
456         }
457       prfield (join_field_1, line1);
458       for (i = 0; i < join_field_1 && i < line1->nfields; ++i)
459         {
460           putchar (tab ? tab : ' ');
461           prfield (i, line1);
462         }
463       for (i = join_field_1 + 1; i < line1->nfields; ++i)
464         {
465           putchar (tab ? tab : ' ');
466           prfield (i, line1);
467         }
468
469       for (i = 0; i < join_field_2 && i < line2->nfields; ++i)
470         {
471           putchar (tab ? tab : ' ');
472           prfield (i, line2);
473         }
474       for (i = join_field_2 + 1; i < line2->nfields; ++i)
475         {
476           putchar (tab ? tab : ' ');
477           prfield (i, line2);
478         }
479       putchar ('\n');
480     }
481 }
482
483 /* Print the join of the files in FP1 and FP2.  */
484
485 static void
486 join (FILE *fp1, FILE *fp2)
487 {
488   struct seq seq1, seq2;
489   struct line line;
490   int diff, i, j, eof1, eof2;
491
492   /* Read the first line of each file.  */
493   initseq (&seq1);
494   getseq (fp1, &seq1);
495   initseq (&seq2);
496   getseq (fp2, &seq2);
497
498   while (seq1.count && seq2.count)
499     {
500       diff = keycmp (&seq1.lines[0], &seq2.lines[0]);
501       if (diff < 0)
502         {
503           if (print_unpairables_1)
504             prjoin (&seq1.lines[0], &uni_blank);
505           freeline (&seq1.lines[0]);
506           seq1.count = 0;
507           getseq (fp1, &seq1);
508           continue;
509         }
510       if (diff > 0)
511         {
512           if (print_unpairables_2)
513             prjoin (&uni_blank, &seq2.lines[0]);
514           freeline (&seq2.lines[0]);
515           seq2.count = 0;
516           getseq (fp2, &seq2);
517           continue;
518         }
519
520       /* Keep reading lines from file1 as long as they continue to
521          match the current line from file2.  */
522       eof1 = 0;
523       do
524         if (!getseq (fp1, &seq1))
525           {
526             eof1 = 1;
527             ++seq1.count;
528             break;
529           }
530       while (!keycmp (&seq1.lines[seq1.count - 1], &seq2.lines[0]));
531
532       /* Keep reading lines from file2 as long as they continue to
533          match the current line from file1.  */
534       eof2 = 0;
535       do
536         if (!getseq (fp2, &seq2))
537           {
538             eof2 = 1;
539             ++seq2.count;
540             break;
541           }
542       while (!keycmp (&seq1.lines[0], &seq2.lines[seq2.count - 1]));
543
544       if (print_pairables)
545         {
546           for (i = 0; i < seq1.count - 1; ++i)
547             for (j = 0; j < seq2.count - 1; ++j)
548               prjoin (&seq1.lines[i], &seq2.lines[j]);
549         }
550
551       for (i = 0; i < seq1.count - 1; ++i)
552         freeline (&seq1.lines[i]);
553       if (!eof1)
554         {
555           seq1.lines[0] = seq1.lines[seq1.count - 1];
556           seq1.count = 1;
557         }
558       else
559         seq1.count = 0;
560
561       for (i = 0; i < seq2.count - 1; ++i)
562         freeline (&seq2.lines[i]);
563       if (!eof2)
564         {
565           seq2.lines[0] = seq2.lines[seq2.count - 1];
566           seq2.count = 1;
567         }
568       else
569         seq2.count = 0;
570     }
571
572   if (print_unpairables_1 && seq1.count)
573     {
574       prjoin (&seq1.lines[0], &uni_blank);
575       freeline (&seq1.lines[0]);
576       while (get_line (fp1, &line))
577         {
578           prjoin (&line, &uni_blank);
579           freeline (&line);
580         }
581     }
582
583   if (print_unpairables_2 && seq2.count)
584     {
585       prjoin (&uni_blank, &seq2.lines[0]);
586       freeline (&seq2.lines[0]);
587       while (get_line (fp2, &line))
588         {
589           prjoin (&uni_blank, &line);
590           freeline (&line);
591         }
592     }
593
594   delseq (&seq1);
595   delseq (&seq2);
596 }
597
598 /* Add a field spec for field FIELD of file FILE to `outlist'.  */
599
600 static void
601 add_field (int file, int field)
602 {
603   struct outlist *o;
604
605   assert (file == 0 || file == 1 || file == 2);
606   assert (field >= 0);
607
608   o = (struct outlist *) xmalloc (sizeof (struct outlist));
609   o->file = file;
610   o->field = field;
611   o->next = NULL;
612
613   /* Add to the end of the list so the fields are in the right order.  */
614   outlist_end->next = o;
615   outlist_end = o;
616 }
617
618 /* Convert a single field specifier string, S, to a *FILE_INDEX, *FIELD_INDEX
619    pair.  In S, the field index string is 1-based; *FIELD_INDEX is zero-based.
620    If S is valid, return zero.  Otherwise, give a diagnostic, don't update
621    *FILE_INDEX or *FIELD_INDEX, and return non-zero.  */
622
623 static int
624 decode_field_spec (const char *s, int *file_index, int *field_index)
625 {
626   int valid = 0;
627
628   /* The first character must be 0, 1, or 2.  */
629   switch (s[0])
630     {
631     case '0':
632       if (s[1] == '\0')
633         {
634           *file_index = 0;
635           /* Leave *field_index undefined.  */
636           valid = 1;
637         }
638       else
639         {
640           /* `0' must be all alone -- no `.FIELD'.  */
641           error (0, 0, _("invalid field specifier: `%s'"), s);
642         }
643       break;
644
645     case '1':
646     case '2':
647       if (s[1] == '.' && s[2] != '\0')
648         {
649           strtol_error s_err;
650           long int tmp_long;
651
652           s_err = xstrtol (s + 2, NULL, 10, &tmp_long, NULL);
653           if (s_err != LONGINT_OK || tmp_long <= 0 || tmp_long > INT_MAX)
654             {
655               error (0, 0, _("invalid field number: `%s'"), s + 2);
656             }
657           else
658             {
659               *file_index = s[0] - '0';
660               /* Convert to a zero-based index.  */
661               *field_index = (int) tmp_long - 1;
662               valid = 1;
663             }
664         }
665       break;
666
667     default:
668       error (0, 0, _("invalid file number in field spec: `%s'"), s);
669       break;
670     }
671   return !valid;
672 }
673
674 /* Add the comma or blank separated field spec(s) in STR to `outlist'.
675    Return non-zero to indicate failure.  */
676
677 static int
678 add_field_list (const char *c_str)
679 {
680   char *p, *str;
681
682   /* Make a writable copy of c_str.  */
683   str = (char *) alloca (strlen (c_str) + 1);
684   strcpy (str, c_str);
685
686   p = str;
687   do
688     {
689       int invalid;
690       int file_index, field_index;
691       char *spec_item = p;
692
693       p = strpbrk (p, ", \t");
694       if (p)
695         *p++ = 0;
696       invalid = decode_field_spec (spec_item, &file_index, &field_index);
697       if (invalid)
698         return 1;
699       add_field (file_index, field_index);
700       uni_blank.nfields = max (uni_blank.nfields, field_index);
701     }
702   while (p);
703   return 0;
704 }
705
706 /* Create a blank line with COUNT fields separated by tabs.  */
707
708 void
709 make_blank (struct line *blank, int count)
710 {
711   int i;
712   blank->nfields = count;
713   blank->beg = xmalloc (blank->nfields + 1);
714   blank->fields = (struct field *) xmalloc (sizeof (struct field) * count);
715   for (i = 0; i < blank->nfields; i++)
716     {
717       blank->beg[i] = '\t';
718       blank->fields[i].beg = &blank->beg[i];
719       blank->fields[i].len = 0;
720     }
721   blank->beg[i] = '\0';
722   blank->lim = &blank->beg[i];
723 }
724
725 void
726 main (int argc, char **argv)
727 {
728   char *names[2];
729   FILE *fp1, *fp2;
730   int optc, prev_optc = 0, nfiles;
731
732   program_name = argv[0];
733
734   /* Initialize this before parsing options.  In parsing options,
735      it may be increased.  */
736   uni_blank.nfields = 1;
737
738   parse_long_options (argc, argv, "join", version_string, usage);
739
740   nfiles = 0;
741   print_pairables = 1;
742
743   while ((optc = getopt_long_only (argc, argv, "-a:e:1:2:o:t:v:", longopts,
744                                    (int *) 0)) != EOF)
745     {
746       long int val;
747
748       switch (optc)
749         {
750         case 0:
751           break;
752
753         case 'v':
754             print_pairables = 0;
755             /* Fall through.  */
756
757         case 'a':
758           if (xstrtol (optarg, NULL, 10, &val, NULL) != LONGINT_OK
759               || (val != 1 && val != 2))
760             error (2, 0, _("invalid field number: `%s'"), optarg);
761           if (val == 1)
762             print_unpairables_1 = 1;
763           else
764             print_unpairables_2 = 1;
765           break;
766
767         case 'e':
768           empty_filler = optarg;
769           break;
770
771         case '1':
772           if (xstrtol (optarg, NULL, 10, &val, NULL) != LONGINT_OK
773               || val <= 0 || val > INT_MAX)
774             {
775               error (2, 0, _("invalid field number for file 1: `%s'"), optarg);
776             }
777           join_field_1 = (int) val - 1;
778           break;
779
780         case '2':
781           if (xstrtol (optarg, NULL, 10, &val, NULL) != LONGINT_OK
782               || val <= 0 || val > INT_MAX)
783             error (2, 0, _("invalid field number for file 2: `%s'"), optarg);
784           join_field_2 = (int) val - 1;
785           break;
786
787         case 'j':
788           if (xstrtol (optarg, NULL, 10, &val, NULL) != LONGINT_OK
789               || val <= 0 || val > INT_MAX)
790             error (2, 0, _("invalid field number: `%s'"), optarg);
791           join_field_1 = join_field_2 = (int) val - 1;
792           break;
793
794         case 'o':
795           if (add_field_list (optarg))
796             exit (1);
797           break;
798
799         case 't':
800           tab = *optarg;
801           break;
802
803         case 1:         /* Non-option argument.  */
804           if (prev_optc == 'o' && optind <= argc - 2)
805             {
806               if (add_field_list (optarg))
807                 exit (1);
808
809               /* Might be continuation of args to -o.  */
810               continue;         /* Don't change `prev_optc'.  */
811             }
812
813           if (nfiles > 1)
814             {
815               error (0, 0, "too many non-option arguments");
816               usage (1);
817             }
818           names[nfiles++] = optarg;
819           break;
820
821         case '?':
822           usage (1);
823         }
824       prev_optc = optc;
825     }
826
827   /* Now that we've seen the options, we can construct the blank line
828      structure.  */
829   make_blank (&uni_blank, uni_blank.nfields);
830
831   if (nfiles != 2)
832     {
833       error (0, 0, "too few non-option arguments");
834       usage (1);
835     }
836
837   fp1 = strcmp (names[0], "-") ? fopen (names[0], "r") : stdin;
838   if (!fp1)
839     error (1, errno, "%s", names[0]);
840   fp2 = strcmp (names[1], "-") ? fopen (names[1], "r") : stdin;
841   if (!fp2)
842     error (1, errno, "%s", names[1]);
843   if (fp1 == fp2)
844     error (1, errno, _("both files cannot be standard input"));
845   join (fp1, fp2);
846
847   if (fp1 != stdin && fclose (fp1) == EOF)
848     error (1, errno, "%s", names[0]);
849   if (fp2 != stdin && fclose (fp2) == EOF)
850     error (1, errno, "%s", names[1]);
851   if ((fp1 == stdin || fp2 == stdin) && fclose (stdin) == EOF)
852     error (1, errno, "-");
853   if (ferror (stdout) || fclose (stdout) == EOF)
854     error (1, errno, _("write error"));
855
856   exit (0);
857 }