/* * CDE - Common Desktop Environment * * Copyright (c) 1993-2012, The Open Group. All rights reserved. * * These libraries and programs are free software; you can * redistribute them and/or modify them under the terms of the GNU * Lesser General Public License as published by the Free Software * Foundation; either version 2 of the License, or (at your option) * any later version. * * These libraries and programs are distributed in the hope that * they will be useful, but WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public * License along with these libraries and programs; if not, write * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth * Floor, Boston, MA 02110-1301 USA */ /* $XConsortium: paste.c /main/3 1995/11/01 19:07:09 rswiston $ */ /*************************************************************** * * * AT&T - PROPRIETARY * * * * THIS IS PROPRIETARY SOURCE CODE LICENSED BY * * AT&T CORP. * * * * Copyright (c) 1995 AT&T Corp. * * All Rights Reserved * * * * This software is licensed by AT&T Corp. * * under the terms and conditions of the license in * * http://www.research.att.com/orgs/ssr/book/reuse * * * * This software was created by the * * Software Engineering Research Department * * AT&T Bell Laboratories * * * * For further information contact * * gsf@research.att.com * * * ***************************************************************/ /* : : generated by proto : : */ #if !defined(__PROTO__) #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus) #if defined(__cplusplus) #define __MANGLE__ "C" #else #define __MANGLE__ #endif #define __STDARG__ #define __PROTO__(x) x #define __OTORP__(x) #define __PARAM__(n,o) n #if !defined(__STDC__) && !defined(__cplusplus) #if !defined(c_plusplus) #define const #endif #define signed #define void int #define volatile #define __V_ char #else #define __V_ void #endif #else #define __PROTO__(x) () #define __OTORP__(x) x #define __PARAM__(n,o) o #define __MANGLE__ #define __V_ char #define const #define signed #define void int #define volatile #endif #if defined(__cplusplus) || defined(c_plusplus) #define __VARARG__ ... #else #define __VARARG__ #endif #if defined(__STDARG__) #define __VA_START__(p,a) va_start(p,a) #else #define __VA_START__(p,a) va_start(p) #endif #endif static const char id[] = "\n@(#)paste (AT&T Bell Laboratories) 04/01/92\0\n"; #include /* * paste the lines of the defined in and put results * to */ static int paste __PARAM__((int nstream,Sfio_t* streams[],Sfio_t *out, const char *delim,int dlen), (nstream, streams, out, delim, dlen)) __OTORP__(int nstream;Sfio_t* streams[];Sfio_t *out; const char *delim;int dlen;){ const char *cp; int d, n, more=1; Sfio_t *fp; do { d = (dlen>0?0:-1); for(n=more-1,more=0; n < nstream;) { if(fp=streams[n]) { if(cp = sfgetr(fp,'\n',0)) { if(n==0) more = 1; else if(!more) /* first stream with output */ { if(dlen==1) sfnputc(out, *delim, n); else if(dlen>0) { for(d=n; d>dlen; d-=dlen) sfwrite(out,delim,dlen); if(d) sfwrite(out,delim,d); } more = n+1; } if(sfwrite(out,cp,sfslen()-((n+1)=0) { int c; if(d >= dlen) d = 0; if(c=delim[d++]) sfputc(out,c); } } } while(more); return(0); } /* * Handles paste -s, for file to file using delimiters */ static int spaste __PARAM__((Sfio_t *in,Sfio_t* out,const char *delim,int dlen), (in, out, delim, dlen)) __OTORP__(Sfio_t *in;Sfio_t* out;const char *delim;int dlen;){ const char *cp; int d=0; if(cp = sfgetr(in,'\n',0)) { if(sfwrite(out,cp,sfslen()-1) < 0) return(-1); } while(cp=sfgetr(in, '\n',0)) { if(dlen) { int c; if(d >= dlen) d = 0; if(c=delim[d++]) sfputc(out,c); } if(sfwrite(out,cp,sfslen()-1) < 0) return(-1); } sfputc(out,'\n'); return(0); } int b_paste __PARAM__((int argc,char *argv[]), (argc, argv)) __OTORP__(int argc;char *argv[];){ int n, sflag=0; Sfio_t *fp, **streams; char *cp, *delim = "\t"; /* default delimiter */ int dlen; NoP(id[0]); cmdinit(argv); while (n = optget(argv, "d:[delim]s [file...]")) switch (n) { case 'd': delim = opt_info.arg; break; case 's': sflag++; break; case ':': error(2, opt_info.arg); break; case '?': error(ERROR_usage(2), opt_info.arg); break; } argv += opt_info.index; if(error_info.errors) error(ERROR_usage(2),optusage(NiL)); dlen = stresc(delim); if(cp = *argv) { n = argc - opt_info.index; argv++; } else n = 1; if(!sflag) { streams = (Sfio_t**)stakalloc(n*sizeof(Sfio_t*)); n = 0; } do { if(!cp || streq(cp,"-")) fp = sfstdin; else if(!(fp = sfopen(NiL,cp,"r"))) { error(ERROR_system(0),"%s: cannot open",cp); error_info.errors = 1; } if(fp && sflag) { if(spaste(fp,sfstdout,delim,dlen) < 0) { error(ERROR_system(0),"write failed"); error_info.errors = 1; } if(fp!=sfstdin) sfclose(fp); } else streams[n++] = fp; } while(cp= *argv++); if(!sflag) { if(error_info.errors==0 && paste(n,streams,sfstdout,delim,dlen) < 0) { error(ERROR_system(0),"write failed"); error_info.errors = 1; } while(--n>=0) { if((fp=streams[n]) && fp!=sfstdin) sfclose(fp); } } return(error_info.errors); }