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 /smlnj-lib/trunk/Unix/unix-path.sml
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2547 - (view) (download)

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

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