Forgot to attach it :]
#!/bin/bash
# 
# Mimics {md5,sha1}sum coreutils' apps using openssl.
# 

# File separator
sep=$(echo -ne '\a')

# Collected files (separated by ${sep})
files=''

action='sum'
quiet='false'
while [ $# -gt 0 ]; do
        case "${1}" in
                (--*)
                        case "${1#??}" in
                                (check)         action='check'          ;;
                                (status)        quiet='true'            ;;
                                (help)          action='help'           ;;
                                (version)       action='version'        ;;
                                (binary|text|warn)
                                        echo "${0}: ignored -- ${1}" >&2
                                        ;;
                                (*)
                                        echo "${0}: unknown option -- ${1}" >&2
                                        ;;
                        esac
                        ;;
                (-*)
                        for opt in $(echo "${1#?}" | sed 's#.#\0 #g'); do
                                case "${opt}" in
                                        (w|b|t)
                                                echo "${0} ignored: -${opt}" >&2
                                                ;;
                                        (c)     action='check'          ;;
                                esac
                        done
                        ;;
                (*)
                        files="${files}${sep}${1}"
                        ;;
        esac
        shift
done

if [ -z "${files}" ]; then
        files='-'
fi

me=${0##*/}
digest=${me%sum}

openssl_sum ( )
{
        if [ x"${1}" = x'-' ]; then
                openssl dgst "-${digest}"
        else
                openssl dgst "-${digest}" <"${1}"
        fi
}

do_sum ( )
{
        echo "$(openssl_sum "${1}")  ${1}"
}

check ( )
{
        curr_sum=$(openssl_sum "${1}")
        if [ x"${curr_sum}" = x"${2}" ]; then
                return 0
        fi
        return 1
}

do_check ( )
{
        local sum=''
        local res=0
        
        exec 9< "${1}"
        while read line <&9; do
                sum=${line%% *}
                file=${line#* }
                [[ "${sum}" =~ '\*$' ]] && sum=${sum%?}
                (( check_count++ ))
                if check "${file}" "${sum}"; then
                        echo "${file}: OK"
                else
                        echo "${file}: FAILED"
                        (( check_failed++ ))
                        res=1
                fi
        done
        exec 9<&-
        return ${res}
}

case "${action}" in
        (sum|check)
                res=0
                check_count=0
                check_failed=0
                IFS=${sep}
                for file in ${files}; do
                        [ -z "${file}" ] && continue
                        [ x"${file}" = x'-' -o -f "${file}" ] || {
                                echo "${0}: ${file}: No such file or directory" 
>&2
                                res=1
                                continue
                        }
                        if [ "${action}" = 'sum' ]; then
                                do_sum "${file}"
                        else
                                do_check "${file}" || res=$?
                        fi
                done
                if [ ${check_failed} -gt 0 ]; then
                        echo "${0}: WARNING: ${check_failed} of ${check_count} 
computed checksum did NOT match" >&2
                fi
                exit ${res}
                ;;
        (help)
                cat <<EOT
Usage: ${me} [OPTION] [FILE]...
Print or check ${digest} checksums using openssl.
With no FILE, or when FILE is -, read standard input.

  -c, --check             read ${digest} sums from the FILEs and check them

The following two options are useful only when verifying checksums:
      --status            don't output anything, status code shows success

      --help     display this help and exit
      --version  output version information and exit

The default mode is to print a line with checksum, a character indicating type
(\`*' for binary, \` ' for text), and name for each FILE, this script however
always uses text mode.

Report bugs to <[email protected]> or as a ticket on
<http://wiki.linuxfromscratch.org/hlfs/report>
EOT
                ;;
        (version)
                cat <<EOT
md5sum (HLFS) 1.20060917

Written by Jan Dvorak for HLFS project.
You may redistribute copies of this script under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
EOT
                ;;
esac

# EOF
-- 
http://linuxfromscratch.org/mailman/listinfo/hlfs-dev
FAQ: http://www.linuxfromscratch.org/faq/
Unsubscribe: See the above information page

Reply via email to