#!/bin/sh -u # Extract and compile a C++ program contained in a tar archive #------------------------------------------------------------ # Syntax: # $0 [ tarfile [ progname ] ] #------------------------------------------------------------ # Purpose (mostly copied from the script specifications): # This script will expand a compressed (gzip) tar archive containing C++ # source code and compile the C++ source code for the given program name. # It will compare the size of the compiled program against a system program. # The user will be prompted to enter any missing command line arguments. # The script validates its arguments, prints error messages, and # exits non-zero on errors. Existing files are not overwritten, # they are renamed with ".bak" on the end. #------------------------------------------------------------ # Student Name: Ian! D. Allen # Algonquin EMail Address: alleni # Student Number: 000-000-000 # Course Number: DAT2330 # Lab Section Number: 010 # Professor Name: Ian! D. Allen # Assignment Name/Number/Date: Sample Exercise. # Comment: This is a sample assignment label. # see http://teaching.idallen.com/assignment_standards.html #------------------------------------------------------------ # Set a standard search path and non-restrictive umask. # PATH=/bin:/usr/bin ; export PATH umask 022 #********** # Input Section #********** # Exit if more than two arguments. # Prompt (always on stderr) and read any missing command line arguments. # if [ $# -gt 2 ]; then echo 1>&2 "$0: Expecting optional tar file and program name;" \ "found $# args ($*)" exit 1 fi if [ $# -lt 1 ]; then echo 1>&2 "Enter compressed tar file name:" read tarfile || exit 1 else tarfile="$1" fi if [ $# -lt 2 ]; then echo 1>&2 "Enter output program name:" read progname || exit 1 else progname="$2" fi # The name of the source file is derived from the command argument. # Put the derived name in a variable, so we only need to make *one* # change if we want to use a different source suffix (e.g. .c++). # source="$progname.cpp" # Echo the input arguments back to the user. # echo "Input tarfile: '$tarfile'" echo "Program name: '$progname'" echo "Source name: '$source'" #********** # Validation Section #********** # Exit if either argument is an empty string. # Make sure the tarfile is an existing, non-empty, readable plain file. # If not readable, add read permissions. # if [ -z "$tarfile" -o -z "$progname" ] ; then echo 1>&2 "$0: Empty tarfile or program name; nothing done. Bye." exit 2 fi if [ ! -f "$tarfile" ]; then echo 1>&2 "$0: Tar file '$tarfile' is missing, inaccessible, or not a file." exit 2 fi if [ ! -s "$tarfile" ]; then echo 1>&2 "$0: Tar file '$tarfile' is empty." exit 2 fi if [ ! -r "$tarfile" ] ; then echo 1>&2 "$0: Tar file '$tarfile' is not readable (fixed)." chmod +r "$tarfile" || exit 1 fi # Make sure the program name is contained in this tar archive. # if ! tar ztf "$tarfile" | grep "$source" >/dev/null ; then echo 1>&2 "$0: Cannot find '$source' in '$tarfile'." exit 3 fi #********** # Processing Section #********** # If the source file already exists, don't overwrite it. # Rename it to a backup name. # if [ -e "$source" ] ; then backup="$source.bak" echo 1>&2 "$0: Program name '$source' already exists." echo 1>&2 "$0: Renamed to '$backup'" mv "$source" "$backup" || exit 1 fi # Extract the entire tar archive. # (Optional: only extract the given progarm source file.) # if ! tar zxf "$tarfile" ; then echo 1>&2 "$0: Failed to extract from '$tarfile'." exit 4 fi # Make sure the source file is an existing, non-empty, readable plain file. # If not readable, add read permissions. # if [ ! -f "$source" ] ; then echo 1>&2 "$0: C++ source file '$source' is missing, inaccessible," \ "or not a file." exit 2 fi if [ ! -s "$source" ] ; then echo 1>&2 "$0: C++ source file '$source' is empty." exit 2 fi if [ ! -r "$source" ] ; then echo 1>&2 "$0: C++ source file '$source' is not readable (fixed)." chmod +r "$source" || exit 1 fi # If the compiled program already exists, don't overwrite it. # Rename it to a backup name. # if [ -e "$progname" ] ; then backup="$progname.bak" echo 1>&2 "$0: Program name '$progname' already exists." echo 1>&2 "$0: Renamed to '$backup'" mv "$progname" "$backup" || exit 1 fi # Compile the source file into the given program name. # if ! g++ "$source" -o "$progname" ; then echo 1>&2 "$0: Failed to compile '$source' into '$progname'." exit 5 fi #********** # Output Section #********** # Compare the size of the compiled program against a system program. # otherprog=/bin/echo mycount=$( wc -c <"$progname" ) || exit 1 sycount=$( wc -c <"$otherprog" ) || exit 1 if [ $mycount -lt $sycount ] ; then echo "Program $progname ($mycount) is smaller than $otherprog ($sycount)" elif [ $mycount -eq $sycount ] ; then echo "Program $progname ($mycount) is the same size as $otherprog" else echo "Program $progname ($mycount) is larger than $otherprog ($sycount)" fi # This script exits with the status of the last command executed.