Article Listing 1 Listing 2 Listing 3 apr2005.tar

Listing 1 ls_floppy -- list contents of a floppy disk

#!/bin/ksh
# This script is executable only by a user with root privileges
# since only root can mount a device.

function get_device
{
   if [[ -n $1 ]]
   then
      case $1 in
      fd|diskette|rdiskette|floppy)
            print /dev/rdiskette
            ;;
      fd0|diskette0|rdiskette0|floppy0)
            print /dev/rdiskette0
            ;;
      fd1|diskette1|rdiskette1|floppy1)
           print /dev/rdiskette1
           ;;
      esac
   fi
}

function get_name
{
   if [[ -n $1 ]]
   then
      case $1 in
      fd|diskette|rdiskette|floppy)
         print floppy0
         ;;
      fd0|diskette0|rdiskette0|floppy0)
         print floppy0
         ;;
      fd1|diskette1|rdiskette1|floppy1)
         print floppy1
         ;;
      esac
   fi
}

function print_valid_nicknames
{
cat << EOF
valid nicknames are:
fd  diskette  rdiskette  floppy
fd0 diskette0 rdiskette0 floppy0
fd1 diskette1 rdiskette1 floppy1
EOF
}

# ls_floppy returns 0 unless a problem is encountered
return_code=0

# if no operands are supplied, use the default nickname
default_nickname=floppy

# assume that all command line arguments are options
# until we determine otherwise
options=$@

# these are the options for ls(1)
while getopts aAbcCdfFgilLmnopqrRstux1 option
do
   case $option in
      a|A|b|c|C|d|f|F|g|i|l|L|m|n|o|p|q|r|R|s|t|u|x|1) : ;;
      *) return 1 ;;
   esac
done

# move past the options
shift $(( $OPTIND - 1 ))

# check to see if any operands were supplied
if [[ -n $@ ]]
then
   # yes, operands exist
   nicknames=$@
   # so remove the operands from the options string
   options=${options%%$nicknames}
else
   # no, so use the default nickname
   nicknames=$default_nickname
fi

for nickname in $nicknames
do

   device=$(get_device $nickname)
   name=$(get_name $nickname)
   dir=/floppy/$name

   if [[ -z $device ]] || [[ -z $name ]]
   then
      print "invalid nickname: $nickname" >&2
      print_valid_nicknames >&2
      continue
   fi

   # make sure we are not in $dir, otherwise we won't be
   # able to unmount the device

   if [[ $(pwd) = $dir ]]
   then
      print "cd from $dir to another directory first!"
      return 1
   fi

   # the eject command prevents problems with volcheck, which
   # can report that media is available when it is not

   eject $name 1>&- 2>&-

   if volcheck -v $device >&2
   then
      return_code=1
      continue
   fi

   if ! volrmmount -e $name
   then
      print "failed to eject $nickname" >&2
      return_code=1
      continue
   fi

   if ! volrmmount -i $name
   then
      print "failed to insert $nickname" >&2
      return_code=1
      continue
   fi

   # cd to $dir (a directory associated with the floppy disk)
   # to avoid dealing with symbolic links when we execute ls

   if ! cd $dir
   then
      print "failed to cd to $dir" >&2
      return_code=1
      continue
   fi

   # list contents of the floppy
   ls $options
done

return $return_code