CSC 357 Programming Assignment 4
sfind -- A Simplified Find Utility



ISSUED: Monday, 7 May 2007
DUE: On or before 11:59:59PM Monday 21 May, via handin on falcon/hornet
POINTS POSSIBLE: 100
WEIGHT: 8% of total class grade
READING: Lecture Notes Week 5, Stevens Chs 4 & 5,find, nftw man pages

Specification

In this assignment, you are writing a simplified version of the very useful UNIX find command. The following excerpt, from the UNIX man page, summarizes find's operation:

Usage:
    find path... expression

The find utility recursively descends the directory hierarchy for each path,
seeking files that match a Boolean expression written in the expression
primaries given below.

The path argument(s) are the starting point in a directory hierarchy. The first argument that starts with a - or !, and all subsequent arguments, are interpreted as an expression made up of the primaries and operators listed below.

In other words, find searches for files in a directory hierarchy that meet certain search criteria.

The primaries in the simplified version of find are the following, which constitute a subset of those available in the full find command:

     -atime n        True if the difference between the file last access time
                     and the time find was started, rounded up to the next full
                     24-hour period, is n 24-hour periods.

     -ctime n        True if the difference between the time of last change of
                     file status information and the time find was started,
                     rounded up to the next full 24-hour period, is n 24-hour
                     periods.

     -exec command [argument ...];
                     True if the executed command returns a zero value as its
                     exit status.  Optional arguments may be passed to the
                     command.  The expression must be terminated by a
                     semicolon.  If {} appears anywhere in the command
                     arguments, it is replaced by the pathname of the current
                     file.  Command is executed from the directory from which
                     find was executed.

     -group gname    True if the file belongs to the group gname; the gname
                     cannot be numeric.

     -mtime n        True if the difference between the file last modification
                     time and the time find was started, rounded up to the next
                     full 24-hour period, is n 24-hour periods.

     -name pattern   True if the last component of the pathname being examined
                     matches pattern.  Special shell pattern matching
                     characters may be used as part of pattern.  Matches are
                     performed per the specification of the fnmatch system
                     function.

                     A backslash is used as an escape character within the
                     pattern. The pattern should be escaped or quoted when find
                     is invoked from the shell.

                     Name patterns match any files, including those beginning
                     with a '.'.  (This is in contrast to /usr/bin/find, but
                     consistent with some other versions of find, e.g.,
                     /usr/xpg4/bin/fin on Solaris.)

     -newer file     True if the current file has been modified more recently
                     than the argument file.

     -print          Always true.  Causes the current path name to be printed.

     -size n         True if the file is n blocks long (512 bytes per block).

     -type c         True if the type of the file is c, where c is one of d,
                     f, or l, for directory, plain file, or symbolic link,
                     respectively.

     -user uname     True if the file is owned by the user uname, which cannot
                     be numeric.

For -atime, -ctime, -mtime, and -size, the n argument can be preceded by a plus or minus sign, with the following interpretation:
+n time or size is greater than n
n time or size is exactly n
-n time or size is less than n

If two or more primaries are present, they are combined by logical and. Any of the primaries can be preceded by a space-delimited '!', indicating Boolean negation of the primary. The '!' has higher precedence than and'ing.

If no expression is given, -print is used by default. If none of the expressions is -exec or -print, then -print is performed by default as the last primary.

Examples

Write out the names of all files and directories in the current working directory:

sfind .
sfind . -print
Note that -print is the default, if not explicitly specified.

Find all files named "*.c":
sfind . -name \*.c
Note the use of the backslash, so that '*' is sent to sfind rather than being expanded by the UNIX shell.

Find all files named "*.c" that are newer than a file named "x":
sfind . -name \*.c -newer x


Find all files not named "*.o", that are newer than a file named "x", and owned by user gfisher:
sfind . \! -name \*.o -newer x -user gfisher
Note the use of the backslash for !, again so it is not expanded by the shell.

Find all symbolic link files and execute "ls -sal" for each:
sfind . -type l -exec ls -sal {} ;


Implementation Suggestions

The following C library functions may be particularly useful in your implementation. You can read about these in Stevens and the man pages.

Function Description
nftw file tree traversal
fnmatch match shell-style regular expression patterns
getopts parse command-line options

Note that the default behavior of sfind is not to follow symbolic links. You must supply an appropriate argument to nftw to implement this behavior, i.e., not following symbolic links.

Deliverables

You must submit a set of .c and .h files plus a Makefile that compiles your program into an executable named sfind. The files must follow the 357 design and implementation conventions. If you want to provide any comments about your implementation, you may optionally include a plain-text README file.

Scoring Details

The testing plan file in the 357 programs/4/testing directory has the precise point breakdown.

Collaboration

NO collaboration is allowed on this assignment. Everyone must do their own individual work.

How to Submit the Deliverable

Submit your deliverable using the handin program on falcon/hornet. For Section 1 of 357, the command is

handin gfisher prog4 ... Makefile
where "..." are your program files.



index | lectures | labs | programs | handouts | solutions | examples | documentation | bin