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

path                          type
/proc/net                     symbolic link
/proc/mounts                  symbolic link
/proc/self                    symbolic link
/proc/thread-self             symbolic link
0                             omited