Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/smlnj-lib/Unix/unix-path.sml
ViewVC logotype

Annotation of /sml/trunk/src/smlnj-lib/Unix/unix-path.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8 - (view) (download)
Original Path: sml/branches/SMLNJ/src/smlnj-lib/Unix/unix-path.sml

1 : monnier 2 (* unix-path.sml
2 :     *
3 :     * COPYRIGHT (c) 1991 by AT&T Bell Laboratories. See COPYRIGHT file for details.
4 :     *
5 :     * AUTHOR: John Reppy
6 :     * AT&T Bell Laboratories
7 :     * Murray Hill, NJ 07974
8 :     * jhr@research.att.com
9 :     *)
10 :    
11 :     structure UnixPath : UNIX_PATH =
12 :     struct
13 :    
14 : monnier 8 datatype access_mode = datatype OS.FileSys.access_mode
15 : monnier 2
16 :     (** WHAT IS THIS IN POSIX??? **)
17 :     datatype file_type = F_REGULAR | F_DIR | F_SYMLINK | F_SOCK | F_CHR | F_BLK
18 :    
19 :    
20 :     (** Path lists **)
21 :    
22 :     datatype path_list = PathList of string list
23 :    
24 :     exception NoSuchFile
25 :    
26 :     fun getPath () = let
27 :     val path = (case (UnixEnv.getEnv "PATH") of (SOME p) => p | _ => "")
28 :     in
29 :     PathList(String.fields (fn #":" => true | _ => false) path)
30 :     end (* getPath *)
31 :    
32 :     local
33 : monnier 8 structure ST = Posix.FileSys.ST
34 :     fun isFileTy (path, ty) = let
35 :     val st = Posix.FileSys.stat path
36 :     in
37 :     case ty
38 :     of F_REGULAR => ST.isReg st
39 :     | F_DIR => ST.isDir st
40 :     | F_SYMLINK => ST.isLink st
41 :     | F_SOCK => ST.isSock st
42 :     | F_CHR => ST.isChr st
43 :     | F_BLK => ST.isBlk st
44 :     (* end case *)
45 :     end
46 : monnier 2 fun access mode pathname = (OS.FileSys.access(pathname, mode))
47 :     fun accessAndType (mode, ftype) pathname = (
48 :     OS.FileSys.access(pathname, mode)
49 :     andalso isFileTy(pathname, ftype))
50 :     handle _ => false
51 :     (* return the first path p in the pathlist, such that p/name satisfies
52 :     * the predicate.
53 :     *)
54 :     fun findFile' (PathList l, pred) fname = let
55 :     fun find [] = raise NoSuchFile
56 :     | find (p::r) = let val pn = OS.Path.joinDirFile{dir=p, file=fname}
57 :     in
58 :     if (pred pn) then pn else find r
59 :     end
60 :     in
61 :     if (OS.Path.isAbsolute fname)
62 :     then if (pred fname) then fname else raise NoSuchFile
63 :     else find l
64 :     end
65 :     (* return the list of paths p in the pathlist, such that p/name satisfies
66 :     * the predicate.
67 :     *)
68 :     fun findFiles' (PathList l, pred) fname = let
69 :     fun find ([], l) = rev l
70 :     | find (p::r, l) = let val pn = OS.Path.joinDirFile{dir=p, file=fname}
71 :     in
72 :     if (pred pn) then find (r, pn::l) else find (r, l)
73 :     end
74 :     in
75 :     if (OS.Path.isAbsolute fname)
76 :     then if (pred fname) then [fname] else []
77 :     else find (l, [])
78 :     end
79 :     in
80 :     fun findFile (pl, mode) = findFile' (pl, access mode)
81 :     fun findFiles (pl, mode) = findFiles' (pl, access mode)
82 :     fun findFileOfType (pl, ftype, mode) =
83 :     findFile' (pl, accessAndType(mode, ftype))
84 :     fun findFilesOfType (pl, ftype, mode) =
85 :     findFiles' (pl, accessAndType(mode, ftype))
86 :     end (* local *)
87 :    
88 :     end (* UnixPath *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0