home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / e_SML / cgi / cgi_ls.sml next >
Encoding:
Text File  |  1997-07-25  |  2.3 KB  |  73 lines  |  [TEXT/Moml]

  1. (* A very simple cgi version of the Unix utility ls: list directory. *)
  2.  
  3. (* build with:
  4.  
  5. load "Path";
  6. load "Process";
  7.  
  8. val home =
  9.   case Process.getEnv "PATH_TRANSLATED" of
  10.     SOME n => Path.dir n
  11.   | NONE => ":"
  12. ;
  13.  
  14. let val base = home ^ "e_SML:cgi:"
  15. in
  16.   make "default" (home ^ "lib") [] base;
  17.   chDir base;
  18.   link "cgi_ls.image"
  19.        (true,true) (* -g -noheader *)
  20.        (true,"")   (* -autolink -P *)
  21.        (home ^ "lib") []
  22.        ["cgi_ls.uo"]
  23. end;
  24. *)
  25.  
  26. fun listdir path = 
  27.     let open FileSys TextIO
  28.         val dir = openDir path
  29.         fun read "" res = res
  30.           | read f  res = read (readDir dir) (f :: res)
  31.         val filenames = Listsort.sort String.compare
  32.                         (read (readDir dir) []) before closeDir dir 
  33.         val (longest, count) = 
  34.             foldl (fn (x, (max, cnt)) => (Int.max(max, size x), cnt+1))
  35.                   (0, 0) filenames
  36.         val cols = Int.max(1, 80 div (longest + 2));
  37.         val fstrows = (count-1) div cols
  38.         val lastrow = count - cols * fstrows       (* 0 <= lastrow <= cols *)
  39.  
  40.         val filenames = Vector.fromList filenames           
  41.         fun file (row, col) = 
  42.             Vector.sub(filenames, col*fstrows+Int.min(lastrow, col)+row)
  43.         fun left n s = StringCvt.padRight #" " n s
  44.         fun prrow row j m = 
  45.             if j >= m then ""
  46.             else left (longest + 2) (file (row, j)) ^ prrow row (j+1) m
  47.         fun println s = output(stdOut, s ^ "<BR>\r\n")
  48.         fun prrows i = 
  49.             if i >= fstrows then () 
  50.             else (println (prrow i 0 cols); prrows (i+1))
  51.     in
  52.         output(stdOut, "HTTP/1.0 200 OK\r\n");
  53.         output(stdOut, "MIME-version: 1.0\r\n");
  54.         output(stdOut, "Content-type: text/html\r\n\r\n");
  55.                                            (* Print the header *)
  56.         output(stdOut, "<P><CODE><PRE>");
  57.         prrows 0;                          (* Print all rows but the last *)
  58.         println (prrow fstrows 0 lastrow); (* Print the last row *)
  59.         output(stdOut, "</PRE></CODE>")
  60.     end;
  61.  
  62. fun errmsg s = TextIO.output(TextIO.stdErr, s ^ "\n");
  63.  
  64. val _ = 
  65. (*
  66.     case Mosml.argv () of 
  67.         [_]      => listdir "."
  68.       | [_, dir] => ((listdir dir) 
  69.                     handle OS.SysErr (explanation, _) => 
  70.                         errmsg ("Error: "^ explanation))
  71.       | _        => errmsg "Usage: mls [directory]"
  72.  
  73. *)  listdir ":"