package net.hyperic.sigar.cmd;

import net.hyperic.sigar.ResourceLimit;
import net.hyperic.sigar.SigarException;
import net.hyperic.sigar.jmx.SigarInvokerJMX;

/**
 * Display system resource limits.
 */
public class Ulimit extends SigarCommandBase {

    private SigarInvokerJMX invoker;
    private String mode;
    
    public Ulimit(Shell shell) {
        super(shell);
    }

    public Ulimit() {
        super();
    }

    public String getUsageShort() {
        return "Display system resource limits";
    }

    private static String format(long val) {
        if (val == ResourceLimit.INFINITY()) {
            return "unlimited";
        }
        else {
            return String.valueOf(val);
        }
    }

    private String getValue(String attr)
        throws SigarException {
        Long val = (Long)this.invoker.invoke(attr + this.mode);
        return format(val.longValue());
    }
    
    public void output(String[] args) throws SigarException {

        this.invoker =
            SigarInvokerJMX.getInstance(this.proxy, "Type=ResourceLimit");

        this.mode = "Cur";
        
        println("core file size......." + getValue("Core"));
        println("data seg size........" + getValue("Data"));
        println("file size............" + getValue("FileSize"));
        println("max memory size......" + getValue("Memory"));
        println("open files..........." + getValue("OpenFiles"));
        println("stack size..........." + getValue("Stack"));
        println("cpu time............." + getValue("Cpu"));
        println("max user processes..." + getValue("Processes"));
        println("virual memory........" + getValue("VirtualMemory"));
    }

    public static void main(String[] args) throws Exception {
        new Ulimit().processCommand(args);
    }
}

@@ -503,32 +503,112 @@
 limits%22;%0A    %7D%0A%0A
+    protected boolean validateArgs(String%5B%5D args) %7B%0A        return true;%0A    %7D%0A%0A
     private stat
@@ -632,24 +632,24 @@
 long val) %7B%0A
-
         if (
@@ -1044,24 +1044,51 @@
 xception %7B%0A%0A
+        this.mode = %22Cur%22;%0A
         this
@@ -1182,34 +1182,373 @@
         
-this.mode = %22Cur%22;
+for (int i=0; i%3Cargs.length; i++) %7B%0A            String arg = args%5Bi%5D;%0A            if (arg.equals(%22-H%22)) %7B%0A                this.mode = %22Max%22;%0A            %7D%0A            else if (arg.equals(%22-S%22)) %7B%0A                this.mode = %22Cur%22;%0A            %7D%0A            else %7B%0A                throw new SigarException(%22Unknown argument: %22 + arg);%0A            %7D%0A        %7D
 %0A       

support -H flag for hard limits