File: Main.hs

package info (click to toggle)
haskell-sha 1.6.4.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 4,764 kB
  • sloc: haskell: 2,308; makefile: 2
file content (37 lines) | stat: -rw-r--r-- 1,215 bytes parent folder | download | duplicates (6)
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
import Control.Monad
import Data.Digest.Pure.SHA
import qualified Data.ByteString.Lazy as BS
import System.Directory
import System.Environment
import System.Exit

main :: IO ()
main = do
  bname <- getProgName
  args <- getArgs
  case args of
    [] -> do
      inconts <- BS.getContents
      print $ ALGORITHM inconts
      exitSuccess
    xs -> foldM (sha_file bname) ExitSuccess xs >>= exitWith

sha_file :: String -> ExitCode -> String -> IO ExitCode
sha_file bname prevEC fname = do
  is_file <- doesFileExist fname
  is_dir  <- doesDirectoryExist fname
  case (is_file, is_dir) of
    (False, False) -> do
      putStrLn $ bname ++ ": " ++ fname ++ ": No such file or directory"
      return $ ExitFailure 22 -- EINVAL
    (False, True)  -> do
      putStrLn $ bname ++ ": " ++ fname ++ ": Is a directory"
      return $ ExitFailure 22 -- EINVAL
    (True,  _)     -> do
      conts <- BS.readFile fname
      putStrLn $ bname ++ " (" ++ fname ++ ") = " ++ show (ALGORITHM conts)
      return $ combineExitCodes prevEC ExitSuccess

combineExitCodes :: ExitCode -> ExitCode -> ExitCode
combineExitCodes ExitSuccess ExitSuccess = ExitSuccess
combineExitCodes _           _           = ExitFailure 22 -- EINVAL