home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-25 | 2.3 KB | 73 lines | [TEXT/Moml] |
- (* A very simple cgi version of the Unix utility ls: list directory. *)
-
- (* build with:
-
- load "Path";
- load "Process";
-
- val home =
- case Process.getEnv "PATH_TRANSLATED" of
- SOME n => Path.dir n
- | NONE => ":"
- ;
-
- let val base = home ^ "e_SML:cgi:"
- in
- make "default" (home ^ "lib") [] base;
- chDir base;
- link "cgi_ls.image"
- (true,true) (* -g -noheader *)
- (true,"") (* -autolink -P *)
- (home ^ "lib") []
- ["cgi_ls.uo"]
- end;
- *)
-
- fun listdir path =
- let open FileSys TextIO
- val dir = openDir path
- fun read "" res = res
- | read f res = read (readDir dir) (f :: res)
- val filenames = Listsort.sort String.compare
- (read (readDir dir) []) before closeDir dir
- val (longest, count) =
- foldl (fn (x, (max, cnt)) => (Int.max(max, size x), cnt+1))
- (0, 0) filenames
- val cols = Int.max(1, 80 div (longest + 2));
- val fstrows = (count-1) div cols
- val lastrow = count - cols * fstrows (* 0 <= lastrow <= cols *)
-
- val filenames = Vector.fromList filenames
- fun file (row, col) =
- Vector.sub(filenames, col*fstrows+Int.min(lastrow, col)+row)
- fun left n s = StringCvt.padRight #" " n s
- fun prrow row j m =
- if j >= m then ""
- else left (longest + 2) (file (row, j)) ^ prrow row (j+1) m
- fun println s = output(stdOut, s ^ "<BR>\r\n")
- fun prrows i =
- if i >= fstrows then ()
- else (println (prrow i 0 cols); prrows (i+1))
- in
- output(stdOut, "HTTP/1.0 200 OK\r\n");
- output(stdOut, "MIME-version: 1.0\r\n");
- output(stdOut, "Content-type: text/html\r\n\r\n");
- (* Print the header *)
- output(stdOut, "<P><CODE><PRE>");
- prrows 0; (* Print all rows but the last *)
- println (prrow fstrows 0 lastrow); (* Print the last row *)
- output(stdOut, "</PRE></CODE>")
- end;
-
- fun errmsg s = TextIO.output(TextIO.stdErr, s ^ "\n");
-
- val _ =
- (*
- case Mosml.argv () of
- [_] => listdir "."
- | [_, dir] => ((listdir dir)
- handle OS.SysErr (explanation, _) =>
- errmsg ("Error: "^ explanation))
- | _ => errmsg "Usage: mls [directory]"
-
- *) listdir ":"