CIS 33A Programming in PERL
Assignment F

Please staple the assignment together. At the top of the first page print, in this order: your first name, your last name, CIS33A, and the assignment letter. Print the problem number at the beginning of each problem. Assignments are due at the beginning of class, and will be marked down if turned in later.

Authorship note: These assignments have been adapted from assignments given by Clare Nguyen, who adapted them from the work of Behrouz Forouzan.

Programming Problems are to be done on the computer. Hand in your your source code and program output.

Programming problems

Problem F1

Write a program that takes any number of directory names as command line arguments and an output filename as a last argument. The program checks that there are at least 2 command line arguments, at least one directory name and one output file name. The program produces one number for each directory -- the total number of bytes of all files in that directory, or zero if the directory could not be opened. Warn the user if a directory could not be opened and then move on to the next directory. Print all good output to the output file. Sample output:

$ lab6.pl dir1 ../dir2 dir3 outf
cannot open dir3:No such file or directory
$ cat outf
dir1: 4310 total bytes.
../dir2: 6579 total bytes.
dir3: 0 bytes.
$ ls -la dir1 | awk '{b += $5} END {print b}'
4310
$ ls -la ../dir2 | awk '{b += $5} END {print b}'
6579

Check the result of your program with the UNIX ls and awk commands as shown. Turn in the check code and results, as well as the perl code and results.

Problem F2

Write a program that accepts a directory name on the command line. The program checks that there is exactly one command line argument, then calls a sub. The sub finds all files in the directory that have 700 access permission (the owner has full permission, group and other users have no permission), and changes the access rights for these files so that everyone else can read the file (744 permission). The sub then prints the name and new access rights of all the changed files.

For added challenge, you may write this program without using a loop. Sample output:

$ ls -l dir1
total 5
-rw-rw-rw-    1 ucn2140  staff       1970 Mar  3 16:24 lab3
-rw-rw-rw-    1 ucn2140  staff        298 Mar  3 16:24 lab3.soln1
-rw-rw-rw-    1 ucn2140  staff        306 Mar  3 16:24 lab3.soln2
-rwx------    1 ucn2140  staff        553 Mar  3 17:21 lab4.soln1
-rwx------    1 ucn2140  staff        180 Mar  3 17:21 lab4.soln2
$ lab6.soln2 dir1
dir1/lab4.soln1      744
dir1/lab4.soln2      744
$ lab6.soln2
Usage: lab6.soln2 dirname

Turn in directory listings before and after your program runs, as well as the perl code and results.