1 8 package bsh.commands; 9 10 import java.io.*; 11 import bsh.*; 12 import java.util.Date ; 13 import java.util.Vector ; 14 import java.util.GregorianCalendar ; 15 import java.util.Calendar ; 16 17 public class dir 18 { 19 static final String [] months = { "Jan", "Feb", "Mar", "Apr", 20 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; 21 22 public static String usage() { 23 return "usage: dir( String dir )\n dir()"; 24 } 25 26 29 public static void invoke( Interpreter env, CallStack callstack ) 30 { 31 String dir = "."; 32 invoke( env, callstack, dir ); 33 } 34 35 38 public static void invoke( 39 Interpreter env, CallStack callstack, String dir ) 40 { 41 File file; 42 try { 43 file = env.pathToFile( dir ); 44 } catch (IOException e ) { 45 env.println("error reading path: "+e); 46 return; 47 } 48 49 if ( !file.exists() || !file.canRead() ) { 50 env.println( "Can't read " + file ); 51 return; 52 } 53 if ( !file.isDirectory() ) { 54 env.println("'"+dir+"' is not a directory"); 55 } 56 57 String [] files = file.list(); 58 files = StringUtil.bubbleSort(files); 59 60 for( int i=0; i< files.length; i++ ) { 61 File f = new File( dir + File.separator + files[i] ); 62 StringBuffer sb = new StringBuffer (); 63 sb.append( f.canRead() ? "r": "-" ); 64 sb.append( f.canWrite() ? "w": "-" ); 65 sb.append( "_" ); 66 sb.append( " "); 67 68 Date d = new Date (f.lastModified()); 69 GregorianCalendar c = new GregorianCalendar (); 70 c.setTime(d); 71 int day = c.get(Calendar.DAY_OF_MONTH); 72 sb.append( months[ c.get(Calendar.MONTH) ] + " " + day ); 73 if ( day < 10 ) 74 sb.append(" "); 75 76 sb.append(" "); 77 78 int fieldlen = 8; 80 StringBuffer len = new StringBuffer (); 81 for(int j=0; j<fieldlen; j++) 82 len.append(" "); 83 len.insert(0, f.length()); 84 len.setLength(fieldlen); 85 int si = len.toString().indexOf(" "); 87 if ( si != -1 ) { 88 String pad = len.toString().substring(si); 89 len.setLength(si); 90 len.insert(0, pad); 91 } 92 93 sb.append( len.toString() ); 94 95 sb.append( " " + f.getName() ); 96 if ( f.isDirectory() ) 97 sb.append("/"); 98 99 env.println( sb.toString() ); 100 } 101 } 102 } 103 104 | Popular Tags |