Hi Alex,

> I happen to agree with the FSF that programs should not change their 
> behaviour because their name has been changed, but that's just personal 
> preference. I think there should be an override option that selects the 
> digest independently of the executable name, at least.
Fine, done. Would you please try this and tell me what do change? And
another thing, what is the practical difference between --binary and
--text options?

# EOF
#!/bin/bash
# 
# Mimics {md5,sha1}sum coreutils' apps using openssl.
# 
# 2006-09-17    Mordae <[EMAIL PROTECTED]>
#       * Added options --digest and -d for digest selection.
# 

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

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

action='sum'
quiet='false'
selDigest=''
while [ $# -gt 0 ]; do
        case "${1}" in
                (--*)
                        case "${1#??}" in
                                (check)         action='check'          ;;
                                (status)        quiet='true'            ;;
                                (help)          action='help'           ;;
                                (version)       action='version'        ;;
                                (digest)
                                                selDigest=${2}
                                                shift
                                                ;;
                                (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
                                                ;;
                                        (d)
                                                selDigest=${2}
                                                shift
                                                ;;
                                        (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}" || return 1
        else
                openssl dgst "-${digest}" <"${1}" || return 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)
                [ -n "${selDigest}" ] && digest=${selDigest}
                
                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

Following options are specific to this particular script and are not supported
by original md5sum and sha1sum executables:

  --digest, -d            Selects digest to use, \`use openssl dgst --help\`
                          to get list of all suported digests.
                          This defaults to ${digest} for this name (${me}).

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