Out: Mon June 14, 2021 Due: Mon July 5th, 10 am. This is an absolute deadline. Midterm: Mon July 5th (Group A) and Wed July 7th (Group B) - During Lab Marked out of 32. 2 marks for a log and 2 for status reports on the 7 programs.

CENG251 – Assignment #3: The Little Functions

Part I: The Little Functions (20 marks)

The goal of this assignment is for you to try out and C functions that are the equivalent of shell commands. Each of the following asks you to between 4-12 statements of code. Use the system function only for verification, not to perform the task.

When the problem tells you process multiple files or directories these should be provided as a list on the line, not read in.

When you are asked to test the success or failure of a function, set errno to zero immediately after your test as it does not always automatically reset on its own.

You are encouraged to use the strace and ltrace commands to view the function calls used to implement the bash shell commands, how do not use the version of the commands. Do NOT give your commands the same name as the shell command as when you go to test the program you’ll be running the original command, not your code.

1. Use the , symlink and access functions to implement your own version of the link command: (4 marks) a. Write a getopt loop to capture the flags -s and -f which can appear at any position in the command line. (1) b. If -f is present, delete the target immediately after the getopt loop. (.5) c. After the loop, if -s is present in the command line, use symlink to create a symbolic link. If -s is not present then use the link function. (1) d. After attempting to create the link use all 3 of errno, perror and the return value from the link function to verify that your link function works. e. Use the system function to verify success or failure of the command. (1.5)

2. Use the C function to implement the equivalent of the octal version of the bash shell’s chmod command. (4 marks, 2 for code, 2 for testing)

YourChmod octalPerm file1 file2 file3 ......

After each attempt to change the permissions of the file use strerror to display the status of the change and compose a system function call to verify the change for each individual file. Test on the following in no particular order, all in one command line: a. A couple of regular files in two different directories b. A valid symbolic link that you own. c. A symbolic link that points to itself d. A directory or a file that you do not own e. A file or directory that does not exist.

3. Before writing this program note the group id # of your alternate group (sharks, minnows, dolphins) using the id command. Implement the following command: (3 marks)

altercmd groupid subdir/*

a. Use the bash shell to create approximated 8 files in your subdirectory b. Have your program use the C function to change the group ownership of all of the files to the groupid specified in the command line and use the truncate function to set the size of each file to a different random value between 100 and 500 bytes. Use the bash shell to verify success. c. Repeat the test using the student group id #.

4. Testing getcwd, chdir, , and realpath: (5 marks)

cmd file1 file2 file3 file4 ..... a. You should test this with at least 5 files though the program should be designed to handle any number of files. One file name should be in the current directory, another should be an absolute file reference to a file you own, the 3rd should be a relative file reference to a file you own in another directory. The 4th should be a reference to a file you do not own. The 5th should be a reference to a file that does not exist. (1) b. Use getcwd to determine the current directory and save the result in a string c. Use the strdup to a copy of each file name and then use the functions dirname, realpath and basename to extract all 3 parts of each file. Note that the order in which you do this may be important as basename and dirname alter the string that they are passed. (2) d. Use the result of realpath and dirname to to resulting directory. Use getcwd to verify that the changedir command worked then switch back to the original directory you saved in step b. (2 marks)

5. Write a C program createFiles dirName n that does the following: (4 marks) a. Use C to create a new directory with the name dirname and display the reason for the success or failure of the command. If the directory creation is successful or fails because the directory already exists create n new files in the directory using the C function mkstemp(template), otherwise the program with an appropriate error message. (1) b. Since mkstemp returns a file descriptor, use ftruncate to create to set the size of each file to a different random value between 50 and 100 bytes. (1) c. Use fchmod to set the file permissions to a random value between 0 and 511. (1) d. Use the access function to count how many files are readable by you, writeable by you and executable by you. (1) e. Provide a sample listing of 8 files to show that it works.

Part II: Binary File I/O (8 marks)

This set of exercises requires the flat file database of binary data that you generated in Assignment #2. If you were unsuccessful in this part of your assignment work with your lab instructor or a classmate so that you (not they) can get it to work.

1. Extracting records from a flat file database using file pointers.

Define 2 environment variables infile and outfile. Recall that the following command will generate a sequence of integers: start inc seq And that the follow command will shuffle the first n lines of any input seq start inc seq | shuf -n nItems

And that these values can be passed to any command, as demonstrated in class: showArgs `seq 2 3 70 | shuf -n 10`

Write a program that is passed a list of record numbers in random order on the command line and uses a combination of fseek, fread and frwrite to copy these records from infile to outfile. Open outfile in append mode that that you are always adding record. (3 marks)

2. Extracting records for a flat file database using file descriptors. a. Open infile for reading and writing. Obtain a file descriptor for manipulating the file. Use lseek to seek to the end of the file and report on the file size. (1) b. Have your program calculate the position of the 1st, last and middle records using the size of your database and the size of your datatype. Use pread to read them in one at a time and then use your display function from assignment #2 to show the records. (2) c. Use the command man proc to review the purpose of the maps file that is associated with every process. Use the mmap function to load all of infile into memory. Pause your program at this point by using gdb. Display the contents of the file /proc/[pid]/maps and then use gdp to display the return value of mmap and then add the length of the file to that. Demonstrate whether or not the information provided by gdb matches either entirely or partly the same values contained in the file /proc/[pid]/maps. (2)