1 /* paste - merge lines of files
2 Copyright (C) 1984, 1997, 1998, 1999, 2000, 2001 by David M. Ihnat
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)
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David Ihnat. */
20 /* The list of valid escape sequences has been expanded over the Unix
21 version, to include \b, \f, \r, and \v.
23 POSIX changes, bug fixes, long-named options, and cleanup
24 by David MacKenzie <djm@gnu.ai.mit.edu>.
28 -s Paste one file at a time rather than
29 one line from each file.
30 --delimiters=delim-list
31 -d delim-list Consecutively use the characters in
32 DELIM-LIST instead of tab to separate
33 merged lines. When DELIM-LIST is exhausted,
34 start again at its beginning.
35 A FILE of `-' means standard input.
36 If no FILEs are given, standard input is used. */
42 #include <sys/types.h>
47 /* The official name of this program (e.g., no `g' prefix). */
48 #define PROGRAM_NAME "paste"
50 #define AUTHORS N_ ("David M. Ihnat and David MacKenzie")
52 /* Indicates that no delimiter should be added in the current position. */
53 #define EMPTY_DELIM '\0'
55 static FILE dummy_closed;
56 /* Element marking a file that has reached EOF and been closed. */
57 #define CLOSED (&dummy_closed)
59 static FILE dummy_endlist;
60 /* Element marking end of list of open files. */
61 #define ENDLIST (&dummy_endlist)
63 /* Name this program was run with. */
66 /* If nonzero, we have read standard input at some point. */
67 static int have_read_stdin;
69 /* If nonzero, merge subsequent lines of each file rather than
70 corresponding lines from each file in parallel. */
71 static int serial_merge;
73 /* The delimeters between lines of input files (used cyclically). */
76 /* A pointer to the character after the end of `delims'. */
77 static char *delim_end;
79 static struct option const longopts[] =
81 {"serial", no_argument, 0, 's'},
82 {"delimiters", required_argument, 0, 'd'},
83 {GETOPT_HELP_OPTION_DECL},
84 {GETOPT_VERSION_OPTION_DECL},
88 /* Replace backslash representations of special characters in
89 STRPTR with their actual values.
90 The set of possible backslash characters has been expanded beyond
91 that recognized by the Unix version.
93 Return a pointer to the character after the new end of STRPTR. */
96 collapse_escapes (char *strptr)
98 register char *strout;
100 strout = strptr; /* Start at the same place, anyway. */
104 if (*strptr != '\\') /* Is it an escape character? */
105 *strout++ = *strptr++; /* No, just transfer it. */
111 *strout++ = EMPTY_DELIM;
148 /* Perform column paste on the NFILES files named in FNAMPTR.
149 Return 0 if no errors, 1 if one or more files could not be
153 paste_parallel (int nfiles, char **fnamptr)
155 int errors = 0; /* 1 if open or read errors occur. */
156 /* Number of files for which space is allocated in `delbuf' and `fileptr'.
157 Enlarged as necessary. */
158 int file_list_size = 12;
159 int chr IF_LINT (= 0); /* Input character. */
160 int line_length; /* Number of chars in line. */
161 int somedone; /* 0 if all files empty for this line. */
162 /* If all files are just ready to be closed, or will be on this
163 round, the string of delimiters must be preserved.
164 delbuf[0] through delbuf[file_list_size]
165 store the delimiters for closed files. */
167 int delims_saved; /* Number of delims saved in `delbuf'. */
168 register char *delimptr; /* Cycling pointer into `delims'. */
169 FILE **fileptr; /* Streams open to the files to process. */
170 int files_open; /* Number of files still open to process. */
171 int i; /* Loop index. */
172 int opened_stdin = 0; /* Nonzero if any fopen got fd 0. */
174 delbuf = (char *) xmalloc (file_list_size + 2);
175 fileptr = (FILE **) xmalloc ((file_list_size + 1) * sizeof (FILE *));
177 /* Attempt to open all files. This could be expanded to an infinite
178 number of files, but at the (considerable) expense of remembering
179 each file and its current offset, then opening/reading/closing. */
181 for (files_open = 0; files_open < nfiles; ++files_open)
183 if (files_open == file_list_size - 2)
185 file_list_size += 12;
186 delbuf = (char *) xrealloc (delbuf, file_list_size + 2);
187 fileptr = (FILE **) xrealloc ((char *) fileptr, (file_list_size + 1)
190 if (STREQ (fnamptr[files_open], "-"))
193 fileptr[files_open] = stdin;
197 fileptr[files_open] = fopen (fnamptr[files_open], "r");
198 if (fileptr[files_open] == NULL)
199 error (EXIT_FAILURE, errno, "%s", fnamptr[files_open]);
200 else if (fileno (fileptr[files_open]) == 0)
205 fileptr[files_open] = ENDLIST;
207 if (opened_stdin && have_read_stdin)
208 error (EXIT_FAILURE, 0, _("standard input is closed"));
210 /* Read a line from each file and output it to stdout separated by a
211 delimiter, until we go through the loop without successfully
212 reading from any of the files. */
216 /* Set up for the next line. */
221 for (i = 0; fileptr[i] != ENDLIST && files_open; i++)
223 line_length = 0; /* Clear so we can easily detect EOF. */
224 if (fileptr[i] != CLOSED)
226 chr = getc (fileptr[i]);
227 if (chr != EOF && delims_saved)
229 fwrite (delbuf, sizeof (char), delims_saved, stdout);
239 chr = getc (fileptr[i]);
243 if (line_length == 0)
245 /* EOF, read error, or closed file.
246 If an EOF or error, close the file and mark it in the list. */
247 if (fileptr[i] != CLOSED)
249 if (ferror (fileptr[i]))
251 error (0, errno, "%s", fnamptr[i]);
254 if (fileptr[i] == stdin)
255 clearerr (fileptr[i]); /* Also clear EOF. */
256 else if (fclose (fileptr[i]) == EOF)
258 error (0, errno, "%s", fnamptr[i]);
266 if (fileptr[i + 1] == ENDLIST)
268 /* End of this output line.
269 Is this the end of the whole thing? */
272 /* No. Some files were not closed for this line. */
275 fwrite (delbuf, sizeof (char), delims_saved, stdout);
280 continue; /* Next read of files, or exit. */
284 /* Closed file; add delimiter to `delbuf'. */
285 if (*delimptr != EMPTY_DELIM)
286 delbuf[delims_saved++] = *delimptr;
287 if (++delimptr == delim_end)
293 /* Some data read. */
296 /* Except for last file, replace last newline with delim. */
297 if (fileptr[i + 1] != ENDLIST)
301 if (*delimptr != EMPTY_DELIM)
302 putc (*delimptr, stdout);
303 if (++delimptr == delim_end)
314 /* Perform serial paste on the NFILES files named in FNAMPTR.
315 Return 0 if no errors, 1 if one or more files could not be
319 paste_serial (int nfiles, char **fnamptr)
321 int errors = 0; /* 1 if open or read errors occur. */
322 register int charnew, charold; /* Current and previous char read. */
323 register char *delimptr; /* Current delimiter char. */
324 register FILE *fileptr; /* Open for reading current file. */
326 for (; nfiles; nfiles--, fnamptr++)
328 if (STREQ (*fnamptr, "-"))
335 fileptr = fopen (*fnamptr, "r");
338 error (0, errno, "%s", *fnamptr);
344 delimptr = delims; /* Set up for delimiter string. */
346 charold = getc (fileptr);
349 /* `charold' is set up. Hit it!
350 Keep reading characters, stashing them in `charnew';
351 output `charold', converting to the appropriate delimiter
352 character if needed. After the EOF, output `charold'
353 if it's a newline; otherwise, output it and then a newline. */
355 while ((charnew = getc (fileptr)) != EOF)
357 /* Process the old character. */
360 if (*delimptr != EMPTY_DELIM)
361 putc (*delimptr, stdout);
363 if (++delimptr == delim_end)
367 putc (charold, stdout);
372 /* Hit EOF. Process that last character. */
373 putc (charold, stdout);
379 if (ferror (fileptr))
381 error (0, errno, "%s", *fnamptr);
384 if (fileptr == stdin)
385 clearerr (fileptr); /* Also clear EOF. */
386 else if (fclose (fileptr) == EOF)
388 error (0, errno, "%s", *fnamptr);
399 fprintf (stderr, _("Try `%s --help' for more information.\n"),
404 Usage: %s [OPTION]... [FILE]...\n\
408 Write lines consisting of the sequentially corresponding lines from\n\
409 each FILE, separated by TABs, to standard output.\n\
410 With no FILE, or when FILE is -, read standard input.\n\
414 Mandatory arguments to long options are mandatory for short options too.\n\
417 -d, --delimiters=LIST reuse characters from LIST instead of TABs\n\
418 -s, --serial paste one file at a time instead of in parallel\n\
421 --help display this help and exit\n\
422 --version output version information and exit\n\
424 /* FIXME: add a couple of examples. */
428 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
430 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
434 main (int argc, char **argv)
436 int optc, exit_status;
437 char default_delims[2], zero_delims[3];
439 program_name = argv[0];
440 setlocale (LC_ALL, "");
441 bindtextdomain (PACKAGE, LOCALEDIR);
442 textdomain (PACKAGE);
444 atexit (close_stdout);
448 delims = default_delims;
449 strcpy (delims, "\t");
450 strcpy (zero_delims, "\\0");
452 while ((optc = getopt_long (argc, argv, "d:s", longopts, NULL)) != -1)
460 /* Delimiter character(s). */
461 if (optarg[0] == '\0')
462 optarg = zero_delims;
470 case_GETOPT_HELP_CHAR;
472 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
482 delim_end = collapse_escapes (delims);
485 exit_status = paste_parallel (argc - optind, &argv[optind]);
487 exit_status = paste_serial (argc - optind, &argv[optind]);
488 if (have_read_stdin && fclose (stdin) == EOF)
489 error (EXIT_FAILURE, errno, "-");
490 exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);