:
#######################################################
# prog8 - show total number bytes, files, and percentage 
# of ownership by user.
#######################################################

# see also mklist
masterlist=/usr/stevei/c/stat/master.list

cat $masterlist |
awk '/^[lbcd]/ {next} # skip special files and symbolic links
{
    #_begin template for stat args: -oidlugpsanw
    mode=$1
    inum=$2
    device=$3

    # the [constructed] idnum must be used when scanning 
    # multiple file systems in order to uniquely 
    # identify the file.
    idnum=sprintf("%s %s", inum, device) 

    links=$4
    uid=$5
    gid=$6
    fsize=$7

    day=$8
    month=$9
    dayofmonth=$10
    time=$11
    year=$12
    # access=sprintf("%s %s %s %s %s", 
    #    day, month, dayofmonth, time, year)

    fname=$13
    #_end template

    # keep track of uids
    who[ uid ] = 0

    # count number of files owned by this user
    numfiles[ uid ] += 1
    tot_files += 1

    # keep track of inode numbers so we do not count the 
    # same ones twice.  if the file is linked, do not 
    # count it again.  we know it is linked if we run 
    # into the same inode/device (idnum) number.

    if ( inumber[ idnum ] == "" ) {

        whosize [ uid ] += fsize
        tot_size += fsize

        inumcount[ uid ] += 1
        tot_inodes += 1
    }

    inumber[ idnum ] = 1
}
function commas(num,count,i,str)
{
    count=1
    for(i=length(num); i>0; i--) {
        str=sprintf("%s%s", str, substr(num,i,1))
        if ( count++ == 3 && i > 1) {
            count = 1
            str=sprintf("%s,",str)
        }
    }
    return ( revstr(str) )
}
function revstr(s,i,str)
{
    for (i=length(s); i>0; i--)
        str=sprintf("%s%s", str, substr(s,i,1))
    return str
}
END {
    printf("    uid     num files      total size  ")
    printf("---------- ----------- ----------------")
    printf("  num inodes     %\n")
    printf(" ------------ -------\n")

    for ( userid in who ) {
        printf ("%10s %11s %16s %12s %7.3f\n",
          userid, commas(numfiles[userid]),
          commas(whosize[userid]),
          commas(inumcount[userid]),
          (100.00 * numfiles[userid]) / tot_inodes )
    }
    printf("           ----------- ")
    printf("---------------- ------------\n")
    printf("           %11s %16s %12s\n", commas(tot_files),
        commas(tot_size), commas(tot_inodes))
}'
exit 0

