Commit 62fcb82bd4dbdeccc43f2b3ec9bb7323f8b5a6b0

  • avatar
  • lsantha <lsantha @407389f7-7c16-0410…ad85acb55d7f.>
  • Tue Oct 20 23:23:04 CEST 2009
Applied patch by Mark Kokish.

git-svn-id: http://jnode.svn.sourceforge.net/svnroot/jnode/trunk@5684 407389f7-7c16-0410-9eee-ad85acb55d7f
  
262262 <option argLabel="url" shortName="u" longName="url" description="print a hex dump of a URL"/>
263263 </syntax>
264264 <syntax alias="ls">
265 <empty description="list the current directory"/>
265 <empty description="with -l, print sizes in human readable format (e.g., 1K 234M 2G)"/>
266 <option argLabel="humanReadable" shortName="h" longName="human-readable"/>
266267 <repeat>
267268 <argument argLabel="path" description="list files or directories"/>
268269 </repeat>
  
2323import java.io.File;
2424import java.io.IOException;
2525import java.io.PrintWriter;
26import java.text.DecimalFormat;
2627import java.text.SimpleDateFormat;
2728import java.util.Arrays;
2829import java.util.Comparator;
3232import org.jnode.shell.AbstractCommand;
3333import org.jnode.shell.syntax.Argument;
3434import org.jnode.shell.syntax.FileArgument;
35import org.jnode.shell.syntax.FlagArgument;
3536
3637/**
3738 * @author epr
4646 private static final SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm");
4747
4848 private static final String help_path = "the file or directory to list";
49 private static final String help_humanReadable = "with -l, print sizes in human readable format (e.g., 1K 234M 2G)";
4950 private static final String help_super = "List files or directories";
5051 private static final String fmt_no_path = "No such path: %s%n";
51
52
5253 private final FileArgument argPath;
53
54 private final FlagArgument humanReadableArg;
55
5456 public DirCommand() {
5557 super(help_super);
58 humanReadableArg = new FlagArgument("humanReadable", Argument.OPTIONAL, help_humanReadable);
5659 argPath = new FileArgument("path", Argument.OPTIONAL | Argument.MULTIPLE | Argument.EXISTING, help_path);
57 registerArguments(argPath);
60 registerArguments(argPath, humanReadableArg);
5861 }
5962
6063 public static void main(String[] args) throws Exception {
107107 sb.setLength(0);
108108 lastModified.setTime(f.lastModified());
109109 if (f.isFile()) {
110 String ln = String.valueOf(f.length()).concat("B");
110 String ln = formatSize(f.length());
111111 int cnt = LEFT_MARGIN - ln.length();
112 for (int j = 0; j < cnt; j++, sb.append(' '))
113 ;
112 for (int j = 0; j < cnt; j++) sb.append(' ');
114113 sb.append(ln);
115114 sb.append(" ");
116115 sb.append(df.format(lastModified));
128128 }
129129 out.println();
130130 }
131 }
132
133 private static final String[] units = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
134
135 protected String formatSize(double bytes) {
136 if (humanReadableArg.isSet()) {
137 int index;
138 for (index = 0; bytes >= 1024; index++) bytes = bytes / 1024;
139 DecimalFormat df = new DecimalFormat("###0.0");
140 return df.format(bytes) + units[index];
141 } else
142 return bytes + "B";
131143 }
132144}