.
[coreutils.git] / src / paste.c
blob50e53b1777d94aa4a61189e2242955ab23e67f26
1 /* paste - merge lines of files
2 Copyright (C) 1984 by David M. Ihnat
4 This program is a total rewrite of the Bell Laboratories Unix(Tm)
5 command of the same name, as of System V. It contains no proprietary
6 code, and therefore may be used without violation of any proprietary
7 agreements whatsoever. However, you will notice that the program is
8 copyrighted by me. This is to assure the program does *not* fall
9 into the public domain. Thus, I may specify just what I am now:
10 This program may be freely copied and distributed, provided this notice
11 remains; it may not be sold for profit without express written consent of
12 the author.
13 Please note that I recreated the behavior of the Unix(Tm) 'paste' command
14 as faithfully as possible, with minor exceptions; however,
15 I haven't run a full set of regression tests. Thus, the user of
16 this program accepts full responsibility for any effects or loss;
17 in particular, the author is not responsible for any losses,
18 explicit or incidental, that may be incurred through use of this program.
20 I ask that any bugs (and, if possible, fixes) be reported to me when
21 possible. -David Ihnat (312) 784-4544 ignatz@homebru.chi.il.us
23 The list of valid escape sequences has been expanded over the Unix
24 version, to include \b, \f, \r, and \v.
26 POSIX changes, bug fixes, long-named options, and cleanup
27 by David MacKenzie <djm@gnu.ai.mit.edu>.
29 Options:
30 --serial
31 -s Paste one file at a time rather than
32 one line from each file.
33 --delimiters=delim-list
34 -d delim-list Consecutively use the characters in
35 DELIM-LIST instead of tab to separate
36 merged lines. When DELIM-LIST is exhausted,
37 start again at its beginning.
38 A FILE of `-' means standard input.
39 If no FILEs are given, standard input is used. */
41 #include <config.h>
43 #include <stdio.h>
44 #include <getopt.h>
45 #include <sys/types.h>
46 #include "system.h"
47 #include "version.h"
48 #include "error.h"
50 char *xmalloc ();
51 char *xrealloc ();
53 /* Indicates that no delimiter should be added in the current position. */
54 #define EMPTY_DELIM '\0'
56 static FILE dummy_closed;
57 /* Element marking a file that has reached EOF and been closed. */
58 #define CLOSED (&dummy_closed)
60 static FILE dummy_endlist;
61 /* Element marking end of list of open files. */
62 #define ENDLIST (&dummy_endlist)
64 /* Name this program was run with. */
65 char *program_name;
67 /* If nonzero, we have read standard input at some point. */
68 static int have_read_stdin;
70 /* If nonzero, merge subsequent lines of each file rather than
71 corresponding lines from each file in parallel. */
72 static int serial_merge;
74 /* The delimeters between lines of input files (used cyclically). */
75 static char *delims;
77 /* A pointer to the character after the end of `delims'. */
78 static char *delim_end;
80 /* If nonzero, display usage information and exit. */
81 static int show_help;
83 /* If nonzero, print the version on standard output then exit. */
84 static int show_version;
86 static struct option const longopts[] =
88 {"serial", no_argument, 0, 's'},
89 {"delimiters", required_argument, 0, 'd'},
90 {"help", no_argument, &show_help, 1},
91 {"version", no_argument, &show_version, 1},
92 {0, 0, 0, 0}
95 /* Replace backslash representations of special characters in
96 STRPTR with their actual values.
97 The set of possible backslash characters has been expanded beyond
98 that recognized by the Unix version.
100 Return a pointer to the character after the new end of STRPTR. */
102 static char *
103 collapse_escapes (char *strptr)
105 register char *strout;
107 strout = strptr; /* Start at the same place, anyway. */
109 while (*strptr)
111 if (*strptr != '\\') /* Is it an escape character? */
112 *strout++ = *strptr++; /* No, just transfer it. */
113 else
115 switch (*++strptr)
117 case '0':
118 *strout++ = EMPTY_DELIM;
119 break;
121 case 'b':
122 *strout++ = '\b';
123 break;
125 case 'f':
126 *strout++ = '\f';
127 break;
129 case 'n':
130 *strout++ = '\n';
131 break;
133 case 'r':
134 *strout++ = '\r';
135 break;
137 case 't':
138 *strout++ = '\t';
139 break;
141 case 'v':
142 *strout++ = '\v';
143 break;
145 default:
146 *strout++ = *strptr;
147 break;
149 strptr++;
152 return strout;
155 /* Perform column paste on the NFILES files named in FNAMPTR.
156 Return 0 if no errors, 1 if one or more files could not be
157 opened or read. */
159 static int
160 paste_parallel (int nfiles, char **fnamptr)
162 int errors = 0; /* 1 if open or read errors occur. */
163 /* Number of files for which space is allocated in `delbuf' and `fileptr'.
164 Enlarged as necessary. */
165 int file_list_size = 12;
166 int chr; /* Input character. */
167 int line_length; /* Number of chars in line. */
168 int somedone; /* 0 if all files empty for this line. */
169 /* If all files are just ready to be closed, or will be on this
170 round, the string of delimiters must be preserved.
171 delbuf[0] through delbuf[file_list_size]
172 store the delimiters for closed files. */
173 char *delbuf;
174 int delims_saved; /* Number of delims saved in `delbuf'. */
175 register char *delimptr; /* Cycling pointer into `delims'. */
176 FILE **fileptr; /* Streams open to the files to process. */
177 int files_open; /* Number of files still open to process. */
178 int i; /* Loop index. */
179 int opened_stdin = 0; /* Nonzero if any fopen got fd 0. */
181 #ifdef lint /* Suppress `used before initialized' warning. */
182 chr = 0;
183 #endif
185 delbuf = (char *) xmalloc (file_list_size + 2);
186 fileptr = (FILE **) xmalloc ((file_list_size + 1) * sizeof (FILE *));
188 /* Attempt to open all files. This could be expanded to an infinite
189 number of files, but at the (considerable) expense of remembering
190 each file and its current offset, then opening/reading/closing. */
192 for (files_open = 0; files_open < nfiles; ++files_open)
194 if (files_open == file_list_size - 2)
196 file_list_size += 12;
197 delbuf = (char *) xrealloc (delbuf, file_list_size + 2);
198 fileptr = (FILE **) xrealloc (fileptr, (file_list_size + 1)
199 * sizeof (FILE *));
201 if (!strcmp (fnamptr[files_open], "-"))
203 have_read_stdin = 1;
204 fileptr[files_open] = stdin;
206 else
208 fileptr[files_open] = fopen (fnamptr[files_open], "r");
209 if (fileptr[files_open] == NULL)
210 error (1, errno, "%s", fnamptr[files_open]);
211 else if (fileno (fileptr[files_open]) == 0)
212 opened_stdin = 1;
216 fileptr[files_open] = ENDLIST;
218 if (opened_stdin && have_read_stdin)
219 error (1, 0, _("standard input is closed"));
221 /* Read a line from each file and output it to stdout separated by a
222 delimiter, until we go through the loop without successfully
223 reading from any of the files. */
225 while (files_open)
227 /* Set up for the next line. */
228 somedone = 0;
229 delimptr = delims;
230 delims_saved = 0;
232 for (i = 0; fileptr[i] != ENDLIST && files_open; i++)
234 line_length = 0; /* Clear so we can easily detect EOF. */
235 if (fileptr[i] != CLOSED)
237 chr = getc (fileptr[i]);
238 if (chr != EOF && delims_saved)
240 fwrite (delbuf, sizeof (char), delims_saved, stdout);
241 delims_saved = 0;
244 while (chr != EOF)
246 line_length++;
247 if (chr == '\n')
248 break;
249 putc (chr, stdout);
250 chr = getc (fileptr[i]);
254 if (line_length == 0)
256 /* EOF, read error, or closed file.
257 If an EOF or error, close the file and mark it in the list. */
258 if (fileptr[i] != CLOSED)
260 if (ferror (fileptr[i]))
262 error (0, errno, "%s", fnamptr[i]);
263 errors = 1;
265 if (fileptr[i] == stdin)
266 clearerr (fileptr[i]); /* Also clear EOF. */
267 else if (fclose (fileptr[i]) == EOF)
269 error (0, errno, "%s", fnamptr[i]);
270 errors = 1;
273 fileptr[i] = CLOSED;
274 files_open--;
277 if (fileptr[i + 1] == ENDLIST)
279 /* End of this output line.
280 Is this the end of the whole thing? */
281 if (somedone)
283 /* No. Some files were not closed for this line. */
284 if (delims_saved)
286 fwrite (delbuf, sizeof (char), delims_saved, stdout);
287 delims_saved = 0;
289 putc ('\n', stdout);
291 continue; /* Next read of files, or exit. */
293 else
295 /* Closed file; add delimiter to `delbuf'. */
296 if (*delimptr != EMPTY_DELIM)
297 delbuf[delims_saved++] = *delimptr;
298 if (++delimptr == delim_end)
299 delimptr = delims;
302 else
304 /* Some data read. */
305 somedone++;
307 /* Except for last file, replace last newline with delim. */
308 if (fileptr[i + 1] != ENDLIST)
310 if (chr != '\n')
311 putc (chr, stdout);
312 if (*delimptr != EMPTY_DELIM)
313 putc (*delimptr, stdout);
314 if (++delimptr == delim_end)
315 delimptr = delims;
317 else
318 putc (chr, stdout);
322 return errors;
325 /* Perform serial paste on the NFILES files named in FNAMPTR.
326 Return 0 if no errors, 1 if one or more files could not be
327 opened or read. */
329 static int
330 paste_serial (int nfiles, char **fnamptr)
332 int errors = 0; /* 1 if open or read errors occur. */
333 register int charnew, charold; /* Current and previous char read. */
334 register char *delimptr; /* Current delimiter char. */
335 register FILE *fileptr; /* Open for reading current file. */
337 for (; nfiles; nfiles--, fnamptr++)
339 if (!strcmp (*fnamptr, "-"))
341 have_read_stdin = 1;
342 fileptr = stdin;
344 else
346 fileptr = fopen (*fnamptr, "r");
347 if (fileptr == NULL)
349 error (0, errno, "%s", *fnamptr);
350 errors = 1;
351 continue;
355 delimptr = delims; /* Set up for delimiter string. */
357 charold = getc (fileptr);
358 if (charold != EOF)
360 /* `charold' is set up. Hit it!
361 Keep reading characters, stashing them in `charnew';
362 output `charold', converting to the appropriate delimiter
363 character if needed. After the EOF, output `charold'
364 if it's a newline; otherwise, output it and then a newline. */
366 while ((charnew = getc (fileptr)) != EOF)
368 /* Process the old character. */
369 if (charold == '\n')
371 if (*delimptr != EMPTY_DELIM)
372 putc (*delimptr, stdout);
374 if (++delimptr == delim_end)
375 delimptr = delims;
377 else
378 putc (charold, stdout);
380 charold = charnew;
383 /* Hit EOF. Process that last character. */
384 putc (charold, stdout);
387 if (charold != '\n')
388 putc ('\n', stdout);
390 if (ferror (fileptr))
392 error (0, errno, "%s", *fnamptr);
393 errors = 1;
395 if (fileptr == stdin)
396 clearerr (fileptr); /* Also clear EOF. */
397 else if (fclose (fileptr) == EOF)
399 error (0, errno, "%s", *fnamptr);
400 errors = 1;
403 return errors;
406 static void
407 usage (int status)
409 if (status != 0)
410 fprintf (stderr, _("Try `%s --help' for more information.\n"),
411 program_name);
412 else
414 printf (_("\
415 Usage: %s [OPTION]... [FILE]...\n\
417 program_name);
418 printf (_("\
419 Write lines consisting of the sequentially corresponding lines from\n\
420 each FILE, separated by TABs, to standard output.\n\
421 With no FILE, or when FILE is -, read standard input.\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\
425 --help display this help and exit\n\
426 --version output version information and exit\n\
428 "));
430 exit (status);
433 void
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 have_read_stdin = 0;
441 serial_merge = 0;
442 delims = default_delims;
443 strcpy (delims, "\t");
444 strcpy (zero_delims, "\\0");
446 while ((optc = getopt_long (argc, argv, "d:s", longopts, (int *) 0))
447 != EOF)
449 switch (optc)
451 case 0:
452 break;
454 case 'd':
455 /* Delimiter character(s). */
456 if (optarg[0] == '\0')
457 optarg = zero_delims;
458 delims = optarg;
459 break;
461 case 's':
462 serial_merge++;
463 break;
465 default:
466 usage (1);
470 if (show_version)
472 printf ("paste - %s\n", version_string);
473 exit (0);
476 if (show_help)
477 usage (0);
479 if (optind == argc)
480 argv[argc++] = "-";
482 delim_end = collapse_escapes (delims);
484 if (!serial_merge)
485 exit_status = paste_parallel (argc - optind, &argv[optind]);
486 else
487 exit_status = paste_serial (argc - optind, &argv[optind]);
488 if (have_read_stdin && fclose (stdin) == EOF)
489 error (1, errno, "-");
490 if (ferror (stdout) || fclose (stdout) == EOF)
491 error (1, errno, _("write error"));
492 exit (exit_status);