     ////////////////////////////////
    // FILE-CONCATENATION          //
   // Written by Andrew Broad     ///
  //                             ////
 // Cat.java (2003:06:03 20:05) /////
////////////////////////////////////
// Methods of Cat:              ///
// - main                       //
/////////////////////////////////

import java.io.*;

/*****************************************************************************
** The class `Cat' implements file-concatenation.
*****************************************************************************/

public class Cat {
	/********************************************************************/
	// static Cat.main : String[] -> void
	/*********************************************************************
	** This function takes a list of files and concatenates them, writing
	** the result to standard output.
	** @param argv The vector of command-line arguments. There should be
	**             at least one of these.
	*********************************************************************/

	public static void main (String argv[]) {
		if (argv.length < 1) {
			System.err.println("Usage: java Cat <output-file> "
			+ "[input-files]");
			System.exit(1);
		}

		FileInputStream fis= null;
		FileOutputStream fos= null;
		try {
			fos= new FileOutputStream(argv[0]);
		} catch (FileNotFoundException fnfe) {
			System.err.println("Cat.main: cannot open\""
			+ argv[0] + "\"");
			System.exit(1);
		}
		for (int i= 1; i < argv.length; i++) {
			try {
				fis= new FileInputStream(argv[i]);
			} catch (FileNotFoundException fnfe) {
				System.err.println("Cat.main: FileNotFoundExc"
				+ "eption for \"" + argv[i] + "\"");
				System.exit(1);
			}
			int c;
			try {
				while (fis.available() > 0) {
					fos.write(fis.read());
				}
				fis.close();
			} catch (IOException ioe) {
				System.err.println("IOException!");
				System.exit(1);
			}
		}
	}
}
