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