The New Korn Shell

Listing 1

  1:#########################################################################
  2:#    lsc
  3:#
  4:#    lsc shell script using ksh93.  Accepts zero or more command line
  5:#    arguments and produces ls(1) style output with subdirs printed
  6:#    in bold.
  7:#########################################################################
  8:video=(
  9:    [bold]=$(tput bold)
 10:    [reset]=$(tput sgr0)
 11:    [reverse]=$(tput rev)
 12:)
 13:
 14:integer max_stringSize max_dirEntries
 15:
 16:cell=(
 17:    integer        maximum=0
 18:    integer        width=0
 19:    integer        index=0
 20:    typeset        entries
 21:)
 22:
 23:eval row="$cell"
 24:eval col="$cell"
 25:
 26:entries=""
 27:
 28:errorMessage=$"${video[reverse]} not found ${video[reset]}"
 29:builtin -f /home/cjn/ksh93.d/lib/libksfio.so.1 strlenList
 30:
 31:function max_stringSize.get
 32:{
 33:    (( .sh.value=$(strlenList ${entries[@]}) + 3 ))
 34:}
 35:
 36:function loadDirEntries # [directory_name]
 37:{
 38:    dirname="${1}"
 39:
 40:    unset row.entries col.entries
 41:
 42:    if    cd "${dirname}" 2>/dev/null
 43:    then
 44:        entries=(*)
 45:        [[ ${entries[0]} == $'*' ]] && return 2
 46:        max_dirEntries=${#entries[@]}
 47:        (( col.maximum = ( ${COLUMNS:=80} -1 ) / max_stringSize ))
 48:        (( col.width   = 0 ))
 49:        (( col.index   = 0 ))
 50:        col.entries[0]=""
 51:        (( row.maximum = ( max_dirEntries / col.maximum ) ))
 52:        (( row.width   = 0 ))
 53:        (( row.index   = 0 ))
 54:        row.entries[0]=""
 55:    else    return 1
 56:    fi
 57:    return 0
 58:}
 59:
 60:function setOutput
 61:{
 62:    if (( max_dirEntries < col.maximum ))
 63:    then     for entry in "${entries[@]}"
 64:        do
 65:            addToCell row "$entry"
 66:            addToCell col "$entry" advance
 67:        done
 68:
 69:    else     for entry in "${entries[@]}"
 70:        do
 71:            addToCell row "$entry" advance
 72:            addToCell col "$entry"
 73:
 74:            if (( row.index == row.maximum ))
 75:            then     (( ++col.index ))
 76:                (( row.index = 0 ))
 77:            fi
 78:        done
 79:    fi
 80:    for((col.index=0 ; col.index < col.maximum; ++col.index ))
 81:    do
 82:        (( col.width[col.index]=
 83:            $( strlenList ${col.entries[col.index]} )
 84:        ))
 85:    done
 86:}
 87:
 88:function addToCell # row|col $entry [advance]
 89:{
 90:    nameref cell=${1}
 91:
 92:    cell.entries[cell.index]="${cell.entries[cell.index]}${2} "
 93:    [[ ${3} == advance ]] && (( ++cell.index ))
 94:}
 95:
 96:
 97:function displayOutput
 98:{
 99:    for(( row.index=0; row.index <= row.maximum; ++row.index ))
100:    do
101:        col.index=0
102:        for dirEntry in ${row.entries[row.index]}
103:        do
104:        (( width = ${col.width[col.index]}  + 1 ))
105:
106:        if [[ -d ${dirEntry} ]]
107:        then     printf "${video[bold]}%-${width}s${video[reset]} " \
108:                    "${dirEntry}"
109:        else     printf "%-${width}s " "${dirEntry}"
110:        fi
111:        (( ++col.index ))
112:        done
113:        print
114:    done
115:}
116:
117:function lsc
118:{
119:    for directory
120:    do
121:        loadDirEntries "${directory}"
122:        case $? in
123:        0 )
124:            print -u1 -f "%s:\n" "${directory}:"
125:            setOutput
126:            displayOutput
127:            print
128:            ;;
129:        1 )
130:            print -u2 -f "%s: %s\n" "${directory}" "${errorMessage}"
131:            ;;
132:        2 )
133:            print -u2 -f "%s:\n" "${directory}"
134:            ;;
135:        esac
136:    done
137:}
138:
139:lsc "${@-.}"

Copyright Notice