parse passwd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import qualified Data.Attoparsec.ByteString.Char8 as AP
import Data.Attoparsec.ByteString.Char8
import qualified Data.ByteString.Char8 as BS
import Control.Applicative
import Control.Monad (void)

data User = User { name :: BS.ByteString,
                   password :: BS.ByteString,
                   uid :: Int,
                   gid :: Int,
                   info :: BS.ByteString,
                   homeDir :: BS.ByteString,
                   shell :: BS.ByteString
} deriving (Show)

parseUser = User <$> str <*> str <*> num <*> num <*> str <*> str <*> str
            where str = AP.takeWhile (/= ':') <* skipWhile (== ':')
                  num = decimal <* skipWhile (== ':')

main = void ((mapM (print . (\(Right x) -> x) . parseOnly parseUser) . BS.lines) =<<
       BS.readFile "/etc/passwd")