Description: The test command line utility on UNIX and UNIX-based platforms checks a condition and exits with zero if the condition succeeds or 1 if the condition fails:

test EXPRESSION

or the shorthand

EXPRESSION

Assignment: In this programming assignment, you will familiarize yourself with the Unix test command utility and then implement a customized simplified version which we will name cstest.

Command line options: In our cstest utility EXPRESSION takes the following forms:

-N EXPRESSION Negate EXPRESSION (true -> false, false -> true)
-d FILE FILE exists and is a directory
-e FILE FILE exists
-f FILE FILE exists and is a regular file
-r FILE FILE exists and read permission is granted
-w FILE FILE exists and write permission is granted
-x FILE FILE exists and execute permission is granted
FILE1 -n FILE2 FILE1 is newer (modification date) than FILE2
-z VALUE VALUE is "0" or an empty string
EXPRESSION1 EXPRESSION2 EXPRESSION1 and EXPRESSION2 are tested

When multiple conditions are given as shown in the last row of the table above then all must succeed, for example:

./cstest -f somefile -r some file -w some file

This example tests if somefile is a file that is readable and writeable.

Note that -N is an option that takes no option argument. This option indicates that the next EXPRESSION's logic is inverted, for example:

./cstest -f somefile -N -r somefile -N -w somefile -x somefile

This example tests if somefile is a file that executable but is not readable and not writeable. Note that -N applies to a single condition.

Exit code: the cstest utility should exit with EXIT_SUCCESS if all conditions succeed and EXIT_FAILURE otherwise. If no conditions to cstest are given then cstest should exit successfully (i.e. empty conditions are true).

To parse EXPRESSION you should use getopt(3), see the man page of getopt(3). The getopt function parses the command line options using a specified option string.

Sample C code: Use the following example to study and implement options and arguments parsing. The example takes option -N without option argument and option -f with a FILE argument. It also parses arguments that are not options, when placed between groups of options. You will need this to handle argument FILE1 in FILE1 -n FILE2.

#include < unistd.h>
#include < stdio.h>
#include < stdlib.h>

void usage ()
{
fprintf(stderr, "Usage: cstest EXPRESSIONn");
exit (EXIT FAILURE);
}

int main(int argc, char **argv)
{
int Nflag, ch;

Nflag = 0;
while (1) {
while ((ch = getopt (argc, argv, "Nf:")) != -1) {
switch (ch) { case 'N':
Nflag = 1;
break; case 'f':
printf("-f %sn", optarg); // print (as an example) // handle option -f here
break; case ':':
fprintf(stderr, "option - %c requires an argument", optopt); usage();
break; case '?': default:
fprintf(stderr, "unrecognized option -%cn", optopt); usage();
}
}
if (optind >= argc)
break; printf ("argument %sn", argv[optind] ); // print (as an example) // handle argument here ++optind; // next argument optreset = 1; // reset to start parsing next group of options
return 0;
}

Your version of cstest should not print anything to standard output. To test the attributes of a file, use stat(2) which populates a structure named 'stat' with file modes and modification time. You can use S_ISREG(m) to test the mode bits m to determine if the file is a regular file. You can use other macros for other purposes. See the stat(2) manual page for details.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.