paste.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1992-2012 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* David Korn
* AT&T Bell Laboratories
*
* paste [-s] [-d delim] [file] ...
*
* paste lines from files together
*/
static const char usage[] =
"[-?\n@(#)$Id: paste (AT&T Research) 2010-06-12 $\n]"
"[+NAME?paste - merge lines of files]"
"[+DESCRIPTION?\bpaste\b concatenates the corresponding lines of a "
"given input file and writes the resulting lines to standard "
"output. By default \bpaste\b replaces the newline character of "
"every line other than the last input file with the TAB character.]"
"[+?Unless the \b-s\b option is specified, if an end-of-file is encountered "
"on one or more input files, but not all input files, \bpaste\b "
"behaves as if empty lines were read from the file(s) on which "
"end-of-file was detected.]"
"[+?Unless the \b-s\b option is specified, \bpaste\b is limited by "
"the underlying operating system on how many \afile\a operands "
"can be specified.]"
"[+?If no \afile\a operands are given or if the \afile\a is \b-\b, \bpaste\b "
"reads from standard input. The start of the file is defined as the "
"current offset.]"
"[s:serial?Paste the lines of one file at a time rather than one line "
"from each file. In this case if the \b-d\b option is "
"specified the delimiter will be reset to the first in the "
"list at the beginning of each file.]"
"[d:delimiters]:[list?\alist\a specifies a list of delimiters. These "
"delimiters are used circularly instead of TAB to replace "
"the newline character of the input lines. Unless the \b-s\b "
"option is specified, the delimiter will be reset to the first "
"element of \alist\a each time a line is processed from each file. "
"The delimiter characters corresponding to \alist\a will be found "
"by treating \alist\a as an ANSI-C string, except that the \b\\0\b "
"sequence will insert the empty string instead of the null character.]"
"\n"
"\n[file ...]\n"
"\n"
"[+EXIT STATUS?]{"
"[+0?All files processed successfully.]"
"[+>0?An error occurred.]"
"}"
"[+SEE ALSO?\bcut\b(1), \bcat\b(1), \bjoin\b(1)]"
;
#include <cmd.h>
typedef struct Delim_s
{
const char* chr;
} Delim_t;
/*
* paste the lines of the <nstreams> defined in <streams> and put results
* to <out>
*/
static int paste(int nstream,Sfio_t* streams[],Sfio_t *out, register const char *delim, int dsiz, int dlen, Delim_t* mp)
{
register const char *cp;
register int d, n, i, z, more=1;
do
{
d = (dlen>0?0:-1);
{
{
{
if(n==0)
more = 1;
else if(!more) /* first stream with output */
{
if(dsiz == 1)
else if(dlen>0)
{
if(d)
{
if(mp)
for (i = z = 0; i < d; i++)
else
z = d;
}
}
more = n+1;
}
return(-1);
}
else
streams[n] = 0;
}
{
register int c;
if(d >= dlen)
d = 0;
if(mp)
else if(c=delim[d])
d++;
}
}
} while(more);
return(0);
}
/*
* Handles paste -s, for file <in> to file <out> using delimiters <delim>
*/
static int spaste(Sfio_t *in,register Sfio_t* out,register const char *delim,int dsiz,int dlen,Delim_t* mp)
{
register const char *cp;
register int d=0;
return(-1);
{
if(dlen)
{
register int c;
if(d >= dlen)
d = 0;
if(mp)
else if(c=delim[d])
d++;
}
return(-1);
}
return(0);
}
int
{
register int n, sflag=0;
char *ep;
char defdelim[2];
delim = 0;
for (;;)
{
{
case 'd':
continue;
case 's':
sflag++;
continue;
case ':':
break;
case '?':
break;
}
break;
}
if(error_info.errors)
{
delim[0] = '\t';
delim[1] = 0;
}
mp = 0;
if (mbwide())
{
dlen = 0;
{
dlen++;
}
{
{
}
dlen = 0;
{
dlen++;
}
}
}
{
argv++;
}
else
n = 1;
if(!sflag)
{
n = 0;
}
do
{
{
}
else if(!sflag)
if(!sflag)
{
while(--n>=0)
}
if (mp)
return(error_info.errors);
}