Question
#!/usr/bin/XXXX #Script name:listdir.pl #Author: Bob D'Andrea #Course number: ITEC400 #Script functionality: program that will provide a listing of files in a directory. # -The files
#!/usr/bin/XXXX
#Script name:listdir.pl
#Author: Bob D'Andrea
#Course number: ITEC400
#Script functionality: program that will provide a listing of files in a directory.
# -The files must be listed in sorted order by the file name.
# -The file accept option l, l d
# -This file will call nooption if no option is entered.
# -This file will call loption file if l option is entered.
#Assignment number: 6-1
####################################################
# Sunny day scenarios
# Case #1: ./listdir.pl
# Case #2: ./listdir.pl -l
# Case #3: ./listdir.pl -d
# Case #4: ./listdir.pl -l -d
# Case #5: ./listdir.pl -l .
# Case #6: ./listdir.pl -d
#####################################################
# Rainy day scenario
# Case #1: ./listdir.pl -D
# Case #1: Output an error and usage message.
# Case #2: ./listdir.pl -d
# Case #2: Output an error and usage message.
#####################################################
#print out the header
# print `flush`;
# The permissions of test.txt must be set to 705
use IO::Handle;
open FH, "+>>", "test.txt" or die $!;
FH->autoflush(1);
thank(); #print thank header
XXX Getopt::Std; # Allow for the use of the options function
getopts("ld:") or usage() and exit; #accept only options l, and d
$doption = $opt_X; #store the d option to a variable
$loption = $opt_l; #store the l option to a variable
$newDIR;
#If there is no option is selected
if(!$loption)
{
nooption(); # call nooption file
}
#if option l is entered
elsif($loption)
{
loption(); # call loption
}
# if only d option is selected
else
{
#display usage and exit
print " ";usage() ;" ";
exit X1;
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SUB ROUTINES~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#openDIR subtoutin
# -If -d option is not entered, then open the current directory.
# -If -d option is entered, then check and open if it is a valid directory.
# -Store all the files in the specify directory to an array
sub openXXX
{
if($doption) #check if -d option is enterd
{
XXXXX($doption); #trim space
if(-d $doption) #check if it is a valid directory
{
chdir($doption);#change to specify directory
opendir($THISDIR,$doption);} #open directory
#print out error message when invalid directory is entered
else{print " \t ==>\"$doption\" is Invalid Directory. ";exit 1;}
}
else {opendir($THISDIR,".");}#open the specify directory
@DIRLIST = readdir($XXXXXXX);# read and store all files to an array
@DIRLIST = sort(@XXXXXXX; # Sort data in acending order
}
#nooption sub routine
# -print out the output when there is no option specify
sub XXXXXXXX
{
openDIR(); #call openDIR() method
print "File Names ";#print header
print "--------------- ";
#print out the result
forXXXX $result(@DIRLIST){if(-f $result){print "$result ";}}
closedir($THISDIR); #close directory.
exit 1;
}
#loption subroutine
XXX loption
{
openDIR(); #call openDIR() method
print " ";
#print header
printf ("\%-25s", " File Name");
printf ("\%7s", "Size");
printf " ";
printf ("\%-10s", "Owner");
printf ("\%-15s ", "Group");
printf ("\%-25s", "------------- ");
printf ("\%7s", "------");
printf " ";
printf ("\%-10s", "---------");
printf ("\%-15s ", "-----------");
#print output
XXXeach $result (@DIRLIST)
{
if(-X $result) # Verify that results contains a file
{
$size = -s $result;
$group = (stat($result))[5];
$group = getgrgid($group);
$user = (stat($result))[4];
$user = getpwuid($user);
#print out file name
printf ("\%-25s", "$result");
#File size = 7 space reserve, RIGHT Agliment
printf ("\%7s", "$size");
printf " ";
#File Owner = 10 spaces reserve, LEFT Agliment
printf ("\%-10s", "$user");
#File Group = 15 spaces reserve, LEFT Agliment
printf ("\%-15s ", "$group");
}
}
XXXXXXXX($THISDIR); # closedir
exit 1;
}
#This is the HERE document which is used to display error usage
sub XXXXX{
print STDERR << "EOF";
usage: $0 [-l][-d dir]
-l : Display the long list(File Name, Size, Owner and Group).
-d dir : scan directory.
NOTE: all the options have to start with '-' sign.
EOF
}
# thank subroutine
XXX thank
{
#print out the header and flush and clear the print buffers
# print `flush`;
use IO::XXXXXX;
open FH, "+>>", "test.txt" or die $!;
FH->autoflush(1);
print `clear`;
print " \t################################### ";
print "\t# Thank for using our script #";
print " \t################################### ";
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started