// Programmer: Kasia Trapszo // Course: CS 152 // Project: Project 1 // Program: od // Description: Unix od utility (octal dump) // Files: User specified input file import java.io.*; public class od { // Defines the end of file public final static int EOF = -1; // Proper usage message private final static String usageMessage[] = { " ", "The Usage is:", " ", " java od -h Printout this message", " ", " java od -b [FileName] Printout the contents of a file as octal", " ", " java od -c [FileName] Printout all printable characters in a file", " as ASCII, all control characters as corresponding", " letter preceded by a backslash and rest in octal." }; public static void main(String args[]) { od o = new od(); // If the number of arguments is 0, program prints out usage message // and exits if (args.length == 0) { System.out.println("Error: Invalid number of arguments"); o.printStringArray(usageMessage); return; } // If the number of arguments is higher than 2, program prints out // usage message and exits if (args.length > 2) { System.out.println("Error: Invalid number of arguments"); o.printStringArray(usageMessage); return; } // Option -h, prints out usage message if (args[0].equals("-h")) { o.printStringArray(usageMessage); } // option -b prints out the octal representation of a user // specified file else if (args[0].equals("-b")) { // If the number of arguments is less than 2, program exits with // an error message if (args.length < 2) { System.out.println("Error: Invalid number of arguments"); return; } // If the procedure worked succesfully, program prints out // a success message if(o.optionB(args[1])) { System.out.println(); System.out.println("Command completed succesfully"); } // If the procedure did not work properly, program prints // out an error message else { System.out.println("Fatal error"); return; } } // Option -c, all printable and control characters printed // rest in octal representation else if (args[0].equals("-c")) { // If the number of arguments is less than 2, program prints // out an error message and exits if (args.length < 2) { System.out.println("Error: Invalid number of arguments"); return; } // If the procedure worked properly, program prints out a // success message if (o.optionC(args[1])) { System.out.println(); System.out.println("Command completed succesfully"); } // If the procedure did not work properly, program prints // out an error message else { System.out.println("Fatal error"); return; } } // If none of the valid options were chosen, program prints out the // usage message and exits else { System.out.println("Error: Invalid argument"); o.printStringArray(usageMessage); } } // main // Option -b, octal representation of a user specified file // returns true when succesfull and false when there's an error, // accepts file name as argument private boolean optionB(String fName) { int i; // used for reading from the file int c = 0; // used for output formatting String b; // used for output FileInputStream inStream; try { inStream = new FileInputStream(fName); } // If file not found, prints an error message and exits catch (FileNotFoundException e) { System.out.println("Error: File '" + fName + "' not found"); return false; } // If unable to access the file for security reasons, prints out // an error message and exits catch (SecurityException e) { System.out.println("Error: You do not have sufficient privileges to complete this operation"); return false; } // Buffered file reader BufferedInputStream inBufStream = new BufferedInputStream(inStream); try { System.out.println(); System.out.println("Octal representation of '" + fName + "':"); System.out.println(); while((i = inBufStream.read()) != EOF) { b = Integer.toString(i, 8); // converts to octal printOctal(b); // output formatting c += 1; if (c == 16) { System.out.println(); c = 0; } } inBufStream.close(); // close in-file System.out.println(); } // if there's an IO error, prints out an error message and exits catch (IOException e){ System.out.println("Error reading from file"); return false; } // Procedure complete succesfully return true; } // optionB // Option -c, prints out control characters and printable characters // and converts everything else to octal. // Returns true when succesfull and false when failed // Accepts file name as argument private boolean optionC(String fName) { int i; // used for reading from file int c = 0; // used for formatting output String b; // used for output FileInputStream inStream; try { inStream = new FileInputStream(fName); } // If file not found, print out an error message and exit catch (FileNotFoundException e) { System.out.println("Error: File '" + fName + "' not found"); return false; } // If file unaccessible for security reasons, print out an error // message and exit catch (SecurityException e) { System.out.println("Error: You do not have sufficient privileges to complete this operation"); return false; } // Buffered file reader BufferedInputStream inBufStream = new BufferedInputStream(inStream); try { System.out.println(); while((i = inBufStream.read()) != EOF) { // If decimal 0, then print out the null character ^@ if (i == 0) { System.out.print(" ^@ "); } // if decimal 10, the print out the end of line character \n else if (i == 10) { System.out.print(" \\n "); } // if decimal 13, then print out the carriage return character // \r else if (i == 13) { System.out.print(" \\r "); } // if decimal 0 - 10, print out the appropriate control // characters else if ((i > 0) && (i < 10)) { System.out.print(" ^" + (char)(i + 64) + " "); } // if decimal 10 - 13, print out the appropriate control // characters else if ((i > 10) && (i < 13)) { System.out.print(" ^" + (char)(i + 64) + " "); } // if decimal 12 - 27, print out the appropriate // control characters else if ((i > 13) && (i < 27)) { System.out.print(" ^" + (char)(i + 64) + " "); } // decimal 27 - 127 print out the printable // characters else if ((i >= 27) && (i <127)) { System.out.print(" " + (char)i + " "); } // decimal 127, print out "del" else if ( i == 127 ) { System.out.print("del "); } // Convert everything else to octal else { b = Integer.toString(i, 8); printOctal(b); } // Output formatting c += 1; if (c == 16) { System.out.println(); c = 0; } } inBufStream.close(); // close the in file System.out.println(); } // File reading error, print out an error message and exit catch (IOException e){ System.out.println("Error reading from file"); return false; } // Procedure completed succesfully return true; } // optionC // Prints out properly formatted octal representations // Returns no value and accepts String b as argument private void printOctal(String b) { // if the length of b is 1 character, add two 0s if (b.length() == 1) { System.out.print("00"); System.out.print(b); } // if the length of b is 2 characters, add 1 0 else if (b.length() == 2) { System.out.print("0"); System.out.print(b); } // else just print out b else System.out.print(b); // print out a blank space to seperate octal numbers System.out.print(" "); } // printOctal // Procedure to print out an array of strings // Returns no value and accepts an array of strings as argument private void printStringArray(String s[]) { for(int i = 0; i < s.length; i++) System.out.println(s[i]); } // printStringArray } //class od