-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswdAL.hs
41 lines (34 loc) · 1.06 KB
/
PasswdAL.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module PasswdAL where
import Data.List
import System.IO
import Control.Monad(when)
import System.Exit
import System.Environment(getArgs)
main = do
args <- getArgs
when (length args /= 2) $ do
putStrLn "Syntax: passwd-al filename uid"
exitFailure
content <- readFile (args !! 0)
let username = findByUID content (read (args !! 1))
case username of
Just x -> putStrLn x
Nothing -> putStrLn "Could not find that UID"
findByUID :: String -> Integer -> Maybe String
findByUID content uid =
let al = map parseline . lines $ content
in lookup uid al
-- Reads a colon separated line into fields
parseline :: String -> (Integer, String)
parseline input =
let fields = split ':' input
in (read (fields !! 2), fields !! 0)
{- Takes a delimiter and a list. Break up the list with delim. -}
split :: Eq a => a -> [a] -> [[a]]
split _ [] = [[]]
split delim str =
let (before, remainder) = span (/= delim) str
in
before : case remainder of
[] -> []
x -> split delim (tail x)