1 /* paste - merge lines of files
2 Copyright (C) 1984, 1997-2003 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>
46 /* The official name of this program (e.g., no `g' prefix). */
47 #define PROGRAM_NAME "paste"
49 #define WRITTEN_BY _("Written by David M. Ihnat and David MacKenzie.")
51 /* Indicates that no delimiter should be added in the current position. */
52 #define EMPTY_DELIM '\0'
54 static FILE dummy_closed;
55 /* Element marking a file that has reached EOF and been closed. */
56 #define CLOSED (&dummy_closed)
58 static FILE dummy_endlist;
59 /* Element marking end of list of open files. */
60 #define ENDLIST (&dummy_endlist)
62 /* Name this program was run with. */
65 /* If nonzero, we have read standard input at some point. */
66 static int have_read_stdin;
68 /* If nonzero, merge subsequent lines of each file rather than
69 corresponding lines from each file in parallel. */
70 static int serial_merge;
72 /* The delimeters between lines of input files (used cyclically). */
75 /* A pointer to the character after the end of `delims'. */
76 static char *delim_end;
78 static struct option const longopts[] =
80 {"serial", no_argument, 0, 's'},
81 {"delimiters", required_argument, 0, 'd'},
82 {GETOPT_HELP_OPTION_DECL},
83 {GETOPT_VERSION_OPTION_DECL},
87 /* Replace backslash representations of special characters in
88 STRPTR with their actual values.
89 The set of possible backslash characters has been expanded beyond
90 that recognized by the Unix version.
92 Return a pointer to the character after the new end of STRPTR. */
95 collapse_escapes (char *strptr)
97 register char *strout;
99 strout = strptr; /* Start at the same place, anyway. */
103 if (*strptr != '\\') /* Is it an escape character? */
104 *strout++ = *strptr++; /* No, just transfer it. */
110 *strout++ = EMPTY_DELIM;
147 /* Perform column paste on the NFILES files named in FNAMPTR.
148 Return 0 if no errors, 1 if one or more files could not be
152 paste_parallel (int nfiles, char **fnamptr)
154 int errors = 0; /* 1 if open or read errors occur. */
155 /* Number of files for which space is allocated in `delbuf' and `fileptr'.
156 Enlarged as necessary. */
157 int file_list_size = 12;
158 int chr IF_LINT (= 0); /* Input character. */
159 int line_length; /* Number of chars in line. */
160 int somedone; /* 0 if all files empty for this line. */
161 /* If all files are just ready to be closed, or will be on this
162 round, the string of delimiters must be preserved.
163 delbuf[0] through delbuf[file_list_size]
164 store the delimiters for closed files. */
166 size_t delims_saved; /* Number of delims saved in `delbuf'. */
167 register char *delimptr; /* Cycling pointer into `delims'. */
168 FILE **fileptr; /* Streams open to the files to process. */
169 int files_open; /* Number of files still open to process. */
170 int i; /* Loop index. */
171 int opened_stdin = 0; /* Nonzero if any fopen got fd 0. */
173 delbuf = xmalloc (file_list_size + 2);
174 fileptr = xmalloc ((file_list_size + 1) * sizeof (FILE *));
176 /* Attempt to open all files. This could be expanded to an infinite
177 number of files, but at the (considerable) expense of remembering
178 each file and its current offset, then opening/reading/closing. */
180 for (files_open = 0; files_open < nfiles; ++files_open)
182 if (files_open == file_list_size - 2)
184 file_list_size += 12;
185 delbuf = xrealloc (delbuf, file_list_size + 2);
186 fileptr = xrealloc (fileptr, (file_list_size + 1) * sizeof (FILE *));
188 if (STREQ (fnamptr[files_open], "-"))
191 fileptr[files_open] = stdin;
195 fileptr[files_open] = fopen (fnamptr[files_open], "r");
196 if (fileptr[files_open] == NULL)
197 error (EXIT_FAILURE, errno, "%s", fnamptr[files_open]);
198 else if (fileno (fileptr[files_open]) == 0)
203 fileptr[files_open] = ENDLIST;
205 if (opened_stdin && have_read_stdin)
206 error (EXIT_FAILURE, 0, _("standard input is closed"));
208 /* Read a line from each file and output it to stdout separated by a
209 delimiter, until we go through the loop without successfully
210 reading from any of the files. */
214 /* Set up for the next line. */
219 for (i = 0; fileptr[i] != ENDLIST && files_open; i++)
221 line_length = 0; /* Clear so we can easily detect EOF. */
222 if (fileptr[i] != CLOSED)
224 chr = getc (fileptr[i]);
225 if (chr != EOF && delims_saved)
227 fwrite (delbuf, sizeof (char), delims_saved, stdout);
237 chr = getc (fileptr[i]);
241 if (line_length == 0)
243 /* EOF, read error, or closed file.
244 If an EOF or error, close the file and mark it in the list. */
245 if (fileptr[i] != CLOSED)
247 if (ferror (fileptr[i]))
249 error (0, errno, "%s", fnamptr[i]);
252 if (fileptr[i] == stdin)
253 clearerr (fileptr[i]); /* Also clear EOF. */
254 else if (fclose (fileptr[i]) == EOF)
256 error (0, errno, "%s", fnamptr[i]);
264 if (fileptr[i + 1] == ENDLIST)
266 /* End of this output line.
267 Is this the end of the whole thing? */
270 /* No. Some files were not closed for this line. */
273 fwrite (delbuf, sizeof (char), delims_saved, stdout);
278 continue; /* Next read of files, or exit. */
282 /* Closed file; add delimiter to `delbuf'. */
283 if (*delimptr != EMPTY_DELIM)
284 delbuf[delims_saved++] = *delimptr;
285 if (++delimptr == delim_end)
291 /* Some data read. */
294 /* Except for last file, replace last newline with delim. */
295 if (fileptr[i + 1] != ENDLIST)
297 if (chr != '\n' && chr != EOF)
299 if (*delimptr != EMPTY_DELIM)
300 putc (*delimptr, stdout);
301 if (++delimptr == delim_end)
306 /* If the last line of the last file lacks a newline,
307 print one anyhow. POSIX requires this. */
308 char c = (chr == EOF ? '\n' : chr);
317 /* Perform serial paste on the NFILES files named in FNAMPTR.
318 Return 0 if no errors, 1 if one or more files could not be
322 paste_serial (int nfiles, char **fnamptr)
324 int errors = 0; /* 1 if open or read errors occur. */
325 register int charnew, charold; /* Current and previous char read. */
326 register char *delimptr; /* Current delimiter char. */
327 register FILE *fileptr; /* Open for reading current file. */
329 for (; nfiles; nfiles--, fnamptr++)
332 if (STREQ (*fnamptr, "-"))
339 fileptr = fopen (*fnamptr, "r");
342 error (0, errno, "%s", *fnamptr);
348 delimptr = delims; /* Set up for delimiter string. */
350 charold = getc (fileptr);
354 /* `charold' is set up. Hit it!
355 Keep reading characters, stashing them in `charnew';
356 output `charold', converting to the appropriate delimiter
357 character if needed. After the EOF, output `charold'
358 if it's a newline; otherwise, output it and then a newline. */
360 while ((charnew = getc (fileptr)) != EOF)
362 /* Process the old character. */
365 if (*delimptr != EMPTY_DELIM)
366 putc (*delimptr, stdout);
368 if (++delimptr == delim_end)
372 putc (charold, stdout);
378 /* Hit EOF. Process that last character. */
379 putc (charold, stdout);
385 if (ferror (fileptr))
387 error (0, saved_errno, "%s", *fnamptr);
390 if (fileptr == stdin)
391 clearerr (fileptr); /* Also clear EOF. */
392 else if (fclose (fileptr) == EOF)
394 error (0, errno, "%s", *fnamptr);
405 fprintf (stderr, _("Try `%s --help' for more information.\n"),
410 Usage: %s [OPTION]... [FILE]...\n\
414 Write lines consisting of the sequentially corresponding lines from\n\
415 each FILE, separated by TABs, to standard output.\n\
416 With no FILE, or when FILE is -, read standard input.\n\
420 Mandatory arguments to long options are mandatory for short options too.\n\
423 -d, --delimiters=LIST reuse characters from LIST instead of TABs\n\
424 -s, --serial paste one file at a time instead of in parallel\n\
426 fputs (HELP_OPTION_DESCRIPTION, stdout);
427 fputs (VERSION_OPTION_DESCRIPTION, stdout);
428 /* FIXME: add a couple of examples. */
429 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
431 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
435 main (int argc, char **argv)
437 int optc, exit_status;
438 char default_delims[2], zero_delims[3];
440 initialize_main (&argc, &argv);
441 program_name = argv[0];
442 setlocale (LC_ALL, "");
443 bindtextdomain (PACKAGE, LOCALEDIR);
444 textdomain (PACKAGE);
446 atexit (close_stdout);
450 delims = default_delims;
451 strcpy (delims, "\t");
452 strcpy (zero_delims, "\\0");
454 while ((optc = getopt_long (argc, argv, "d:s", longopts, NULL)) != -1)
462 /* Delimiter character(s). */
463 if (optarg[0] == '\0')
464 optarg = zero_delims;
472 case_GETOPT_HELP_CHAR;
474 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
477 usage (EXIT_FAILURE);
484 delim_end = collapse_escapes (delims);
487 exit_status = paste_parallel (argc - optind, &argv[optind]);
489 exit_status = paste_serial (argc - optind, &argv[optind]);
490 if (have_read_stdin && fclose (stdin) == EOF)
491 error (EXIT_FAILURE, errno, "-");
492 exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);