package org.jboss.fresh.shell.commands;
import org.jboss.fresh.vfs.FileInfo;
import org.jboss.fresh.vfs.FileName;
import org.jboss.fresh.vfs.VFS;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.io.OutBuffer;
import org.jboss.fresh.shell.AbstractExecutable;
import org.jboss.fresh.shell.Shell;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;
// EX OK
public class LSCommand extends AbstractExecutable {
private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LSCommand.class);
/**
Displays the contents of the specified directory if no directory is specified then the current working directory is used.
*/
public void process(String exepath, String[] params) throws Exception {
log.debug("entered");
if (helpRequested()) {
PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
out.print("Usage: ls [--help] [dir]\n");
out.print(" dir : the directory to be viewed - if none is specified then the current working directory is viewed.\n");
out.print(" --help : this help");
out.close();
log.debug("done");
return;
}
Shell sh = getShell();
VFS vfs = sh.getVFS();
OutBuffer out = getStdOut();
FileName fname;
if (params == null || params.length == 0) {
fname = new FileName(sh.getEnvProperty("PWD"));
} else {
fname = new FileName(sh.getEnvProperty("PWD")).absolutize(params[0]);
}
if (vfs.exists(null, fname, false)) {
out.put("\n Directory of " + fname + "\n\n", 10000L);
Collection col = vfs.list(null, fname, false);
Iterator it = col.iterator();
out.put(" " + "\t<DIR>" + "\t..\n", 10000L);
while (it.hasNext()) {
FileInfo fd = (FileInfo) it.next();
String ftyp = "";
if (fd.isLink()) {
// dobi extra
FileInfo extra = fd.getExtra();
if (extra == null) {
ftyp = "-dead L";
} else {
if (extra.isDirectory())
ftyp = "<DIR> L";
else
ftyp = "file L";
}
} else {
if (fd.isDirectory())
ftyp = "<DIR> ";
else
ftyp = "file ";
}
out.put(fd.getLastModified() + "\t" + ftyp + "\t" + fd.getFileName().getName() + "\n", 10000L);
}
} else {
out.put("Path does not exist!", 10000L);
}
log.debug("done");
}
}