1 /* join - join lines of two files on a common field
2 Copyright (C) 1991, 1995-2006, 2008-2011 Free Software Foundation, Inc.
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 3 of the License, or
7 (at your option) any later version.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 Written by Mike Haertel, mike@gnu.ai.mit.edu. */
22 #include <sys/types.h>
28 #include "hard-locale.h"
29 #include "linebuffer.h"
30 #include "memcasecmp.h"
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "join"
40 #define AUTHORS proper_name ("Mike Haertel")
42 #define join system_join
44 #define SWAPLINES(a, b) do { \
45 struct line *tmp = a; \
50 /* An element of the list identifying which fields to print for each
54 /* File number: 0, 1, or 2. 0 means use the join field.
55 1 means use the first file argument, 2 the second. */
58 /* Field index (zero-based), specified only when FILE is 1 or 2. */
64 /* A field of a line. */
67 char *beg
; /* First character in field. */
68 size_t len
; /* The length of the field. */
71 /* A line read from an input file. */
74 struct linebuffer buf
; /* The line itself. */
75 size_t nfields
; /* Number of elements in `fields'. */
76 size_t nfields_allocated
; /* Number of elements allocated for `fields'. */
80 /* One or more consecutive lines read from a file that all have the
81 same join field value. */
84 size_t count
; /* Elements used in `lines'. */
85 size_t alloc
; /* Elements allocated in `lines'. */
89 /* The previous line read from each file. */
90 static struct line
*prevline
[2] = {NULL
, NULL
};
92 /* This provides an extra line buffer for each file. We need these if we
93 try to read two consecutive lines into the same buffer, since we don't
94 want to overwrite the previous buffer before we check order. */
95 static struct line
*spareline
[2] = {NULL
, NULL
};
97 /* True if the LC_COLLATE locale is hard. */
98 static bool hard_LC_COLLATE
;
100 /* If nonzero, print unpairable lines in file 1 or 2. */
101 static bool print_unpairables_1
, print_unpairables_2
;
103 /* If nonzero, print pairable lines. */
104 static bool print_pairables
;
106 /* If nonzero, we have seen at least one unpairable line. */
107 static bool seen_unpairable
;
109 /* If nonzero, we have warned about disorder in that file. */
110 static bool issued_disorder_warning
[2];
112 /* Empty output field filler. */
113 static char const *empty_filler
;
115 /* Whether to ensure the same number of fields are output from each line. */
116 static bool autoformat
;
117 /* The number of fields to output for each line.
118 Only significant when autoformat is true. */
119 static size_t autocount_1
;
120 static size_t autocount_2
;
122 /* Field to join on; SIZE_MAX means they haven't been determined yet. */
123 static size_t join_field_1
= SIZE_MAX
;
124 static size_t join_field_2
= SIZE_MAX
;
126 /* List of fields to print. */
127 static struct outlist outlist_head
;
129 /* Last element in `outlist', where a new element can be added. */
130 static struct outlist
*outlist_end
= &outlist_head
;
132 /* Tab character separating fields. If negative, fields are separated
133 by any nonempty string of blanks, otherwise by exactly one
134 tab character whose value (when cast to unsigned char) equals TAB. */
137 /* If nonzero, check that the input is correctly ordered. */
147 CHECK_ORDER_OPTION
= CHAR_MAX
+ 1,
148 NOCHECK_ORDER_OPTION
,
153 static struct option
const longopts
[] =
155 {"ignore-case", no_argument
, NULL
, 'i'},
156 {"check-order", no_argument
, NULL
, CHECK_ORDER_OPTION
},
157 {"nocheck-order", no_argument
, NULL
, NOCHECK_ORDER_OPTION
},
158 {"header", no_argument
, NULL
, HEADER_LINE_OPTION
},
159 {GETOPT_HELP_OPTION_DECL
},
160 {GETOPT_VERSION_OPTION_DECL
},
164 /* Used to print non-joining lines */
165 static struct line uni_blank
;
167 /* If nonzero, ignore case when comparing join fields. */
168 static bool ignore_case
;
170 /* If nonzero, treat the first line of each file as column headers -
171 join them without checking for ordering */
172 static bool join_header_lines
;
177 if (status
!= EXIT_SUCCESS
)
178 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
183 Usage: %s [OPTION]... FILE1 FILE2\n\
187 For each pair of input lines with identical join fields, write a line to\n\
188 standard output. The default join field is the first, delimited\n\
189 by whitespace. When FILE1 or FILE2 (not both) is -, read standard input.\n\
191 -a FILENUM print unpairable lines coming from file FILENUM, where\n\
192 FILENUM is 1 or 2, corresponding to FILE1 or FILE2\n\
193 -e EMPTY replace missing input fields with EMPTY\n\
196 -i, --ignore-case ignore differences in case when comparing fields\n\
197 -j FIELD equivalent to `-1 FIELD -2 FIELD'\n\
198 -o FORMAT obey FORMAT while constructing output line\n\
199 -t CHAR use CHAR as input and output field separator\n\
202 -v FILENUM like -a FILENUM, but suppress joined output lines\n\
203 -1 FIELD join on this FIELD of file 1\n\
204 -2 FIELD join on this FIELD of file 2\n\
205 --check-order check that the input is correctly sorted, even\n\
206 if all input lines are pairable\n\
207 --nocheck-order do not check that the input is correctly sorted\n\
208 --header treat the first line in each file as field headers,\n\
209 print them without trying to pair them\n\
211 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
212 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
215 Unless -t CHAR is given, leading blanks separate fields and are ignored,\n\
216 else fields are separated by CHAR. Any FIELD is a field number counted\n\
217 from 1. FORMAT is one or more comma or blank separated specifications,\n\
218 each being `FILENUM.FIELD' or `0'. Default FORMAT outputs the join field,\n\
219 the remaining fields from FILE1, the remaining fields from FILE2, all\n\
220 separated by CHAR. If FORMAT is the keyword 'auto', then the first\n\
221 line of each file determines the number of fields output for each line.\n\
223 Important: FILE1 and FILE2 must be sorted on the join fields.\n\
224 E.g., use ` sort -k 1b,1 ' if `join' has no options,\n\
225 or use ` join -t '' ' if `sort' has no options.\n\
226 Note, comparisons honor the rules specified by `LC_COLLATE'.\n\
227 If the input is not sorted and some lines cannot be joined, a\n\
228 warning message will be given.\n\
230 emit_ancillary_info ();
235 /* Record a field in LINE, with location FIELD and size LEN. */
238 extract_field (struct line
*line
, char *field
, size_t len
)
240 if (line
->nfields
>= line
->nfields_allocated
)
242 line
->fields
= X2NREALLOC (line
->fields
, &line
->nfields_allocated
);
244 line
->fields
[line
->nfields
].beg
= field
;
245 line
->fields
[line
->nfields
].len
= len
;
249 /* Fill in the `fields' structure in LINE. */
252 xfields (struct line
*line
)
254 char *ptr
= line
->buf
.buffer
;
255 char const *lim
= ptr
+ line
->buf
.length
- 1;
260 if (0 <= tab
&& tab
!= '\n')
263 for (; (sep
= memchr (ptr
, tab
, lim
- ptr
)) != NULL
; ptr
= sep
+ 1)
264 extract_field (line
, ptr
, sep
- ptr
);
268 /* Skip leading blanks before the first field. */
269 while (isblank (to_uchar (*ptr
)))
276 for (sep
= ptr
+ 1; sep
!= lim
&& ! isblank (to_uchar (*sep
)); sep
++)
278 extract_field (line
, ptr
, sep
- ptr
);
281 for (ptr
= sep
+ 1; ptr
!= lim
&& isblank (to_uchar (*ptr
)); ptr
++)
287 extract_field (line
, ptr
, lim
- ptr
);
291 freeline (struct line
*line
)
297 free (line
->buf
.buffer
);
298 line
->buf
.buffer
= NULL
;
301 /* Return <0 if the join field in LINE1 compares less than the one in LINE2;
302 >0 if it compares greater; 0 if it compares equal.
303 Report an error and exit if the comparison fails.
304 Use join fields JF_1 and JF_2 respectively. */
307 keycmp (struct line
const *line1
, struct line
const *line2
,
308 size_t jf_1
, size_t jf_2
)
310 /* Start of field to compare in each file. */
315 size_t len2
; /* Length of fields to compare. */
318 if (jf_1
< line1
->nfields
)
320 beg1
= line1
->fields
[jf_1
].beg
;
321 len1
= line1
->fields
[jf_1
].len
;
329 if (jf_2
< line2
->nfields
)
331 beg2
= line2
->fields
[jf_2
].beg
;
332 len2
= line2
->fields
[jf_2
].len
;
341 return len2
== 0 ? 0 : -1;
347 /* FIXME: ignore_case does not work with NLS (in particular,
348 with multibyte chars). */
349 diff
= memcasecmp (beg1
, beg2
, MIN (len1
, len2
));
354 return xmemcoll (beg1
, len1
, beg2
, len2
);
355 diff
= memcmp (beg1
, beg2
, MIN (len1
, len2
));
360 return len1
< len2
? -1 : len1
!= len2
;
363 /* Check that successive input lines PREV and CURRENT from input file
364 WHATFILE are presented in order, unless the user may be relying on
365 the GNU extension that input lines may be out of order if no input
366 lines are unpairable.
368 If the user specified --nocheck-order, the check is not made.
369 If the user specified --check-order, the problem is fatal.
370 Otherwise (the default), the message is simply a warning.
372 A message is printed at most once per input file. */
375 check_order (const struct line
*prev
,
376 const struct line
*current
,
379 if (check_input_order
!= CHECK_ORDER_DISABLED
380 && ((check_input_order
== CHECK_ORDER_ENABLED
) || seen_unpairable
))
382 if (!issued_disorder_warning
[whatfile
-1])
384 size_t join_field
= whatfile
== 1 ? join_field_1
: join_field_2
;
385 if (keycmp (prev
, current
, join_field
, join_field
) > 0)
387 error ((check_input_order
== CHECK_ORDER_ENABLED
389 0, _("file %d is not in sorted order"), whatfile
);
391 /* If we get to here, the message was just a warning, but we
392 want only to issue it once. */
393 issued_disorder_warning
[whatfile
-1] = true;
400 reset_line (struct line
*line
)
406 init_linep (struct line
**linep
)
408 struct line
*line
= xmalloc (sizeof *line
);
409 memset (line
, '\0', sizeof *line
);
414 /* Read a line from FP into LINE and split it into fields.
415 Return true if successful. */
418 get_line (FILE *fp
, struct line
**linep
, int which
)
420 struct line
*line
= *linep
;
422 if (line
== prevline
[which
- 1])
424 SWAPLINES (line
, spareline
[which
- 1]);
431 line
= init_linep (linep
);
433 if (! readlinebuffer (&line
->buf
, fp
))
436 error (EXIT_FAILURE
, errno
, _("read error"));
443 if (prevline
[which
- 1])
444 check_order (prevline
[which
- 1], line
, which
);
446 prevline
[which
- 1] = line
;
451 free_spareline (void)
455 for (i
= 0; i
< ARRAY_CARDINALITY (spareline
); i
++)
459 freeline (spareline
[i
]);
466 initseq (struct seq
*seq
)
473 /* Read a line from FP and add it to SEQ. Return true if successful. */
476 getseq (FILE *fp
, struct seq
*seq
, int whichfile
)
478 if (seq
->count
== seq
->alloc
)
481 seq
->lines
= X2NREALLOC (seq
->lines
, &seq
->alloc
);
482 for (i
= seq
->count
; i
< seq
->alloc
; i
++)
483 seq
->lines
[i
] = NULL
;
486 if (get_line (fp
, &seq
->lines
[seq
->count
], whichfile
))
494 /* Read a line from FP and add it to SEQ, as the first item if FIRST is
495 true, else as the next. */
497 advance_seq (FILE *fp
, struct seq
*seq
, bool first
, int whichfile
)
502 return getseq (fp
, seq
, whichfile
);
506 delseq (struct seq
*seq
)
509 for (i
= 0; i
< seq
->alloc
; i
++)
511 freeline (seq
->lines
[i
]);
512 free (seq
->lines
[i
]);
518 /* Print field N of LINE if it exists and is nonempty, otherwise
519 `empty_filler' if it is nonempty. */
522 prfield (size_t n
, struct line
const *line
)
526 if (n
< line
->nfields
)
528 len
= line
->fields
[n
].len
;
530 fwrite (line
->fields
[n
].beg
, 1, len
, stdout
);
531 else if (empty_filler
)
532 fputs (empty_filler
, stdout
);
534 else if (empty_filler
)
535 fputs (empty_filler
, stdout
);
538 /* Output all the fields in line, other than the join field. */
541 prfields (struct line
const *line
, size_t join_field
, size_t autocount
)
544 size_t nfields
= autoformat
? autocount
: line
->nfields
;
545 char output_separator
= tab
< 0 ? ' ' : tab
;
547 for (i
= 0; i
< join_field
&& i
< nfields
; ++i
)
549 putchar (output_separator
);
552 for (i
= join_field
+ 1; i
< nfields
; ++i
)
554 putchar (output_separator
);
559 /* Print the join of LINE1 and LINE2. */
562 prjoin (struct line
const *line1
, struct line
const *line2
)
564 const struct outlist
*outlist
;
565 char output_separator
= tab
< 0 ? ' ' : tab
;
567 struct line
const *line
;
569 outlist
= outlist_head
.next
;
572 const struct outlist
*o
;
579 if (line1
== &uni_blank
)
582 field
= join_field_2
;
587 field
= join_field_1
;
592 line
= (o
->file
== 1 ? line1
: line2
);
595 prfield (field
, line
);
599 putchar (output_separator
);
605 if (line1
== &uni_blank
)
608 field
= join_field_2
;
613 field
= join_field_1
;
616 /* Output the join field. */
617 prfield (field
, line
);
619 /* Output other fields. */
620 prfields (line1
, join_field_1
, autocount_1
);
621 prfields (line2
, join_field_2
, autocount_2
);
627 /* Print the join of the files in FP1 and FP2. */
630 join (FILE *fp1
, FILE *fp2
)
632 struct seq seq1
, seq2
;
636 fadvise (fp1
, FADVISE_SEQUENTIAL
);
637 fadvise (fp2
, FADVISE_SEQUENTIAL
);
639 /* Read the first line of each file. */
641 getseq (fp1
, &seq1
, 1);
643 getseq (fp2
, &seq2
, 2);
647 autocount_1
= seq1
.count
? seq1
.lines
[0]->nfields
: 0;
648 autocount_2
= seq2
.count
? seq2
.lines
[0]->nfields
: 0;
651 if (join_header_lines
&& (seq1
.count
|| seq2
.count
))
653 struct line
const *hline1
= seq1
.count
? seq1
.lines
[0] : &uni_blank
;
654 struct line
const *hline2
= seq2
.count
? seq2
.lines
[0] : &uni_blank
;
655 prjoin (hline1
, hline2
);
659 advance_seq (fp1
, &seq1
, true, 1);
661 advance_seq (fp2
, &seq2
, true, 2);
664 while (seq1
.count
&& seq2
.count
)
667 diff
= keycmp (seq1
.lines
[0], seq2
.lines
[0],
668 join_field_1
, join_field_2
);
671 if (print_unpairables_1
)
672 prjoin (seq1
.lines
[0], &uni_blank
);
673 advance_seq (fp1
, &seq1
, true, 1);
674 seen_unpairable
= true;
679 if (print_unpairables_2
)
680 prjoin (&uni_blank
, seq2
.lines
[0]);
681 advance_seq (fp2
, &seq2
, true, 2);
682 seen_unpairable
= true;
686 /* Keep reading lines from file1 as long as they continue to
687 match the current line from file2. */
690 if (!advance_seq (fp1
, &seq1
, false, 1))
696 while (!keycmp (seq1
.lines
[seq1
.count
- 1], seq2
.lines
[0],
697 join_field_1
, join_field_2
));
699 /* Keep reading lines from file2 as long as they continue to
700 match the current line from file1. */
703 if (!advance_seq (fp2
, &seq2
, false, 2))
709 while (!keycmp (seq1
.lines
[0], seq2
.lines
[seq2
.count
- 1],
710 join_field_1
, join_field_2
));
714 for (i
= 0; i
< seq1
.count
- 1; ++i
)
717 for (j
= 0; j
< seq2
.count
- 1; ++j
)
718 prjoin (seq1
.lines
[i
], seq2
.lines
[j
]);
724 SWAPLINES (seq1
.lines
[0], seq1
.lines
[seq1
.count
- 1]);
732 SWAPLINES (seq2
.lines
[0], seq2
.lines
[seq2
.count
- 1]);
739 /* If the user did not specify --nocheck-order, then we read the
740 tail ends of both inputs to verify that they are in order. We
741 skip the rest of the tail once we have issued a warning for that
742 file, unless we actually need to print the unpairable lines. */
743 struct line
*line
= NULL
;
744 bool checktail
= false;
746 if (check_input_order
!= CHECK_ORDER_DISABLED
747 && !(issued_disorder_warning
[0] && issued_disorder_warning
[1]))
750 if ((print_unpairables_1
|| checktail
) && seq1
.count
)
752 if (print_unpairables_1
)
753 prjoin (seq1
.lines
[0], &uni_blank
);
755 seen_unpairable
= true;
756 while (get_line (fp1
, &line
, 1))
758 if (print_unpairables_1
)
759 prjoin (line
, &uni_blank
);
760 if (issued_disorder_warning
[0] && !print_unpairables_1
)
765 if ((print_unpairables_2
|| checktail
) && seq2
.count
)
767 if (print_unpairables_2
)
768 prjoin (&uni_blank
, seq2
.lines
[0]);
770 seen_unpairable
= true;
771 while (get_line (fp2
, &line
, 2))
773 if (print_unpairables_2
)
774 prjoin (&uni_blank
, line
);
775 if (issued_disorder_warning
[1] && !print_unpairables_2
)
787 /* Add a field spec for field FIELD of file FILE to `outlist'. */
790 add_field (int file
, size_t field
)
794 assert (file
== 0 || file
== 1 || file
== 2);
795 assert (file
!= 0 || field
== 0);
797 o
= xmalloc (sizeof *o
);
802 /* Add to the end of the list so the fields are in the right order. */
803 outlist_end
->next
= o
;
807 /* Convert a string of decimal digits, STR (the 1-based join field number),
808 to an integral value. Upon successful conversion, return one less
809 (the zero-based field number). Silently convert too-large values
810 to SIZE_MAX - 1. Otherwise, if a value cannot be converted, give a
811 diagnostic and exit. */
814 string_to_join_field (char const *str
)
817 unsigned long int val
;
818 verify (SIZE_MAX
<= ULONG_MAX
);
820 strtol_error s_err
= xstrtoul (str
, NULL
, 10, &val
, "");
821 if (s_err
== LONGINT_OVERFLOW
|| (s_err
== LONGINT_OK
&& SIZE_MAX
< val
))
823 else if (s_err
!= LONGINT_OK
|| val
== 0)
824 error (EXIT_FAILURE
, 0, _("invalid field number: %s"), quote (str
));
831 /* Convert a single field specifier string, S, to a *FILE_INDEX, *FIELD_INDEX
832 pair. In S, the field index string is 1-based; *FIELD_INDEX is zero-based.
833 If S is valid, return true. Otherwise, give a diagnostic and exit. */
836 decode_field_spec (const char *s
, int *file_index
, size_t *field_index
)
838 /* The first character must be 0, 1, or 2. */
844 /* `0' must be all alone -- no `.FIELD'. */
845 error (EXIT_FAILURE
, 0, _("invalid field specifier: %s"), quote (s
));
854 error (EXIT_FAILURE
, 0, _("invalid field specifier: %s"), quote (s
));
855 *file_index
= s
[0] - '0';
856 *field_index
= string_to_join_field (s
+ 2);
860 error (EXIT_FAILURE
, 0,
861 _("invalid file number in field spec: %s"), quote (s
));
863 /* Tell gcc -W -Wall that we can't get beyond this point.
864 This avoids a warning (otherwise legit) that the caller's copies
865 of *file_index and *field_index might be used uninitialized. */
872 /* Add the comma or blank separated field spec(s) in STR to `outlist'. */
875 add_field_list (char *str
)
883 char const *spec_item
= p
;
885 p
= strpbrk (p
, ", \t");
888 decode_field_spec (spec_item
, &file_index
, &field_index
);
889 add_field (file_index
, field_index
);
894 /* Set the join field *VAR to VAL, but report an error if *VAR is set
895 more than once to incompatible values. */
898 set_join_field (size_t *var
, size_t val
)
900 if (*var
!= SIZE_MAX
&& *var
!= val
)
902 unsigned long int var1
= *var
+ 1;
903 unsigned long int val1
= val
+ 1;
904 error (EXIT_FAILURE
, 0, _("incompatible join fields %lu, %lu"),
910 /* Status of command-line arguments. */
914 /* This argument must be an operand, i.e., one of the files to be
918 /* This might be the argument of the preceding -j1 or -j2 option,
919 or it might be an operand. */
923 /* This might be the argument of the preceding -o option, or it might be
928 /* Add NAME to the array of input file NAMES with operand statuses
929 OPERAND_STATUS; currently there are NFILES names in the list. */
932 add_file_name (char *name
, char *names
[2],
933 int operand_status
[2], int joption_count
[2], int *nfiles
,
934 int *prev_optc_status
, int *optc_status
)
940 bool op0
= (operand_status
[0] == MUST_BE_OPERAND
);
941 char *arg
= names
[op0
];
942 switch (operand_status
[op0
])
944 case MUST_BE_OPERAND
:
945 error (0, 0, _("extra operand %s"), quote (name
));
946 usage (EXIT_FAILURE
);
948 case MIGHT_BE_J1_ARG
:
950 set_join_field (&join_field_1
, string_to_join_field (arg
));
953 case MIGHT_BE_J2_ARG
:
955 set_join_field (&join_field_2
, string_to_join_field (arg
));
959 add_field_list (arg
);
964 operand_status
[0] = operand_status
[1];
970 operand_status
[n
] = *prev_optc_status
;
973 if (*prev_optc_status
== MIGHT_BE_O_ARG
)
974 *optc_status
= MIGHT_BE_O_ARG
;
978 main (int argc
, char **argv
)
981 int prev_optc_status
= MUST_BE_OPERAND
;
982 int operand_status
[2];
983 int joption_count
[2] = { 0, 0 };
990 initialize_main (&argc
, &argv
);
991 set_program_name (argv
[0]);
992 setlocale (LC_ALL
, "");
993 bindtextdomain (PACKAGE
, LOCALEDIR
);
994 textdomain (PACKAGE
);
995 hard_LC_COLLATE
= hard_locale (LC_COLLATE
);
997 atexit (close_stdout
);
998 atexit (free_spareline
);
1000 print_pairables
= true;
1001 seen_unpairable
= false;
1002 issued_disorder_warning
[0] = issued_disorder_warning
[1] = false;
1003 check_input_order
= CHECK_ORDER_DEFAULT
;
1005 while ((optc
= getopt_long (argc
, argv
, "-a:e:i1:2:j:o:t:v:",
1009 optc_status
= MUST_BE_OPERAND
;
1014 print_pairables
= false;
1019 unsigned long int val
;
1020 if (xstrtoul (optarg
, NULL
, 10, &val
, "") != LONGINT_OK
1021 || (val
!= 1 && val
!= 2))
1022 error (EXIT_FAILURE
, 0,
1023 _("invalid field number: %s"), quote (optarg
));
1025 print_unpairables_1
= true;
1027 print_unpairables_2
= true;
1032 if (empty_filler
&& ! STREQ (empty_filler
, optarg
))
1033 error (EXIT_FAILURE
, 0,
1034 _("conflicting empty-field replacement strings"));
1035 empty_filler
= optarg
;
1043 set_join_field (&join_field_1
, string_to_join_field (optarg
));
1047 set_join_field (&join_field_2
, string_to_join_field (optarg
));
1051 if ((optarg
[0] == '1' || optarg
[0] == '2') && !optarg
[1]
1052 && optarg
== argv
[optind
- 1] + 2)
1054 /* The argument was either "-j1" or "-j2". */
1055 bool is_j2
= (optarg
[0] == '2');
1056 joption_count
[is_j2
]++;
1057 optc_status
= MIGHT_BE_J1_ARG
+ is_j2
;
1061 set_join_field (&join_field_1
, string_to_join_field (optarg
));
1062 set_join_field (&join_field_2
, join_field_1
);
1067 if (STREQ (optarg
, "auto"))
1071 add_field_list (optarg
);
1072 optc_status
= MIGHT_BE_O_ARG
;
1078 unsigned char newtab
= optarg
[0];
1080 newtab
= '\n'; /* '' => process the whole line. */
1083 if (STREQ (optarg
, "\\0"))
1086 error (EXIT_FAILURE
, 0, _("multi-character tab %s"),
1089 if (0 <= tab
&& tab
!= newtab
)
1090 error (EXIT_FAILURE
, 0, _("incompatible tabs"));
1095 case NOCHECK_ORDER_OPTION
:
1096 check_input_order
= CHECK_ORDER_DISABLED
;
1099 case CHECK_ORDER_OPTION
:
1100 check_input_order
= CHECK_ORDER_ENABLED
;
1103 case 1: /* Non-option argument. */
1104 add_file_name (optarg
, names
, operand_status
, joption_count
,
1105 &nfiles
, &prev_optc_status
, &optc_status
);
1108 case HEADER_LINE_OPTION
:
1109 join_header_lines
= true;
1112 case_GETOPT_HELP_CHAR
;
1114 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
1117 usage (EXIT_FAILURE
);
1120 prev_optc_status
= optc_status
;
1123 /* Process any operands after "--". */
1124 prev_optc_status
= MUST_BE_OPERAND
;
1125 while (optind
< argc
)
1126 add_file_name (argv
[optind
++], names
, operand_status
, joption_count
,
1127 &nfiles
, &prev_optc_status
, &optc_status
);
1132 error (0, 0, _("missing operand"));
1134 error (0, 0, _("missing operand after %s"), quote (argv
[argc
- 1]));
1135 usage (EXIT_FAILURE
);
1138 /* If "-j1" was specified and it turns out not to have had an argument,
1139 treat it as "-j 1". Likewise for -j2. */
1140 for (i
= 0; i
< 2; i
++)
1141 if (joption_count
[i
] != 0)
1143 set_join_field (&join_field_1
, i
);
1144 set_join_field (&join_field_2
, i
);
1147 if (join_field_1
== SIZE_MAX
)
1149 if (join_field_2
== SIZE_MAX
)
1152 fp1
= STREQ (names
[0], "-") ? stdin
: fopen (names
[0], "r");
1154 error (EXIT_FAILURE
, errno
, "%s", names
[0]);
1155 fp2
= STREQ (names
[1], "-") ? stdin
: fopen (names
[1], "r");
1157 error (EXIT_FAILURE
, errno
, "%s", names
[1]);
1159 error (EXIT_FAILURE
, errno
, _("both files cannot be standard input"));
1162 if (fclose (fp1
) != 0)
1163 error (EXIT_FAILURE
, errno
, "%s", names
[0]);
1164 if (fclose (fp2
) != 0)
1165 error (EXIT_FAILURE
, errno
, "%s", names
[1]);
1167 if (issued_disorder_warning
[0] || issued_disorder_warning
[1])
1168 exit (EXIT_FAILURE
);
1170 exit (EXIT_SUCCESS
);