/* lam - laminate two or more files ** ** Let's say you have these three files: ** ** "file 1" ** "file 2" ** "file 3" ** ** "lam file1 file2 file3" will produce: ** ** "file 1file 2file 3" ** ** Note that there is a fairly wide-spread program by the same name ** that does more or less the same thing. I wrote this because the ** other version wasn't available one day when I needed it, and besides ** its argument syntax is weird. ** ** Copyright (C) 1993 by Jef Poskanzer . ** All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions ** are met: ** 1. Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** 2. Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in the ** documentation and/or other materials provided with the distribution. ** ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** SUCH DAMAGE. */ #include #include #define MAXFILES 1000 #define MAXLINE 5000 int main( int argc, char** argv ) { int argn, n, i, l; int gotone; int eof[MAXFILES]; FILE* fp[MAXFILES]; char* filename[MAXFILES]; char* separator[MAXFILES]; char line[MAXLINE]; if ( argc < 3 ) { (void) fprintf( stderr, "usage: %s file1 file2 ...\n", argv[0] ); exit( 1 ); } if ( argc - 1 > MAXFILES ) { (void) fprintf( stderr, "%s: too many files!\n", argv[0] ); exit( 1 ); } n = 0; for ( argn = 1; argn < argc; ++argn ) { if ( strcmp( argv[argn], "-s" ) == 0 && argn + 1 < argc ) { ++argn; separator[n] = argv[argn]; } else { filename[n] = argv[argn]; if ( strcmp( filename[n], "-" ) == 0 ) fp[n] = stdin; else { fp[n] = fopen( filename[n], "r" ); if ( fp[n] == (FILE*) 0 ) { perror( filename[n] ); exit( 1 ); } } ++n; } } do { gotone = 0; for ( i = 0; i < n; ++i ) { if ( fp[i] != (FILE*) 0 ) { if ( fgets( line, sizeof(line), fp[i] ) == (char*) 0 ) { eof[i] = 1; if ( fp[i] != stdin ) (void) fclose( fp[i] ); fp[i] = (FILE*) 0; } else { eof[i] = 0; gotone = 1; if ( separator[i] != (char*) 0 ) (void) fputs( separator[i], stdout ); l = strlen( line ); if ( line[l-1] == '\n' ) line[l-1] = '\0'; (void) fputs( line, stdout ); } } } if ( gotone ) { (void) fputs( "\n", stdout ); for ( i = 0; i < n; ++i ) if ( eof[i] ) (void) fprintf( stderr, "Premature EOF on %s\n", filename[i] ); } } while ( gotone ); exit( 0 ); }