(recheck): Handle a race condition (including <dev,inode>
[coreutils.git] / src / paste.c
blob7dbf7091b5516ac2ccb6bc1bb25ffc0e0aaa91c4
1 /* paste - merge lines of files
2 Copyright (C) 1984, 1997, 1998, 1999 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 "error.h"
49 /* The official name of this program (e.g., no `g' prefix). */
50 #define PROGRAM_NAME "paste"
52 #define AUTHORS "David M. Ihnat"
54 /* Indicates that no delimiter should be added in the current position. */
55 #define EMPTY_DELIM '\0'
57 static FILE dummy_closed;
58 /* Element marking a file that has reached EOF and been closed. */
59 #define CLOSED (&dummy_closed)
61 static FILE dummy_endlist;
62 /* Element marking end of list of open files. */
63 #define ENDLIST (&dummy_endlist)
65 /* Name this program was run with. */
66 char *program_name;
68 /* If nonzero, we have read standard input at some point. */
69 static int have_read_stdin;
71 /* If nonzero, merge subsequent lines of each file rather than
72 corresponding lines from each file in parallel. */
73 static int serial_merge;
75 /* The delimeters between lines of input files (used cyclically). */
76 static char *delims;
78 /* A pointer to the character after the end of `delims'. */
79 static char *delim_end;
81 static struct option const longopts[] =
83 {"serial", no_argument, 0, 's'},
84 {"delimiters", required_argument, 0, 'd'},
85 {GETOPT_HELP_OPTION_DECL},
86 {GETOPT_VERSION_OPTION_DECL},
87 {0, 0, 0, 0}
90 /* Replace backslash representations of special characters in
91 STRPTR with their actual values.
92 The set of possible backslash characters has been expanded beyond
93 that recognized by the Unix version.
95 Return a pointer to the character after the new end of STRPTR. */
97 static char *
98 collapse_escapes (char *strptr)
100 register char *strout;
102 strout = strptr; /* Start at the same place, anyway. */
104 while (*strptr)
106 if (*strptr != '\\') /* Is it an escape character? */
107 *strout++ = *strptr++; /* No, just transfer it. */
108 else
110 switch (*++strptr)
112 case '0':
113 *strout++ = EMPTY_DELIM;
114 break;
116 case 'b':
117 *strout++ = '\b';
118 break;
120 case 'f':
121 *strout++ = '\f';
122 break;
124 case 'n':
125 *strout++ = '\n';
126 break;
128 case 'r':
129 *strout++ = '\r';
130 break;
132 case 't':
133 *strout++ = '\t';
134 break;
136 case 'v':
137 *strout++ = '\v';
138 break;
140 default:
141 *strout++ = *strptr;
142 break;
144 strptr++;
147 return strout;
150 /* Perform column paste on the NFILES files named in FNAMPTR.
151 Return 0 if no errors, 1 if one or more files could not be
152 opened or read. */
154 static int
155 paste_parallel (int nfiles, char **fnamptr)
157 int errors = 0; /* 1 if open or read errors occur. */
158 /* Number of files for which space is allocated in `delbuf' and `fileptr'.
159 Enlarged as necessary. */
160 int file_list_size = 12;
161 int chr IF_LINT (= 0); /* Input character. */
162 int line_length; /* Number of chars in line. */
163 int somedone; /* 0 if all files empty for this line. */
164 /* If all files are just ready to be closed, or will be on this
165 round, the string of delimiters must be preserved.
166 delbuf[0] through delbuf[file_list_size]
167 store the delimiters for closed files. */
168 char *delbuf;
169 int delims_saved; /* Number of delims saved in `delbuf'. */
170 register char *delimptr; /* Cycling pointer into `delims'. */
171 FILE **fileptr; /* Streams open to the files to process. */
172 int files_open; /* Number of files still open to process. */
173 int i; /* Loop index. */
174 int opened_stdin = 0; /* Nonzero if any fopen got fd 0. */
176 delbuf = (char *) xmalloc (file_list_size + 2);
177 fileptr = (FILE **) xmalloc ((file_list_size + 1) * sizeof (FILE *));
179 /* Attempt to open all files. This could be expanded to an infinite
180 number of files, but at the (considerable) expense of remembering
181 each file and its current offset, then opening/reading/closing. */
183 for (files_open = 0; files_open < nfiles; ++files_open)
185 if (files_open == file_list_size - 2)
187 file_list_size += 12;
188 delbuf = (char *) xrealloc (delbuf, file_list_size + 2);
189 fileptr = (FILE **) xrealloc ((char *) fileptr, (file_list_size + 1)
190 * sizeof (FILE *));
192 if (STREQ (fnamptr[files_open], "-"))
194 have_read_stdin = 1;
195 fileptr[files_open] = stdin;
197 else
199 fileptr[files_open] = fopen (fnamptr[files_open], "r");
200 if (fileptr[files_open] == NULL)
201 error (EXIT_FAILURE, errno, "%s", fnamptr[files_open]);
202 else if (fileno (fileptr[files_open]) == 0)
203 opened_stdin = 1;
207 fileptr[files_open] = ENDLIST;
209 if (opened_stdin && have_read_stdin)
210 error (EXIT_FAILURE, 0, _("standard input is closed"));
212 /* Read a line from each file and output it to stdout separated by a
213 delimiter, until we go through the loop without successfully
214 reading from any of the files. */
216 while (files_open)
218 /* Set up for the next line. */
219 somedone = 0;
220 delimptr = delims;
221 delims_saved = 0;
223 for (i = 0; fileptr[i] != ENDLIST && files_open; i++)
225 line_length = 0; /* Clear so we can easily detect EOF. */
226 if (fileptr[i] != CLOSED)
228 chr = getc (fileptr[i]);
229 if (chr != EOF && delims_saved)
231 fwrite (delbuf, sizeof (char), delims_saved, stdout);
232 delims_saved = 0;
235 while (chr != EOF)
237 line_length++;
238 if (chr == '\n')
239 break;
240 putc (chr, stdout);
241 chr = getc (fileptr[i]);
245 if (line_length == 0)
247 /* EOF, read error, or closed file.
248 If an EOF or error, close the file and mark it in the list. */
249 if (fileptr[i] != CLOSED)
251 if (ferror (fileptr[i]))
253 error (0, errno, "%s", fnamptr[i]);
254 errors = 1;
256 if (fileptr[i] == stdin)
257 clearerr (fileptr[i]); /* Also clear EOF. */
258 else if (fclose (fileptr[i]) == EOF)
260 error (0, errno, "%s", fnamptr[i]);
261 errors = 1;
264 fileptr[i] = CLOSED;
265 files_open--;
268 if (fileptr[i + 1] == ENDLIST)
270 /* End of this output line.
271 Is this the end of the whole thing? */
272 if (somedone)
274 /* No. Some files were not closed for this line. */
275 if (delims_saved)
277 fwrite (delbuf, sizeof (char), delims_saved, stdout);
278 delims_saved = 0;
280 putc ('\n', stdout);
282 continue; /* Next read of files, or exit. */
284 else
286 /* Closed file; add delimiter to `delbuf'. */
287 if (*delimptr != EMPTY_DELIM)
288 delbuf[delims_saved++] = *delimptr;
289 if (++delimptr == delim_end)
290 delimptr = delims;
293 else
295 /* Some data read. */
296 somedone++;
298 /* Except for last file, replace last newline with delim. */
299 if (fileptr[i + 1] != ENDLIST)
301 if (chr != '\n')
302 putc (chr, stdout);
303 if (*delimptr != EMPTY_DELIM)
304 putc (*delimptr, stdout);
305 if (++delimptr == delim_end)
306 delimptr = delims;
308 else
309 putc (chr, stdout);
313 return errors;
316 /* Perform serial paste on the NFILES files named in FNAMPTR.
317 Return 0 if no errors, 1 if one or more files could not be
318 opened or read. */
320 static int
321 paste_serial (int nfiles, char **fnamptr)
323 int errors = 0; /* 1 if open or read errors occur. */
324 register int charnew, charold; /* Current and previous char read. */
325 register char *delimptr; /* Current delimiter char. */
326 register FILE *fileptr; /* Open for reading current file. */
328 for (; nfiles; nfiles--, fnamptr++)
330 if (STREQ (*fnamptr, "-"))
332 have_read_stdin = 1;
333 fileptr = stdin;
335 else
337 fileptr = fopen (*fnamptr, "r");
338 if (fileptr == NULL)
340 error (0, errno, "%s", *fnamptr);
341 errors = 1;
342 continue;
346 delimptr = delims; /* Set up for delimiter string. */
348 charold = getc (fileptr);
349 if (charold != EOF)
351 /* `charold' is set up. Hit it!
352 Keep reading characters, stashing them in `charnew';
353 output `charold', converting to the appropriate delimiter
354 character if needed. After the EOF, output `charold'
355 if it's a newline; otherwise, output it and then a newline. */
357 while ((charnew = getc (fileptr)) != EOF)
359 /* Process the old character. */
360 if (charold == '\n')
362 if (*delimptr != EMPTY_DELIM)
363 putc (*delimptr, stdout);
365 if (++delimptr == delim_end)
366 delimptr = delims;
368 else
369 putc (charold, stdout);
371 charold = charnew;
374 /* Hit EOF. Process that last character. */
375 putc (charold, stdout);
378 if (charold != '\n')
379 putc ('\n', stdout);
381 if (ferror (fileptr))
383 error (0, errno, "%s", *fnamptr);
384 errors = 1;
386 if (fileptr == stdin)
387 clearerr (fileptr); /* Also clear EOF. */
388 else if (fclose (fileptr) == EOF)
390 error (0, errno, "%s", *fnamptr);
391 errors = 1;
394 return errors;
397 void
398 usage (int status)
400 if (status != 0)
401 fprintf (stderr, _("Try `%s --help' for more information.\n"),
402 program_name);
403 else
405 printf (_("\
406 Usage: %s [OPTION]... [FILE]...\n\
408 program_name);
409 printf (_("\
410 Write lines consisting of the sequentially corresponding lines from\n\
411 each FILE, separated by TABs, to standard output.\n\
412 With no FILE, or when FILE is -, read standard input.\n\
414 -d, --delimiters=LIST reuse characters from LIST instead of TABs\n\
415 -s, --serial paste one file at a time instead of in parallel\n\
416 --help display this help and exit\n\
417 --version output version information and exit\n\
419 "));
420 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
422 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
426 main (int argc, char **argv)
428 int optc, exit_status;
429 char default_delims[2], zero_delims[3];
431 program_name = argv[0];
432 setlocale (LC_ALL, "");
433 bindtextdomain (PACKAGE, LOCALEDIR);
434 textdomain (PACKAGE);
436 have_read_stdin = 0;
437 serial_merge = 0;
438 delims = default_delims;
439 strcpy (delims, "\t");
440 strcpy (zero_delims, "\\0");
442 while ((optc = getopt_long (argc, argv, "d:s", longopts, NULL)) != -1)
444 switch (optc)
446 case 0:
447 break;
449 case 'd':
450 /* Delimiter character(s). */
451 if (optarg[0] == '\0')
452 optarg = zero_delims;
453 delims = optarg;
454 break;
456 case 's':
457 serial_merge++;
458 break;
460 case_GETOPT_HELP_CHAR;
462 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
464 default:
465 usage (1);
469 if (optind == argc)
470 argv[argc++] = "-";
472 delim_end = collapse_escapes (delims);
474 if (!serial_merge)
475 exit_status = paste_parallel (argc - optind, &argv[optind]);
476 else
477 exit_status = paste_serial (argc - optind, &argv[optind]);
478 if (have_read_stdin && fclose (stdin) == EOF)
479 error (EXIT_FAILURE, errno, "-");
480 if (ferror (stdout) || fclose (stdout) == EOF)
481 error (EXIT_FAILURE, errno, _("write error"));
482 exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);