Factor out some common strings to make translation easier.
[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 "), stdout);
171       fputs (_("\
172       --help        display this help and exit\n\
173       --version     output version information and exit\n\
174 "), stdout);
175       fputs (_("\
176 \n\
177 "), stdout);
178       fputs (_("\
179 Unless -t CHAR is given, leading blanks separate fields and are ignored,\n\
180 else fields are separated by CHAR.  Any FIELD is a field number counted\n\
181 from 1.  FORMAT is one or more comma or blank separated specifications,\n\
182 each being `SIDE.FIELD' or `0'.  Default FORMAT outputs the join field,\n\
183 the remaining fields from FILE1, the remaining fields from FILE2, all\n\
184 separated by CHAR.\n\
185 "), stdout);
186       puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
187     }
188   exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
189 }
190
191 static void
192 ADD_FIELD (struct line *line, const unsigned char *field, size_t len)
193 {
194   if (line->nfields >= line->nfields_allocated)
195     {
196       line->nfields_allocated = (3 * line->nfields_allocated) / 2 + 1;
197       line->fields = (struct field *) xrealloc ((char *) line->fields,
198                                                 (line->nfields_allocated
199                                                  * sizeof (struct field)));
200     }
201   line->fields[line->nfields].beg = field;
202   line->fields[line->nfields].len = len;
203   ++(line->nfields);
204 }
205
206 /* Fill in the `fields' structure in LINE.  */
207
208 static void
209 xfields (struct line *line)
210 {
211   int i;
212   unsigned char *ptr0 = (unsigned char *) line->buf.buffer;
213   unsigned char *ptr;
214   unsigned char *lim;
215
216   ptr = ptr0;
217   lim = ptr0 + line->buf.length - 1;
218
219   if (!tab)
220     {
221       /* Skip leading blanks before the first field.  */
222       while (ptr < lim && ISBLANK (*ptr))
223         ++ptr;
224     }
225
226   for (i = 0; ptr < lim; ++i)
227     {
228       if (tab)
229         {
230           unsigned char *beg;
231
232           beg = ptr;
233           while (ptr < lim && *ptr != tab)
234             ++ptr;
235           ADD_FIELD (line, beg, ptr - beg);
236           if (ptr < lim)
237             ++ptr;
238         }
239       else
240         {
241           unsigned char *beg;
242
243           beg = ptr;
244           while (ptr < lim && !ISBLANK (*ptr))
245             ++ptr;
246           ADD_FIELD (line, beg, ptr - beg);
247           while (ptr < lim && ISBLANK (*ptr))
248             ++ptr;
249         }
250     }
251
252   if (ptr != ptr0 && ((!tab && ISBLANK (ptr[-1])) || ptr[-1] == tab))
253     {
254       /* Add one more (empty) field because the last character of the
255          line was a delimiter.  */
256       ADD_FIELD (line, NULL, 0);
257     }
258 }
259
260 /* Read a line from FP into LINE and split it into fields.
261    Return 0 if EOF, 1 otherwise.  */
262
263 static int
264 get_line (FILE *fp, struct line *line)
265 {
266   initbuffer (&line->buf);
267
268   if (! readline (&line->buf, fp))
269     {
270       free (line->buf.buffer);
271       line->buf.buffer = NULL;
272       return 0;
273     }
274
275   line->nfields_allocated = 0;
276   line->nfields = 0;
277   line->fields = NULL;
278   xfields (line);
279   return 1;
280 }
281
282 static void
283 freeline (struct line *line)
284 {
285   free ((char *) line->fields);
286   free (line->buf.buffer);
287   line->buf.buffer = NULL;
288 }
289
290 static void
291 initseq (struct seq *seq)
292 {
293   seq->count = 0;
294   seq->alloc = 1;
295   seq->lines = (struct line *) xmalloc (seq->alloc * sizeof (struct line));
296 }
297
298 /* Read a line from FP and add it to SEQ.  Return 0 if EOF, 1 otherwise.  */
299
300 static int
301 getseq (FILE *fp, struct seq *seq)
302 {
303   if (seq->count == seq->alloc)
304     {
305       seq->alloc *= 2;
306       seq->lines = (struct line *)
307         xrealloc ((char *) seq->lines, seq->alloc * sizeof (struct line));
308     }
309
310   if (get_line (fp, &seq->lines[seq->count]))
311     {
312       ++seq->count;
313       return 1;
314     }
315   return 0;
316 }
317
318 static void
319 delseq (struct seq *seq)
320 {
321   int i;
322   for (i = 0; i < seq->count; i++)
323     if (seq->lines[i].buf.buffer)
324       freeline (&seq->lines[i]);
325   free ((char *) seq->lines);
326 }
327
328 /* Return <0 if the join field in LINE1 compares less than the one in LINE2;
329    >0 if it compares greater; 0 if it compares equal.  */
330
331 static int
332 keycmp (struct line *line1, struct line *line2)
333 {
334   /* Start of field to compare in each file.  */
335   const unsigned char *beg1, *beg2;
336
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     {
372       /* FIXME: ignore_case does not work with NLS (in particular,
373          with multibyte chars).  */
374       diff = memcasecmp (beg1, beg2, min (len1, len2));
375     }
376   else
377     {
378 #ifdef ENABLE_NLS
379       if (hard_LC_COLLATE)
380         return memcoll ((char *) beg1, len1, (char *) beg2, len2);
381 #endif
382       diff = memcmp (beg1, beg2, min (len1, len2));
383     }
384
385   if (diff)
386     return diff;
387   return len1 - len2;
388 }
389
390 /* Print field N of LINE if it exists and is nonempty, otherwise
391    `empty_filler' if it is nonempty.  */
392
393 static void
394 prfield (int n, struct line *line)
395 {
396   size_t len;
397
398   if (n < line->nfields)
399     {
400       len = line->fields[n].len;
401       if (len)
402         fwrite (line->fields[n].beg, 1, len, stdout);
403       else if (empty_filler)
404         fputs (empty_filler, stdout);
405     }
406   else if (empty_filler)
407     fputs (empty_filler, stdout);
408 }
409
410 /* Print the join of LINE1 and LINE2.  */
411
412 static void
413 prjoin (struct line *line1, struct line *line2)
414 {
415   const struct outlist *outlist;
416
417   outlist = outlist_head.next;
418   if (outlist)
419     {
420       const struct outlist *o;
421
422       o = outlist;
423       while (1)
424         {
425           int field;
426           struct line *line;
427
428           if (o->file == 0)
429             {
430               if (line1 == &uni_blank)
431                 {
432                   line = line2;
433                   field = join_field_2;
434                 }
435               else
436                 {
437                   line = line1;
438                   field = join_field_1;
439                 }
440             }
441           else
442             {
443               line = (o->file == 1 ? line1 : line2);
444               assert (o->field >= 0);
445               field = o->field;
446             }
447           prfield (field, line);
448           o = o->next;
449           if (o == NULL)
450             break;
451           putchar (tab ? tab : ' ');
452         }
453       putchar ('\n');
454     }
455   else
456     {
457       int i;
458
459       if (line1 == &uni_blank)
460         {
461           struct line *t;
462           t = line1;
463           line1 = line2;
464           line2 = t;
465         }
466       prfield (join_field_1, line1);
467       for (i = 0; i < join_field_1 && i < line1->nfields; ++i)
468         {
469           putchar (tab ? tab : ' ');
470           prfield (i, line1);
471         }
472       for (i = join_field_1 + 1; i < line1->nfields; ++i)
473         {
474           putchar (tab ? tab : ' ');
475           prfield (i, line1);
476         }
477
478       for (i = 0; i < join_field_2 && i < line2->nfields; ++i)
479         {
480           putchar (tab ? tab : ' ');
481           prfield (i, line2);
482         }
483       for (i = join_field_2 + 1; i < line2->nfields; ++i)
484         {
485           putchar (tab ? tab : ' ');
486           prfield (i, line2);
487         }
488       putchar ('\n');
489     }
490 }
491
492 /* Print the join of the files in FP1 and FP2.  */
493
494 static void
495 join (FILE *fp1, FILE *fp2)
496 {
497   struct seq seq1, seq2;
498   struct line line;
499   int diff, i, j, eof1, eof2;
500
501   /* Read the first line of each file.  */
502   initseq (&seq1);
503   getseq (fp1, &seq1);
504   initseq (&seq2);
505   getseq (fp2, &seq2);
506
507   while (seq1.count && seq2.count)
508     {
509       diff = keycmp (&seq1.lines[0], &seq2.lines[0]);
510       if (diff < 0)
511         {
512           if (print_unpairables_1)
513             prjoin (&seq1.lines[0], &uni_blank);
514           freeline (&seq1.lines[0]);
515           seq1.count = 0;
516           getseq (fp1, &seq1);
517           continue;
518         }
519       if (diff > 0)
520         {
521           if (print_unpairables_2)
522             prjoin (&uni_blank, &seq2.lines[0]);
523           freeline (&seq2.lines[0]);
524           seq2.count = 0;
525           getseq (fp2, &seq2);
526           continue;
527         }
528
529       /* Keep reading lines from file1 as long as they continue to
530          match the current line from file2.  */
531       eof1 = 0;
532       do
533         if (!getseq (fp1, &seq1))
534           {
535             eof1 = 1;
536             ++seq1.count;
537             break;
538           }
539       while (!keycmp (&seq1.lines[seq1.count - 1], &seq2.lines[0]));
540
541       /* Keep reading lines from file2 as long as they continue to
542          match the current line from file1.  */
543       eof2 = 0;
544       do
545         if (!getseq (fp2, &seq2))
546           {
547             eof2 = 1;
548             ++seq2.count;
549             break;
550           }
551       while (!keycmp (&seq1.lines[0], &seq2.lines[seq2.count - 1]));
552
553       if (print_pairables)
554         {
555           for (i = 0; i < seq1.count - 1; ++i)
556             for (j = 0; j < seq2.count - 1; ++j)
557               prjoin (&seq1.lines[i], &seq2.lines[j]);
558         }
559
560       for (i = 0; i < seq1.count - 1; ++i)
561         freeline (&seq1.lines[i]);
562       if (!eof1)
563         {
564           seq1.lines[0] = seq1.lines[seq1.count - 1];
565           seq1.count = 1;
566         }
567       else
568         seq1.count = 0;
569
570       for (i = 0; i < seq2.count - 1; ++i)
571         freeline (&seq2.lines[i]);
572       if (!eof2)
573         {
574           seq2.lines[0] = seq2.lines[seq2.count - 1];
575           seq2.count = 1;
576         }
577       else
578         seq2.count = 0;
579     }
580
581   if (print_unpairables_1 && seq1.count)
582     {
583       prjoin (&seq1.lines[0], &uni_blank);
584       freeline (&seq1.lines[0]);
585       while (get_line (fp1, &line))
586         {
587           prjoin (&line, &uni_blank);
588           freeline (&line);
589         }
590     }
591
592   if (print_unpairables_2 && seq2.count)
593     {
594       prjoin (&uni_blank, &seq2.lines[0]);
595       freeline (&seq2.lines[0]);
596       while (get_line (fp2, &line))
597         {
598           prjoin (&uni_blank, &line);
599           freeline (&line);
600         }
601     }
602
603   delseq (&seq1);
604   delseq (&seq2);
605 }
606
607 /* Add a field spec for field FIELD of file FILE to `outlist'.  */
608
609 static void
610 add_field (int file, int field)
611 {
612   struct outlist *o;
613
614   assert (file == 0 || file == 1 || file == 2);
615   assert (file == 0 ? field < 0 : field >= 0);
616
617   o = (struct outlist *) xmalloc (sizeof (struct outlist));
618   o->file = file;
619   o->field = field;
620   o->next = NULL;
621
622   /* Add to the end of the list so the fields are in the right order.  */
623   outlist_end->next = o;
624   outlist_end = o;
625 }
626
627 /* Convert a single field specifier string, S, to a *FILE_INDEX, *FIELD_INDEX
628    pair.  In S, the field index string is 1-based; *FIELD_INDEX is zero-based.
629    If S is valid, return zero.  Otherwise, give a diagnostic, don't update
630    *FILE_INDEX or *FIELD_INDEX, and return nonzero.  */
631
632 static int
633 decode_field_spec (const char *s, int *file_index, int *field_index)
634 {
635   int invalid = 1;
636
637   /* The first character must be 0, 1, or 2.  */
638   switch (s[0])
639     {
640     case '0':
641       if (s[1] == '\0')
642         {
643           *file_index = 0;
644           /* Give *field_index an invalid value.  */
645           *field_index = -1;
646           invalid = 0;
647         }
648       else
649         {
650           /* `0' must be all alone -- no `.FIELD'.  */
651           error (0, 0, _("invalid field specifier: `%s'"), s);
652         }
653       break;
654
655     case '1':
656     case '2':
657       if (s[1] == '.' && s[2] != '\0')
658         {
659           strtol_error s_err;
660           long int tmp_long;
661
662           s_err = xstrtol (s + 2, NULL, 10, &tmp_long, "");
663           if (s_err != LONGINT_OK || tmp_long <= 0 || tmp_long > INT_MAX)
664             {
665               error (0, 0, _("invalid field number: `%s'"), s + 2);
666             }
667           else
668             {
669               *file_index = s[0] - '0';
670               /* Convert to a zero-based index.  */
671               *field_index = (int) tmp_long - 1;
672               invalid = 0;
673             }
674         }
675       break;
676
677     default:
678       error (0, 0, _("invalid file number in field spec: `%s'"), s);
679       break;
680     }
681   return invalid;
682 }
683
684 /* Add the comma or blank separated field spec(s) in STR to `outlist'.
685    Return nonzero to indicate failure.  */
686
687 static int
688 add_field_list (const char *c_str)
689 {
690   char *p, *str;
691
692   /* Make a writable copy of c_str.  */
693   str = (char *) alloca (strlen (c_str) + 1);
694   strcpy (str, c_str);
695
696   p = str;
697   do
698     {
699       int invalid;
700       int file_index, field_index;
701       char *spec_item = p;
702
703       p = strpbrk (p, ", \t");
704       if (p)
705         *p++ = 0;
706       invalid = decode_field_spec (spec_item, &file_index, &field_index);
707       if (invalid)
708         return 1;
709       add_field (file_index, field_index);
710       uni_blank.nfields = max (uni_blank.nfields, field_index);
711     }
712   while (p);
713   return 0;
714 }
715
716 /* Create a blank line with COUNT fields separated by tabs.  */
717
718 static void
719 make_blank (struct line *blank, int count)
720 {
721   int i;
722   unsigned char *buffer;
723   struct field *fields;
724   blank->nfields = count;
725   blank->buf.size = blank->buf.length = count + 1;
726   blank->buf.buffer = xmalloc (blank->buf.size);
727   buffer = (unsigned char *) blank->buf.buffer;
728   blank->fields = fields =
729     (struct field *) xmalloc (sizeof (struct field) * count);
730   for (i = 0; i < count; i++)
731     {
732       buffer[i] = '\t';
733       fields[i].beg = &buffer[i];
734       fields[i].len = 0;
735     }
736   buffer[i] = '\n';
737 }
738
739 int
740 main (int argc, char **argv)
741 {
742   char *names[2];
743   FILE *fp1, *fp2;
744   int optc, prev_optc = 0, nfiles;
745
746   program_name = argv[0];
747   setlocale (LC_ALL, "");
748   bindtextdomain (PACKAGE, LOCALEDIR);
749   textdomain (PACKAGE);
750
751   atexit (close_stdout);
752
753 #ifdef ENABLE_NLS
754   hard_LC_COLLATE = hard_locale (LC_COLLATE);
755 #endif
756
757   /* Initialize this before parsing options.  In parsing options,
758      it may be increased.  */
759   uni_blank.nfields = 1;
760
761   nfiles = 0;
762   print_pairables = 1;
763
764   while ((optc = getopt_long_only (argc, argv, "-a:e:i1:2:o:t:v:", longopts,
765                                    NULL)) != -1)
766     {
767       long int val;
768
769       switch (optc)
770         {
771         case 0:
772           break;
773
774         case 'v':
775             print_pairables = 0;
776             /* Fall through.  */
777
778         case 'a':
779           if (xstrtol (optarg, NULL, 10, &val, "") != LONGINT_OK
780               || (val != 1 && val != 2))
781             error (EXIT_FAILURE, 0, _("invalid field number: `%s'"), optarg);
782           if (val == 1)
783             print_unpairables_1 = 1;
784           else
785             print_unpairables_2 = 1;
786           break;
787
788         case 'e':
789           empty_filler = optarg;
790           break;
791
792         case 'i':
793           ignore_case = 1;
794           break;
795
796         case '1':
797           if (xstrtol (optarg, NULL, 10, &val, "") != LONGINT_OK
798               || val <= 0 || val > INT_MAX)
799             {
800               error (EXIT_FAILURE, 0,
801                      _("invalid field number for file 1: `%s'"), optarg);
802             }
803           join_field_1 = (int) val - 1;
804           break;
805
806         case '2':
807           if (xstrtol (optarg, NULL, 10, &val, "") != LONGINT_OK
808               || val <= 0 || val > INT_MAX)
809             error (EXIT_FAILURE, 0,
810                    _("invalid field number for file 2: `%s'"), optarg);
811           join_field_2 = (int) val - 1;
812           break;
813
814         case 'j':
815           if (xstrtol (optarg, NULL, 10, &val, "") != LONGINT_OK
816               || val <= 0 || val > INT_MAX)
817             error (EXIT_FAILURE, 0, _("invalid field number: `%s'"), optarg);
818           join_field_1 = join_field_2 = (int) val - 1;
819           break;
820
821         case 'o':
822           if (add_field_list (optarg))
823             exit (EXIT_FAILURE);
824           break;
825
826         case 't':
827           tab = *optarg;
828           break;
829
830         case 1:         /* Non-option argument.  */
831           if (prev_optc == 'o' && optind <= argc - 2)
832             {
833               if (add_field_list (optarg))
834                 exit (EXIT_FAILURE);
835
836               /* Might be continuation of args to -o.  */
837               continue;         /* Don't change `prev_optc'.  */
838             }
839
840           if (nfiles > 1)
841             {
842               error (0, 0, _("too many non-option arguments"));
843               usage (1);
844             }
845           names[nfiles++] = optarg;
846           break;
847
848         case_GETOPT_HELP_CHAR;
849
850         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
851
852         default:
853           usage (1);
854         }
855       prev_optc = optc;
856     }
857
858   /* Now that we've seen the options, we can construct the blank line
859      structure.  */
860   make_blank (&uni_blank, uni_blank.nfields);
861
862   if (nfiles != 2)
863     {
864       error (0, 0, _("too few non-option arguments"));
865       usage (1);
866     }
867
868   fp1 = STREQ (names[0], "-") ? stdin : fopen (names[0], "r");
869   if (!fp1)
870     error (EXIT_FAILURE, errno, "%s", names[0]);
871   fp2 = STREQ (names[1], "-") ? stdin : fopen (names[1], "r");
872   if (!fp2)
873     error (EXIT_FAILURE, errno, "%s", names[1]);
874   if (fp1 == fp2)
875     error (EXIT_FAILURE, errno, _("both files cannot be standard input"));
876   join (fp1, fp2);
877
878   if (fp1 != stdin && fclose (fp1) == EOF)
879     error (EXIT_FAILURE, errno, "%s", names[0]);
880   if (fp2 != stdin && fclose (fp2) == EOF)
881     error (EXIT_FAILURE, errno, "%s", names[1]);
882   if ((fp1 == stdin || fp2 == stdin) && fclose (stdin) == EOF)
883     error (EXIT_FAILURE, errno, "-");
884
885   exit (EXIT_SUCCESS);
886 }