#!/bin/bash

###########################################################################
#
#	Shell program to output the size of one or more files.
#
#	Copyright 2000-2002, William Shotts <bshotts@users.sourceforge.net>.
#
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the GNU General Public License as
#	published by the Free Software Foundation; either version 2 of the
#	License, or (at your option) any later version. 
#
#	This program is distributed in the hope that it will be useful, but
#	WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#	General Public License for more details.
#
#	This software is part of the LinuxCommand.org project, a site for
#	Linux education and advocacy devoted to helping users of legacy
#	operating systems migrate into the future.
#
#	You may contact the LinuxCommand.org project at:
#
#		http://www.linuxcommand.org
#
#	Description:
#
#	This program outputs the size (in bytes) of one or more specified
#	files.  Optionally, it can also display the name of the file (the
#	default is to output only the file size) and the total size of 
#	all the specified files. 
#
#	Usage:
#
#		sizeof [ -h | --help ] [-v] [-t] file...
#
#	Options:
#
#		-h, --help	Display this help message and exit.
#		-v              Verbose mode.  Outputs file name in
#				addition to file sizes.
#		-t              Total size of specified files.
#
#
#	Revisions:
#
#	08/27/2000	File created by lc_new_script v.2.0.6
#	02/18/2002	Cosmetic improvements and other updates (0.0.2)
#
#	$Id$
#
###########################################################################


###########################################################################
#	Constants
###########################################################################

PROGNAME=$(basename $0)
VERSION="0.0.2"


###########################################################################
#	Functions
###########################################################################


function clean_up
{

	#####	
	#	Function to remove temporary files and other housekeeping
	#	No arguments
	#####

	# Nothing needed
	return
}


function error_exit 
{
	#####	
	# 	Function for exit due to fatal program error
	# 	Accepts 1 argument
	#		string containing descriptive error message
	#####

	
	echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2
	clean_up
	exit 1
}


function graceful_exit
{
	#####
	#	Function called for a graceful exit
	#	No arguments
	#####

	clean_up
	exit
}


function term_exit
{
	#####
	#	Function to perform exit if termination signal is trapped
	#	No arguments
	#####

	echo "${PROGNAME}: Terminated"
	clean_up
	exit
}


function int_exit
{
	#####
	#	Function to perform exit if interrupt signal is trapped
	#	No arguments
	#####

	echo "${PROGNAME}: Aborted by user"
	clean_up
	exit
}


function usage
{
	#####
	#	Function to display usage message (does not exit)
	#	No arguments
	#####

	echo "Usage: ${PROGNAME} [-h | --help] [-v] [-t] file..."
}


function helptext
{
	#####
	#	Function to display help message for program
	#	No arguments
	#####
	
	local tab=$(echo -en "\t\t")
		
	cat <<- -EOF-

	${PROGNAME} ver. ${VERSION}	
	This is a program to output the size of one or more files.
	
	$(usage)
	
	Options:
	
	-h, --help	Display this help message and exit.
	-v              Verbose mode.  Outputs file name in
			${tab}addition to file size.
	-t              Total size of specified files.

-EOF-
}	


###########################################################################
#	Program starts here
###########################################################################
	
##### Initialization And Setup #####

# Set file creation mask so that all files are created with 600 permissions.

umask 066

# Trap TERM, HUP, and INT signals and properly exit

trap term_exit TERM HUP
trap int_exit INT


##### Command Line Processing #####

if [ "$1" = "--help" ]; then
	helptext
	graceful_exit
fi

verbose_mode=1	# default is false
total_mode=1	# default is false

while getopts ":hvt" opt; do
	case $opt in
		v )	verbose_mode=0 # set to true
			;;
		t )	total_mode=0 # set to true
			;;
		h )	helptext
			graceful_exit ;;
		* )	usage
			clean_up
			exit 1
	esac
done

# Shift past the options to get to the file names

if [ $verbose_mode = "0" ]; then
	shift 1
fi
if [ $total_mode = "0" ]; then
	shift 1
fi

# If no files are specified, display usage message and exit

if [ "$1" = "" ]; then
	usage
	clean_up
	exit 1
fi

##### Main Logic #####

total=0
while [ "$1" != "" ]; do
	if [ -f "$1" ]; then
		if [ $total_mode = "0" ]; then
			total=$((total + $(ls -l "$1" | awk '{ print $5 }')))
		fi
		if [ $verbose_mode = "0" ]; then
			ls -l "$1" | awk '	# Start of awk program
				NF == 9 {	
						printf("%10d\t%s\n", $5, $9)
					}
				
				NF > 9  {	# Deal with filenames with embedded spaces
				
						filename = $9
						for (i=10; i<= NF; i++)
						{
							filename = filename " " $i
						}
						printf("%10d\t%s\n", $5, filename)
					}
						# End of awk program '
						
		elif [ $total_mode = "1" ] ; then
			ls -l "$1" | awk '{ print $5 }'
		fi
	fi
	shift 1
done

if [ $total_mode = "0" ]; then
	printf "%10d\n" $total
fi

graceful_exit



syntax highlighted by Code2HTML, v. 0.9.1