Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
#!/usr/bin/runhugs
module Main(main) where
import System(getArgs)
main :: IO ()
main = do args <- getArgs
if null args
then wcFiles ["-"]
else wcFiles args
wcFiles :: [String] -> IO ()
wcFiles [] = return ()
wcFiles ("-" : xs) = do input <- getContents
wcFile "-" input
wcFiles xs
wcFiles (x : xs) = do contents <- catch
(readFile x)
(\e -> do putStrLn ("ERROR reading"
++ " file: " ++ x)
return "")
wcFile x contents
wcFiles xs
wcFile :: String -> String -> IO ()
wcFile fname contents
= putStrLn (lineCnt ++ wordCnt ++ charCnt ++ " " ++ fname)
where ls = lines contents
lineCnt = rjustify 8 (show (length ls))
wordCnt = rjustify 8 (show (sum (map (length . words) ls)))
charCnt = rjustify 8 (show (length contents))
rjustify :: Int -> String -> String
rjustify n s = space (n - length s) ++ s
space :: Int -> String
space n = replicate n ' '