2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
4 \section[LibPosixDB]{Haskell 1.3 POSIX System Databases}
30 data GroupEntry = GE String GroupID [String]
32 groupName :: GroupEntry -> String
33 groupName (GE name _ _) = name
35 groupID :: GroupEntry -> GroupID
36 groupID (GE _ gid _) = gid
38 groupMembers :: GroupEntry -> [String]
39 groupMembers (GE _ _ members) = members
41 getGroupEntryForID :: GroupID -> IO GroupEntry
42 getGroupEntryForID gid =
43 _ccall_ getgrgid gid `thenPrimIO` \ ptr ->
44 if ptr == ``NULL'' then
45 failWith (NoSuchThing "no such group entry")
47 unpackGroupEntry ptr `thenPrimIO` \ group ->
50 getGroupEntryForName :: String -> IO GroupEntry
51 getGroupEntryForName name =
52 _packBytesForCST name `thenStrictlyST` \ gname ->
53 _ccall_ getgrnam gname `thenPrimIO` \ ptr ->
54 if ptr == ``NULL'' then
55 failWith (NoSuchThing "no such group entry")
57 unpackGroupEntry ptr `thenPrimIO` \ group ->
60 data UserEntry = UE String UserID GroupID String String
62 userName :: UserEntry -> String
63 userName (UE name _ _ _ _) = name
65 userID :: UserEntry -> UserID
66 userID (UE _ uid _ _ _) = uid
68 userGroupID :: UserEntry -> GroupID
69 userGroupID (UE _ _ gid _ _) = gid
71 homeDirectory :: UserEntry -> String
72 homeDirectory (UE _ _ _ home _) = home
74 userShell :: UserEntry -> String
75 userShell (UE _ _ _ _ shell) = shell
77 getUserEntryForID :: UserID -> IO UserEntry
78 getUserEntryForID uid =
79 _ccall_ getpwuid uid `thenPrimIO` \ ptr ->
80 if ptr == ``NULL'' then
81 failWith (NoSuchThing "no such user entry")
83 unpackUserEntry ptr `thenPrimIO` \ user ->
86 getUserEntryForName :: String -> IO UserEntry
87 getUserEntryForName name =
88 _packBytesForCST name `thenStrictlyST` \ uname ->
89 _ccall_ getpwnam uname `thenPrimIO` \ ptr ->
90 if ptr == ``NULL'' then
91 failWith (NoSuchThing "no such user entry")
93 unpackUserEntry ptr `thenPrimIO` \ user ->
98 Local utility functions
102 -- Copy the static structure returned by getgr* into a Haskell structure
104 unpackGroupEntry :: _Addr -> PrimIO GroupEntry
105 unpackGroupEntry ptr =
106 _casm_ ``%r = ((struct group *)%0)->gr_name;'' ptr
107 `thenPrimIO` \ str ->
108 strcpy str `thenPrimIO` \ name ->
109 _casm_ ``%r = ((struct group *)%0)->gr_gid;'' ptr
110 `thenPrimIO` \ gid ->
111 _casm_ ``%r = ((struct group *)%0)->gr_mem;'' ptr
112 `thenPrimIO` \ mem ->
113 unvectorize mem 0 `thenStrictlyST` \ members ->
114 returnPrimIO (GE name gid members)
116 -- Copy the static structure returned by getpw* into a Haskell structure
118 unpackUserEntry :: _Addr -> PrimIO UserEntry
119 unpackUserEntry ptr =
120 _casm_ ``%r = ((struct passwd *)%0)->pw_name;'' ptr
121 `thenPrimIO` \ str ->
122 strcpy str `thenPrimIO` \ name ->
123 _casm_ ``%r = ((struct passwd *)%0)->pw_uid;'' ptr
124 `thenPrimIO` \ uid ->
125 _casm_ ``%r = ((struct passwd *)%0)->pw_gid;'' ptr
126 `thenPrimIO` \ gid ->
127 _casm_ ``%r = ((struct passwd *)%0)->pw_dir;'' ptr
128 `thenPrimIO` \ str ->
129 strcpy str `thenPrimIO` \ home ->
130 _casm_ ``%r = ((struct passwd *)%0)->pw_shell;'' ptr
131 `thenPrimIO` \ str ->
132 strcpy str `thenPrimIO` \ shell ->
133 returnPrimIO (UE name uid gid home shell)