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