1992-11-08 02:50:43 +00:00
|
|
|
/* paste - merge lines of files
|
2020-01-01 14:16:56 +00:00
|
|
|
Copyright (C) 1997-2020 Free Software Foundation, Inc.
|
2004-01-21 23:37:54 +00:00
|
|
|
Copyright (C) 1984 David M. Ihnat
|
2000-01-21 15:08:08 +00:00
|
|
|
|
2007-07-23 14:35:58 +02:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
2000-01-21 15:08:08 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
2007-07-23 14:35:58 +02:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
2000-01-21 15:08:08 +00:00
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2017-09-19 01:13:23 -07:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2000-01-21 15:08:08 +00:00
|
|
|
|
|
|
|
|
/* Written by David Ihnat. */
|
|
|
|
|
|
|
|
|
|
/* The list of valid escape sequences has been expanded over the Unix
|
1992-11-08 02:50:43 +00:00
|
|
|
version, to include \b, \f, \r, and \v.
|
1993-10-21 17:19:34 +00:00
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
POSIX changes, bug fixes, long-named options, and cleanup
|
1992-11-19 21:03:49 +00:00
|
|
|
by David MacKenzie <djm@gnu.ai.mit.edu>.
|
1993-10-21 17:19:34 +00:00
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
Options:
|
|
|
|
|
--serial
|
|
|
|
|
-s Paste one file at a time rather than
|
2009-08-22 18:56:06 +02:00
|
|
|
one line from each file.
|
1992-11-08 02:50:43 +00:00
|
|
|
--delimiters=delim-list
|
|
|
|
|
-d delim-list Consecutively use the characters in
|
2009-08-22 18:56:06 +02:00
|
|
|
DELIM-LIST instead of tab to separate
|
|
|
|
|
merged lines. When DELIM-LIST is exhausted,
|
|
|
|
|
start again at its beginning.
|
2012-01-08 15:08:30 +01:00
|
|
|
A FILE of '-' means standard input.
|
1992-11-08 02:50:43 +00:00
|
|
|
If no FILEs are given, standard input is used. */
|
|
|
|
|
|
1993-10-21 22:08:53 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include "system.h"
|
2016-10-15 23:10:35 +01:00
|
|
|
#include "die.h"
|
1994-12-16 05:42:47 +00:00
|
|
|
#include "error.h"
|
2010-07-20 18:51:01 +01:00
|
|
|
#include "fadvise.h"
|
1992-11-08 02:50:43 +00:00
|
|
|
|
2012-01-08 15:08:30 +01:00
|
|
|
/* The official name of this program (e.g., no 'g' prefix). */
|
1999-04-03 05:01:48 +00:00
|
|
|
#define PROGRAM_NAME "paste"
|
|
|
|
|
|
2008-05-19 16:26:25 +02:00
|
|
|
#define AUTHORS \
|
|
|
|
|
proper_name ("David M. Ihnat"), \
|
|
|
|
|
proper_name ("David MacKenzie")
|
1999-04-03 05:22:05 +00:00
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
/* Indicates that no delimiter should be added in the current position. */
|
|
|
|
|
#define EMPTY_DELIM '\0'
|
|
|
|
|
|
|
|
|
|
/* If nonzero, we have read standard input at some point. */
|
2004-01-09 16:05:36 +00:00
|
|
|
static bool have_read_stdin;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
/* If nonzero, merge subsequent lines of each file rather than
|
|
|
|
|
corresponding lines from each file in parallel. */
|
2004-01-09 16:05:36 +00:00
|
|
|
static bool serial_merge;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
2014-05-30 17:44:32 +01:00
|
|
|
/* The delimiters between lines of input files (used cyclically). */
|
1992-11-08 20:19:58 +00:00
|
|
|
static char *delims;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
2012-01-08 15:08:30 +01:00
|
|
|
/* A pointer to the character after the end of 'delims'. */
|
2004-01-09 16:05:36 +00:00
|
|
|
static char const *delim_end;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
2016-01-08 15:57:06 +00:00
|
|
|
static unsigned char line_delim = '\n';
|
|
|
|
|
|
remove redundant const directives
In 1463824d8e7f72c31f1d803d7cfe2b608ccafc5c, I added some
missing "const" directives, as well as some new, redundant ones.
This removes the redundant ones. Pointed out by Eric Blake.
* base64.c, cat.c, chcon.c, chgrp.c, chmod.c, chown.c, comm.c:
* cp.c, csplit.c, cut.c, date.c, dd.c, df.c, dircolors.c, du.c:
* env.c, expand.c, fmt.c, fold.c, groups.c, head.c, id.c:
* install.c, join.c, kill.c, ln.c, ls.c, md5sum.c, mkdir.c:
* mkfifo.c, mknod.c, mktemp.c, mv.c, nice.c, nl.c, od.c:
* paste.c, pathchk.c, pinky.c, pr.c, ptx.c, readlink.c, rm.c:
* rmdir.c, runcon.c, seq.c, shred.c, shuf.c, sort.c, split.c:
* stat.c, stty.c, su.c, sum.c, tac.c, tail.c, tee.c, timeout.c:
* touch.c, tr.c, truncate.c, tty.c, uname.c, unexpand.c, uniq.c:
* wc.c, who.c: Remove redundant const directives.
* maint.mk (sc_const_long_option): Don't require redundant "const".
2008-06-16 14:55:06 +02:00
|
|
|
static struct option const longopts[] =
|
1992-11-08 02:50:43 +00:00
|
|
|
{
|
2005-03-28 18:05:53 +00:00
|
|
|
{"serial", no_argument, NULL, 's'},
|
|
|
|
|
{"delimiters", required_argument, NULL, 'd'},
|
2016-01-08 15:57:06 +00:00
|
|
|
{"zero-terminated", no_argument, NULL, 'z'},
|
1999-04-04 15:44:26 +00:00
|
|
|
{GETOPT_HELP_OPTION_DECL},
|
|
|
|
|
{GETOPT_VERSION_OPTION_DECL},
|
2005-03-28 18:05:53 +00:00
|
|
|
{NULL, 0, NULL, 0}
|
1992-11-08 02:50:43 +00:00
|
|
|
};
|
|
|
|
|
|
2004-01-09 16:05:36 +00:00
|
|
|
/* Set globals delims and delim_end. Copy STRPTR to DELIMS, converting
|
|
|
|
|
backslash representations of special characters in STRPTR to their actual
|
|
|
|
|
values. The set of possible backslash characters has been expanded beyond
|
2008-03-27 12:18:25 +01:00
|
|
|
that recognized by the Unix version.
|
|
|
|
|
Return 0 upon success.
|
|
|
|
|
If the string ends in an odd number of backslashes, ignore the
|
|
|
|
|
final backslash and return nonzero. */
|
1992-11-08 02:50:43 +00:00
|
|
|
|
2008-03-27 12:18:25 +01:00
|
|
|
static int
|
2004-01-09 16:05:36 +00:00
|
|
|
collapse_escapes (char const *strptr)
|
1992-11-08 02:50:43 +00:00
|
|
|
{
|
2004-01-09 16:05:36 +00:00
|
|
|
char *strout = xstrdup (strptr);
|
2008-03-27 12:18:25 +01:00
|
|
|
bool backslash_at_end = false;
|
|
|
|
|
|
2004-01-09 16:05:36 +00:00
|
|
|
delims = strout;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
while (*strptr)
|
|
|
|
|
{
|
|
|
|
|
if (*strptr != '\\') /* Is it an escape character? */
|
2009-08-22 18:56:06 +02:00
|
|
|
*strout++ = *strptr++; /* No, just transfer it. */
|
1992-11-08 02:50:43 +00:00
|
|
|
else
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
switch (*++strptr)
|
|
|
|
|
{
|
|
|
|
|
case '0':
|
|
|
|
|
*strout++ = EMPTY_DELIM;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
|
*strout++ = '\b';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
|
*strout++ = '\f';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
|
*strout++ = '\n';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
|
*strout++ = '\r';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
|
*strout++ = '\t';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
|
*strout++ = '\v';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
|
*strout++ = '\\';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '\0':
|
|
|
|
|
backslash_at_end = true;
|
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
*strout++ = *strptr;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
strptr++;
|
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
}
|
2008-03-27 12:18:25 +01:00
|
|
|
|
2011-07-25 18:39:28 +02:00
|
|
|
done:
|
2008-03-27 12:18:25 +01:00
|
|
|
|
2004-01-09 16:05:36 +00:00
|
|
|
delim_end = strout;
|
2008-03-27 12:18:25 +01:00
|
|
|
return backslash_at_end ? 1 : 0;
|
1992-11-08 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 01:35:33 +00:00
|
|
|
/* Report a write error and exit. */
|
|
|
|
|
|
|
|
|
|
static void write_error (void) ATTRIBUTE_NORETURN;
|
|
|
|
|
static void
|
|
|
|
|
write_error (void)
|
|
|
|
|
{
|
2016-10-15 23:10:35 +01:00
|
|
|
die (EXIT_FAILURE, errno, _("write error"));
|
2004-09-03 01:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Output a single byte, reporting any write errors. */
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
|
xputchar (char c)
|
|
|
|
|
{
|
|
|
|
|
if (putchar (c) < 0)
|
|
|
|
|
write_error ();
|
|
|
|
|
}
|
|
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
/* Perform column paste on the NFILES files named in FNAMPTR.
|
2004-08-03 15:30:08 +00:00
|
|
|
Return true if successful, false if one or more files could not be
|
1992-11-08 02:50:43 +00:00
|
|
|
opened or read. */
|
|
|
|
|
|
2004-08-03 15:30:08 +00:00
|
|
|
static bool
|
2004-01-09 16:05:36 +00:00
|
|
|
paste_parallel (size_t nfiles, char **fnamptr)
|
1992-11-08 02:50:43 +00:00
|
|
|
{
|
2004-08-03 15:30:08 +00:00
|
|
|
bool ok = true;
|
1992-11-08 02:50:43 +00:00
|
|
|
/* If all files are just ready to be closed, or will be on this
|
|
|
|
|
round, the string of delimiters must be preserved.
|
2004-01-09 16:05:36 +00:00
|
|
|
delbuf[0] through delbuf[nfiles]
|
1992-11-08 02:50:43 +00:00
|
|
|
store the delimiters for closed files. */
|
2004-09-02 23:56:42 +00:00
|
|
|
char *delbuf = xmalloc (nfiles + 2);
|
|
|
|
|
|
2004-09-03 01:23:15 +00:00
|
|
|
/* Streams open to the files to process; NULL if the corresponding
|
|
|
|
|
stream is closed. */
|
2004-09-02 23:56:42 +00:00
|
|
|
FILE **fileptr = xnmalloc (nfiles + 1, sizeof *fileptr);
|
|
|
|
|
|
|
|
|
|
/* Number of files still open to process. */
|
|
|
|
|
size_t files_open;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
2004-09-02 23:56:42 +00:00
|
|
|
/* True if any fopen got fd == STDIN_FILENO. */
|
|
|
|
|
bool opened_stdin = false;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
/* Attempt to open all files. This could be expanded to an infinite
|
|
|
|
|
number of files, but at the (considerable) expense of remembering
|
|
|
|
|
each file and its current offset, then opening/reading/closing. */
|
|
|
|
|
|
|
|
|
|
for (files_open = 0; files_open < nfiles; ++files_open)
|
|
|
|
|
{
|
1998-04-12 09:27:45 +00:00
|
|
|
if (STREQ (fnamptr[files_open], "-"))
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
have_read_stdin = true;
|
|
|
|
|
fileptr[files_open] = stdin;
|
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
else
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
fileptr[files_open] = fopen (fnamptr[files_open], "r");
|
|
|
|
|
if (fileptr[files_open] == NULL)
|
2016-10-15 23:10:35 +01:00
|
|
|
die (EXIT_FAILURE, errno, "%s", quotef (fnamptr[files_open]));
|
2009-08-22 18:56:06 +02:00
|
|
|
else if (fileno (fileptr[files_open]) == STDIN_FILENO)
|
|
|
|
|
opened_stdin = true;
|
2010-07-20 18:51:01 +01:00
|
|
|
fadvise (fileptr[files_open], FADVISE_SEQUENTIAL);
|
2009-08-22 18:56:06 +02:00
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (opened_stdin && have_read_stdin)
|
2016-10-15 23:10:35 +01:00
|
|
|
die (EXIT_FAILURE, 0, _("standard input is closed"));
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
/* Read a line from each file and output it to stdout separated by a
|
|
|
|
|
delimiter, until we go through the loop without successfully
|
|
|
|
|
reading from any of the files. */
|
|
|
|
|
|
|
|
|
|
while (files_open)
|
|
|
|
|
{
|
|
|
|
|
/* Set up for the next line. */
|
2004-01-09 16:05:36 +00:00
|
|
|
bool somedone = false;
|
|
|
|
|
char const *delimptr = delims;
|
2012-01-08 15:08:30 +01:00
|
|
|
size_t delims_saved = 0; /* Number of delims saved in 'delbuf'. */
|
1992-11-08 02:50:43 +00:00
|
|
|
|
2017-06-17 14:54:23 -07:00
|
|
|
for (size_t i = 0; i < nfiles && files_open; i++)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
2010-05-15 19:36:56 +02:00
|
|
|
int chr IF_LINT ( = 0); /* Input character. */
|
|
|
|
|
int err IF_LINT ( = 0); /* Input errno value. */
|
2014-11-24 09:14:44 +00:00
|
|
|
bool sometodo = false; /* Input chars to process. */
|
2009-08-22 18:56:06 +02:00
|
|
|
|
|
|
|
|
if (fileptr[i])
|
|
|
|
|
{
|
|
|
|
|
chr = getc (fileptr[i]);
|
|
|
|
|
err = errno;
|
|
|
|
|
if (chr != EOF && delims_saved)
|
|
|
|
|
{
|
|
|
|
|
if (fwrite (delbuf, 1, delims_saved, stdout) != delims_saved)
|
|
|
|
|
write_error ();
|
|
|
|
|
delims_saved = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (chr != EOF)
|
|
|
|
|
{
|
2014-11-24 09:14:44 +00:00
|
|
|
sometodo = true;
|
2016-01-08 15:57:06 +00:00
|
|
|
if (chr == line_delim)
|
2009-08-22 18:56:06 +02:00
|
|
|
break;
|
|
|
|
|
xputchar (chr);
|
|
|
|
|
chr = getc (fileptr[i]);
|
|
|
|
|
err = errno;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-24 09:14:44 +00:00
|
|
|
if (! sometodo)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
/* EOF, read error, or closed file.
|
|
|
|
|
If an EOF or error, close the file. */
|
|
|
|
|
if (fileptr[i])
|
|
|
|
|
{
|
|
|
|
|
if (ferror (fileptr[i]))
|
|
|
|
|
{
|
2015-11-01 18:53:26 +00:00
|
|
|
error (0, err, "%s", quotef (fnamptr[i]));
|
2009-08-22 18:56:06 +02:00
|
|
|
ok = false;
|
|
|
|
|
}
|
|
|
|
|
if (fileptr[i] == stdin)
|
|
|
|
|
clearerr (fileptr[i]); /* Also clear EOF. */
|
|
|
|
|
else if (fclose (fileptr[i]) == EOF)
|
|
|
|
|
{
|
2015-11-01 18:53:26 +00:00
|
|
|
error (0, errno, "%s", quotef (fnamptr[i]));
|
2009-08-22 18:56:06 +02:00
|
|
|
ok = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileptr[i] = NULL;
|
|
|
|
|
files_open--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i + 1 == nfiles)
|
|
|
|
|
{
|
|
|
|
|
/* End of this output line.
|
|
|
|
|
Is this the end of the whole thing? */
|
|
|
|
|
if (somedone)
|
|
|
|
|
{
|
|
|
|
|
/* No. Some files were not closed for this line. */
|
|
|
|
|
if (delims_saved)
|
|
|
|
|
{
|
|
|
|
|
if (fwrite (delbuf, 1, delims_saved, stdout)
|
|
|
|
|
!= delims_saved)
|
|
|
|
|
write_error ();
|
|
|
|
|
delims_saved = 0;
|
|
|
|
|
}
|
2016-01-08 15:57:06 +00:00
|
|
|
xputchar (line_delim);
|
2009-08-22 18:56:06 +02:00
|
|
|
}
|
|
|
|
|
continue; /* Next read of files, or exit. */
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-01-08 15:08:30 +01:00
|
|
|
/* Closed file; add delimiter to 'delbuf'. */
|
2009-08-22 18:56:06 +02:00
|
|
|
if (*delimptr != EMPTY_DELIM)
|
|
|
|
|
delbuf[delims_saved++] = *delimptr;
|
|
|
|
|
if (++delimptr == delim_end)
|
|
|
|
|
delimptr = delims;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Some data read. */
|
|
|
|
|
somedone = true;
|
|
|
|
|
|
|
|
|
|
/* Except for last file, replace last newline with delim. */
|
|
|
|
|
if (i + 1 != nfiles)
|
|
|
|
|
{
|
2016-01-08 15:57:06 +00:00
|
|
|
if (chr != line_delim && chr != EOF)
|
2009-08-22 18:56:06 +02:00
|
|
|
xputchar (chr);
|
|
|
|
|
if (*delimptr != EMPTY_DELIM)
|
|
|
|
|
xputchar (*delimptr);
|
|
|
|
|
if (++delimptr == delim_end)
|
|
|
|
|
delimptr = delims;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* If the last line of the last file lacks a newline,
|
|
|
|
|
print one anyhow. POSIX requires this. */
|
2016-01-08 15:57:06 +00:00
|
|
|
char c = (chr == EOF ? line_delim : chr);
|
2009-08-22 18:56:06 +02:00
|
|
|
xputchar (c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
}
|
2004-01-09 16:05:36 +00:00
|
|
|
free (fileptr);
|
|
|
|
|
free (delbuf);
|
2004-08-03 15:30:08 +00:00
|
|
|
return ok;
|
1992-11-08 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Perform serial paste on the NFILES files named in FNAMPTR.
|
2004-08-03 15:30:08 +00:00
|
|
|
Return true if no errors, false if one or more files could not be
|
1992-11-08 02:50:43 +00:00
|
|
|
opened or read. */
|
|
|
|
|
|
2004-08-03 15:30:08 +00:00
|
|
|
static bool
|
2004-01-09 16:05:36 +00:00
|
|
|
paste_serial (size_t nfiles, char **fnamptr)
|
1992-11-08 02:50:43 +00:00
|
|
|
{
|
2004-08-03 15:30:08 +00:00
|
|
|
bool ok = true; /* false if open or read errors occur. */
|
2004-01-09 16:05:36 +00:00
|
|
|
int charnew, charold; /* Current and previous char read. */
|
|
|
|
|
char const *delimptr; /* Current delimiter char. */
|
|
|
|
|
FILE *fileptr; /* Open for reading current file. */
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
for (; nfiles; nfiles--, fnamptr++)
|
|
|
|
|
{
|
2003-09-23 17:47:59 +00:00
|
|
|
int saved_errno;
|
2005-04-11 20:09:22 +00:00
|
|
|
bool is_stdin = STREQ (*fnamptr, "-");
|
|
|
|
|
if (is_stdin)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
have_read_stdin = true;
|
|
|
|
|
fileptr = stdin;
|
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
else
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
fileptr = fopen (*fnamptr, "r");
|
|
|
|
|
if (fileptr == NULL)
|
|
|
|
|
{
|
2015-11-01 18:53:26 +00:00
|
|
|
error (0, errno, "%s", quotef (*fnamptr));
|
2009-08-22 18:56:06 +02:00
|
|
|
ok = false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2010-07-20 18:51:01 +01:00
|
|
|
fadvise (fileptr, FADVISE_SEQUENTIAL);
|
2009-08-22 18:56:06 +02:00
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
delimptr = delims; /* Set up for delimiter string. */
|
|
|
|
|
|
1998-06-29 15:57:45 +00:00
|
|
|
charold = getc (fileptr);
|
2003-09-23 17:47:59 +00:00
|
|
|
saved_errno = errno;
|
1992-11-08 02:50:43 +00:00
|
|
|
if (charold != EOF)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
2012-01-08 15:08:30 +01:00
|
|
|
/* 'charold' is set up. Hit it!
|
|
|
|
|
Keep reading characters, stashing them in 'charnew';
|
|
|
|
|
output 'charold', converting to the appropriate delimiter
|
|
|
|
|
character if needed. After the EOF, output 'charold'
|
2009-08-22 18:56:06 +02:00
|
|
|
if it's a newline; otherwise, output it and then a newline. */
|
|
|
|
|
|
|
|
|
|
while ((charnew = getc (fileptr)) != EOF)
|
|
|
|
|
{
|
|
|
|
|
/* Process the old character. */
|
2016-01-08 15:57:06 +00:00
|
|
|
if (charold == line_delim)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
if (*delimptr != EMPTY_DELIM)
|
|
|
|
|
xputchar (*delimptr);
|
|
|
|
|
|
|
|
|
|
if (++delimptr == delim_end)
|
|
|
|
|
delimptr = delims;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
xputchar (charold);
|
|
|
|
|
|
|
|
|
|
charold = charnew;
|
|
|
|
|
}
|
|
|
|
|
saved_errno = errno;
|
|
|
|
|
|
|
|
|
|
/* Hit EOF. Process that last character. */
|
|
|
|
|
xputchar (charold);
|
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
2016-01-08 15:57:06 +00:00
|
|
|
if (charold != line_delim)
|
|
|
|
|
xputchar (line_delim);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
1998-06-29 15:57:45 +00:00
|
|
|
if (ferror (fileptr))
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
2015-11-01 18:53:26 +00:00
|
|
|
error (0, saved_errno, "%s", quotef (*fnamptr));
|
2009-08-22 18:56:06 +02:00
|
|
|
ok = false;
|
|
|
|
|
}
|
2005-04-11 20:09:22 +00:00
|
|
|
if (is_stdin)
|
2009-08-22 18:56:06 +02:00
|
|
|
clearerr (fileptr); /* Also clear EOF. */
|
1998-06-29 15:57:45 +00:00
|
|
|
else if (fclose (fileptr) == EOF)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
2015-11-01 18:53:26 +00:00
|
|
|
error (0, errno, "%s", quotef (*fnamptr));
|
2009-08-22 18:56:06 +02:00
|
|
|
ok = false;
|
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
}
|
2004-08-03 15:30:08 +00:00
|
|
|
return ok;
|
1992-11-08 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
1999-01-14 18:25:16 +00:00
|
|
|
void
|
1995-10-29 20:05:29 +00:00
|
|
|
usage (int status)
|
1992-11-08 02:50:43 +00:00
|
|
|
{
|
2004-01-21 23:37:54 +00:00
|
|
|
if (status != EXIT_SUCCESS)
|
2012-01-07 16:54:26 +01:00
|
|
|
emit_try_help ();
|
1993-10-23 15:37:19 +00:00
|
|
|
else
|
|
|
|
|
{
|
1995-08-07 14:57:29 +00:00
|
|
|
printf (_("\
|
1993-10-23 15:37:19 +00:00
|
|
|
Usage: %s [OPTION]... [FILE]...\n\
|
1995-08-07 14:57:29 +00:00
|
|
|
"),
|
2009-08-22 18:56:06 +02:00
|
|
|
program_name);
|
2001-11-23 19:58:23 +00:00
|
|
|
fputs (_("\
|
1995-05-13 18:34:54 +00:00
|
|
|
Write lines consisting of the sequentially corresponding lines from\n\
|
|
|
|
|
each FILE, separated by TABs, to standard output.\n\
|
2001-11-23 19:58:23 +00:00
|
|
|
"), stdout);
|
maint: define usage note about mandatory args centrally
Each program with at least one long option which is marked as
'required_argument' and which has also a short option for that
option, should print a note about mandatory arguments.
Define that well-known note centrally and use it rather than
literal printf/fputs, and add it where it was missing.
* src/system.h (emit_mandatory_arg_note): Add new function.
* src/cp.c (usage): Use it rather than literal printf/fputs.
* src/csplit.c, src/cut.c, src/date.c, src/df.c, src/du.c:
* src/expand.c, src/fmt.c, src/fold.c, src/head.c, src/install.c:
* src/kill.c, src/ln.c, src/ls.c, src/mkdir.c, src/mkfifo.c:
* src/mknod.c, src/mv.c, src/nl.c, src/od.c, src/paste.c:
* src/pr.c, src/ptx.c, src/shred.c, src/shuf.c, src/sort.c:
* src/split.c, src/stdbuf.c, src/tac.c, src/tail.c, src/timeout.c:
* src/touch.c, src/truncate.c, src/unexpand.c, src/uniq.c:
Likewise.
* src/base64.c (usage): Add call of the above new function
because at least one long option has a required argument.
* src/basename.c, src/chcon.c, src/date.c, src/env.c:
* src/nice.c, src/runcon.c, src/seq.c, src/stat.c, src/stty.c:
Likewise.
2013-01-23 01:03:38 +01:00
|
|
|
|
2015-04-30 14:02:46 +01:00
|
|
|
emit_stdin_note ();
|
maint: define usage note about mandatory args centrally
Each program with at least one long option which is marked as
'required_argument' and which has also a short option for that
option, should print a note about mandatory arguments.
Define that well-known note centrally and use it rather than
literal printf/fputs, and add it where it was missing.
* src/system.h (emit_mandatory_arg_note): Add new function.
* src/cp.c (usage): Use it rather than literal printf/fputs.
* src/csplit.c, src/cut.c, src/date.c, src/df.c, src/du.c:
* src/expand.c, src/fmt.c, src/fold.c, src/head.c, src/install.c:
* src/kill.c, src/ln.c, src/ls.c, src/mkdir.c, src/mkfifo.c:
* src/mknod.c, src/mv.c, src/nl.c, src/od.c, src/paste.c:
* src/pr.c, src/ptx.c, src/shred.c, src/shuf.c, src/sort.c:
* src/split.c, src/stdbuf.c, src/tac.c, src/tail.c, src/timeout.c:
* src/touch.c, src/truncate.c, src/unexpand.c, src/uniq.c:
Likewise.
* src/base64.c (usage): Add call of the above new function
because at least one long option has a required argument.
* src/basename.c, src/chcon.c, src/date.c, src/env.c:
* src/nice.c, src/runcon.c, src/seq.c, src/stat.c, src/stty.c:
Likewise.
2013-01-23 01:03:38 +01:00
|
|
|
emit_mandatory_arg_note ();
|
|
|
|
|
|
2001-11-23 19:58:23 +00:00
|
|
|
fputs (_("\
|
1993-10-24 20:00:39 +00:00
|
|
|
-d, --delimiters=LIST reuse characters from LIST instead of TABs\n\
|
1993-10-23 15:37:19 +00:00
|
|
|
-s, --serial paste one file at a time instead of in parallel\n\
|
2016-01-08 15:57:06 +00:00
|
|
|
"), stdout);
|
|
|
|
|
fputs (_("\
|
|
|
|
|
-z, --zero-terminated line delimiter is NUL, not newline\n\
|
2001-11-23 19:58:23 +00:00
|
|
|
"), stdout);
|
2001-12-01 17:41:25 +00:00
|
|
|
fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|
|
|
|
fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
2001-11-23 19:58:23 +00:00
|
|
|
/* FIXME: add a couple of examples. */
|
2014-09-18 14:50:47 +01:00
|
|
|
emit_ancillary_info (PROGRAM_NAME);
|
1993-10-23 15:37:19 +00:00
|
|
|
}
|
2004-01-21 23:37:54 +00:00
|
|
|
exit (status);
|
1992-11-08 02:50:43 +00:00
|
|
|
}
|
1995-10-29 20:05:29 +00:00
|
|
|
|
1996-03-21 22:41:04 +00:00
|
|
|
int
|
1995-10-29 20:05:29 +00:00
|
|
|
main (int argc, char **argv)
|
|
|
|
|
{
|
2004-08-03 15:30:08 +00:00
|
|
|
int optc;
|
2004-01-09 16:05:36 +00:00
|
|
|
char const *delim_arg = "\t";
|
1995-10-29 20:05:29 +00:00
|
|
|
|
2003-06-17 18:13:23 +00:00
|
|
|
initialize_main (&argc, &argv);
|
2008-06-03 08:34:09 +02:00
|
|
|
set_program_name (argv[0]);
|
1996-03-09 20:19:13 +00:00
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
|
textdomain (PACKAGE);
|
|
|
|
|
|
2000-05-20 22:06:38 +00:00
|
|
|
atexit (close_stdout);
|
|
|
|
|
|
2004-01-09 16:05:36 +00:00
|
|
|
have_read_stdin = false;
|
|
|
|
|
serial_merge = false;
|
1995-10-29 20:05:29 +00:00
|
|
|
|
2016-01-08 15:57:06 +00:00
|
|
|
while ((optc = getopt_long (argc, argv, "d:sz", longopts, NULL)) != -1)
|
1995-10-29 20:05:29 +00:00
|
|
|
{
|
|
|
|
|
switch (optc)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
case 'd':
|
|
|
|
|
/* Delimiter character(s). */
|
|
|
|
|
delim_arg = (optarg[0] == '\0' ? "\\0" : optarg);
|
|
|
|
|
break;
|
1995-10-29 20:05:29 +00:00
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
case 's':
|
|
|
|
|
serial_merge = true;
|
|
|
|
|
break;
|
1995-10-29 20:05:29 +00:00
|
|
|
|
2016-01-08 15:57:06 +00:00
|
|
|
case 'z':
|
|
|
|
|
line_delim = '\0';
|
|
|
|
|
break;
|
|
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
case_GETOPT_HELP_CHAR;
|
1999-04-04 15:44:26 +00:00
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
|
1999-04-04 15:44:26 +00:00
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
default:
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
}
|
1995-10-29 20:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
2016-06-23 20:16:45 +02:00
|
|
|
int nfiles = argc - optind;
|
|
|
|
|
if (nfiles == 0)
|
|
|
|
|
{
|
|
|
|
|
argv[optind] = bad_cast ("-");
|
|
|
|
|
nfiles++;
|
|
|
|
|
}
|
1995-10-29 20:05:29 +00:00
|
|
|
|
2008-03-27 12:18:25 +01:00
|
|
|
if (collapse_escapes (delim_arg))
|
|
|
|
|
{
|
2015-11-04 01:14:33 +00:00
|
|
|
/* Don't use the quote() quoting style, because that would double the
|
2009-08-22 18:56:06 +02:00
|
|
|
number of displayed backslashes, making the diagnostic look bogus. */
|
2016-10-15 23:10:35 +01:00
|
|
|
die (EXIT_FAILURE, 0,
|
|
|
|
|
_("delimiter list ends with an unescaped backslash: %s"),
|
|
|
|
|
quotearg_n_style_colon (0, c_maybe_quoting_style, delim_arg));
|
2008-03-27 12:18:25 +01:00
|
|
|
}
|
1995-10-29 20:05:29 +00:00
|
|
|
|
2016-06-23 20:16:45 +02:00
|
|
|
bool ok = ((serial_merge ? paste_serial : paste_parallel)
|
|
|
|
|
(nfiles, &argv[optind]));
|
2004-01-09 16:05:36 +00:00
|
|
|
|
|
|
|
|
free (delims);
|
|
|
|
|
|
1998-06-29 15:57:45 +00:00
|
|
|
if (have_read_stdin && fclose (stdin) == EOF)
|
2016-10-15 23:10:35 +01:00
|
|
|
die (EXIT_FAILURE, errno, "-");
|
maint: prefer 'return status;' to 'exit (status);' in 'main'
* build-aux/gen-single-binary.sh: Don't use ATTRIBUTE_NORETURN
for main functions.
* src/base64.c, src/basename.c, src/cat.c, src/chcon.c, src/chgrp.c:
* src/chmod.c, src/chown.c, src/chroot.c, src/cksum.c, src/comm.c:
* src/cp.c, src/csplit.c, src/cut.c, src/date.c, src/dd.c, src/df.c:
* src/dircolors.c, src/dirname.c, src/du.c, src/echo.c, src/env.c:
* src/expand.c, src/expr.c, src/factor.c, src/fmt.c, src/fold.c:
* src/getlimits.c, src/groups.c, src/head.c, src/hostid.c:
* src/hostname.c, src/id.c, src/install.c, src/join.c, src/kill.c:
* src/link.c, src/ln.c, src/logname.c, src/ls.c, src/make-prime-list.c:
* src/md5sum.c, src/mkdir.c, src/mkfifo.c, src/mknod.c, src/mktemp.c:
* src/mv.c, src/nice.c, src/nl.c, src/nohup.c, src/nproc.c:
* src/numfmt.c, src/od.c, src/paste.c, src/pathchk.c, src/pinky.c:
* src/pr.c, src/printenv.c, src/printf.c, src/ptx.c, src/pwd.c:
* src/readlink.c, src/realpath.c, src/rm.c, src/rmdir.c, src/runcon.c:
* src/seq.c, src/shred.c, src/shuf.c, src/sleep.c, src/sort.c:
* src/split.c, src/stat.c, src/stdbuf.c, src/stty.c, src/sum.c:
* src/sync.c, src/tac.c, src/tail.c, src/tee.c, src/timeout.c:
* src/touch.c, src/tr.c, src/true.c, src/truncate.c, src/tsort.c:
* src/tty.c, src/uname.c, src/unexpand.c, src/uniq.c, src/unlink.c:
* src/uptime.c, src/users.c, src/wc.c, src/who.c, src/whoami.c:
In 'main' functions, Prefer 'return status;' to 'exit (status);'.
* src/coreutils-arch.c (_single_binary_main_uname)
(_single_binary_main_arch):
* src/coreutils-dir.c, src/coreutils-vdir.c (_single_binary_main_ls)
(_single_binary_main_dir, _single_binary_main_vdir):
Omit ATTRIBUTE_NORETURN. Return a value.
* src/coreutils.c (SINGLE_BINARY_PROGRAM): Omit ATTRIBUTE_NORETURN.
(launch_program): Now static.
* src/dd.c (finish_up): New function.
(quit, main): Use it.
* src/getlimits.c (main): Return a proper exit status.
* src/test.c (test_main_return): New macro.
(main): Use it.
* src/logname.c, src/nohup.c, src/whoami.c:
Use 'error' to simplify exit status in 'main' function.
* src/yes.c (main): Use 'return' rather than 'error' to exit,
so that GCC doesn't suggest ATTRIBUTE_NORETURN.
2014-09-08 16:31:14 -07:00
|
|
|
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
|
1995-10-29 20:05:29 +00:00
|
|
|
}
|