]> arthur.barton.de Git - bup.git/blob - dev/checksum
fmincore: fix mmap leak
[bup.git] / dev / checksum
1 #!/usr/bin/env bash
2
3 set -ueo pipefail
4
5 usage() { echo "Usage: checksum -t <sha1|sha256> [--] [PATH]"; }
6
7 misuse() { usage 1>&2; exit 2; }
8
9 kind=''
10 while test $# -gt 0; do
11     case "$1" in
12         --)
13             shift
14             break
15             ;;
16         -t)
17             shift
18             test $# -gt 0 || misuse
19             kind="$1"
20             case "$kind" in
21                 sha1|sha256) ;;
22                 *) misuse ;;
23             esac
24             shift
25             ;;
26         -*)
27             misuse ;;
28         *)
29             break ;;
30     esac
31 done
32
33 test "$kind" || misuse
34
35 src=''
36 case $# in
37     0) ;;
38     1) src="$1" ;;
39     *) misuse ;;
40 esac
41
42 # Use KINDsum if available, else KIND (e.g. sha1sum or sha1).  Assumes
43 # the former is compatible with the coreutils version, and the latter
44 # is compatible with the FreeBSD version.
45
46 if command -v "$kind"sum > /dev/null; then
47     if test "$src"; then
48         result=$("$kind"sum "$src")
49     else
50         result=$("$kind"sum)
51     fi
52     echo "${result%% *}"
53 elif command -v "$kind" > /dev/null; then
54     if test "$src"; then
55         "$kind" -q "$src"
56     else
57         "$kind" -q
58     fi
59 else
60     echo "Can't find sha1sum or sha1" 1>&2
61     exit 2
62 fi