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