Computer Sciences Department logo

CS 368 (Summer 2009) — Day 9 Homework

Due Tuesday, July 28th, at the start of class.

Description

Implement a simple directory viewer, similar to "ls" or "dir", showing files by size.

Details

This program should read a directory name as a command-line argument. The program should scan the directory and for each file or directory report the file size in bytes. The list should be sorted by the size of the file. No size should be reported for directories; you may either entirely omit directories, or report some special value for the "size." The total size of the files (and only the files) should be reported at the end.

You may assume that there are only files and directories.

Example Output

% ./homework-09.pl /p/course/cs368-cat/public/html/09-stdlib/example-dir/
15000 large-file
<DIR> .
<DIR> ..
<DIR> subdir
29 aa
29 bb
29 cc
0 ccc
0 dd
15087 TOTAL

Hints

Like C, Perl provides "stat" for retrieving information about files. However, it can be a nuisance to use for some tasks, the so-called "-X" functions like "-f" may be easier to use. (If the previous link doesn't work, you can also review the -X functions with "perldoc -f -X".)

Reminders

Do the work yourself, consulting reasonable reference materials as needed; any reference material that gives you a complete or nearly complete solution to this problem or a similar one is not OK to use. Asking the instructors for help is OK, asking other students for help is not.

Hand In

A printout of your script on a single sheet of paper. At the top of the printout, please include “CS 368 Summer 2009”, your name, and “Homework 09, July 27, 2009”. Identifying your work is important, or you may not receive appropriate credit.