fork download
  1. import Data.List
  2. import System.Directory
  3. import System.Posix.Files as PF
  4. import Text.Printf
  5. data StMode
  6. = BlkDev
  7. | CharDev
  8. | Dir
  9. | NamedPipe
  10. | RegFile
  11. | Sock
  12. | SymLink
  13. deriving (Eq, Ord)
  14. instance Show StMode where
  15. show BlkDev = "block device"
  16. show CharDev = "character device"
  17. show Dir = "directory"
  18. show NamedPipe = "named pipe"
  19. show RegFile = "regular file"
  20. show Sock = "socket"
  21. show SymLink = "symbolic link"
  22. getStMode :: FilePath -> IO StMode
  23. getStMode fp =
  24. getFileStatus fp >>=
  25. (\x ->
  26. case () of
  27. _
  28. | isBlockDevice x -> return BlkDev
  29. | isCharacterDevice x -> return CharDev
  30. | isNamedPipe x -> return NamedPipe
  31. | isRegularFile x -> return RegFile
  32. | isDirectory x -> return Dir
  33. | PF.isSymbolicLink x -> return SymLink
  34. | isSocket x -> return Sock)
  35. groupByStMode :: [FilePath] -> IO [[(FilePath, StMode)]]
  36. groupByStMode fps = do
  37. stModes <- mapM getStMode fps
  38. let zipped = zip fps stModes
  39. sorted = sortBy (\(_, m0) (_, m1) -> compare m0 m1) zipped
  40. grouped = groupBy (\(_, m0) (_, m1) -> m0 == m1) sorted
  41. return grouped
  42. ls :: FilePath -> IO [FilePath]
  43. ls fp = listDirectory fp >>= (\list -> return $ map ((fp ++ "/") ++) list)
  44. main =
  45. ls "/proc/" >>= groupByStMode >>=
  46. (\list ->
  47. let (toShow, toHide) = splitAt 10 list
  48. in printf "%-30s%s\n" "path" "type" >>
  49. mapM_ (\x -> printf "%-30s%s\n" (fst x) (show $ snd x)) toShow >>
  50. printf "%-30s%s" (show $ length toHide) "omited\n\n")
Success #stdin #stdout 0.01s 5428KB
stdin
Standard input is empty
stdout
path                          type
/proc//fs                     directory
/proc//bus                    directory
/proc//irq                    directory
/proc//net                    directory
/proc//sys                    directory
/proc//tty                    directory
/proc//acpi                   directory
/proc//driver                 directory
/proc//sysvipc                directory
/proc//self                   directory
2                             omited

path                          type
/proc//fb                     regular file
/proc//dma                    regular file
/proc//keys                   regular file
/proc//kmsg                   regular file
/proc//misc                   regular file
/proc//mtrr                   regular file
/proc//stat                   regular file
/proc//iomem                  regular file
/proc//kcore                  regular file
/proc//locks                  regular file
36                            omited