/* ncat.c: Named cat - display multiple files with their filenames. Jason Hood, 2 January, 1999. Public Domain. 991111: slight modifications. 031013, v1.00: and again. */ #define PVERS "1.00" #define PDATE "13 October, 2003" #include #include #include #ifdef __DJGPP__ // Location of isatty # include #else # include #endif void help( void ) { puts( "\ Named Cat by Jason Hood .\n\ Version "PVERS" ("PDATE"). Public Domain.\n\ http://misc.adoxa.cjb.net/\n\ \n\ Combine multiple files to standard output, along with their filenames.\n\ Use a filename of \"-\" to specify standard input (if not given it will\n\ be read first).\ " ); exit( 0 ); } int main( int argc, char* argv[] ) { FILE* file; char buf[2048]; int len; int input; int j; if ((argc == 1 && isatty( 0 )) || (argc > 1 && (strcmp( argv[1], "/?" ) == 0 || strcmp( argv[1], "-?" ) == 0 || strcmp( argv[1], "--help" ) == 0))) help(); // If input has been redirected, see if "-" has been given. if (!isatty( 0 )) { input = 0; for (j = 1; j < argc; ++j) { if (argv[j][0] == '-' && argv[j][1] == '\0') { input = 1; break; } } j = input; } else j = 1; for (; j < argc; ++j) { input = (j == 0 || (argv[j][0] == '-' && argv[j][1] == '\0')); printf( "==> %s <==\n", (input) ? "" : argv[j] ); if (input) file = stdin; else if ((file = fopen( argv[j], "r" )) == NULL) { printf( " could not be opened.\n" ); continue; } putchar( '\n' ); while ((len = fread( buf, 1, sizeof(buf), file )) > 0) fwrite( buf, 1, len, stdout ); if (!input) fclose( file ); if (j < argc - 1) putchar( '\n' ); } return 0; }