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