Question
Part II: The Little Functions (18 marks/3 each) The point of this assignment is for you to try out and test C functions that are
Part II: The Little Functions (18 marks/3 each) The point of this assignment is for you to try out and test C functions that are the equivalent of Unix shell commands. Create and test a series of short demonstration programs in C for the following. Each of the 7 programs, excluding include statements, comments (include FHEAD information) and main will likely be 4-10 lines of code. Only use the system function for verification at the end. You may want to keep 2-3 window panels open in vim so you can work on the exercises in parallel. Where the problem specifies that you process multiple files or directories these should be provided as a list on the command line. Document your command line tests and your program status in the FHEAD comment at the top of each program as opposed to a separate writeup. Also make sure that when you hand in the program listings they are labelled and in the correct order. When you are asked to use the perror function to test the success of the function, set errno back to zero immediately after as it does not always automatically reset on its own. Each of these is worth 3 marks. All are candidates for testing on the midterm. 1. Use the link and symlink functions to create both a hard link and a symbolic link to a file. Verify the success of each function call using perror. The command line for your program should be: cmd filename hardLinkName symbolicLinkName These will correspond to argv[1], argv[2] and argv[3]. Assign each of these to meaningful variable names. Then turn the 2nd argument into a hard link reference and the 3rd argument into a symbolic link. Do the following test cases using different file names for each case: a. filename exists and the other two files dont. b. The filename exists and the other two files also exist. c. None of the files exist. Summarize your results for each case indicating whether or not you were successful in creating the hard link and/or the symbolic link and whether your result was reasonable. 2. Use access, creat, chmod and unlink as follows: Pass the name of a file to your program. Test whether or not it exists. Report on the result. If it doesnt exist, create it and test again to make sure that your file creation was successful. Use the chmod function to turn on rwx for groups and other, but turn all 3 off for yourself using the symbolic constants listed under chmods man page or on page 295 of Kerrisk. Test using access whether or not you are allow to read, then write and then execute the file. Your tests should report back an answer that you cant. Using chmod again, turn on read and execute for yourself. Repeat the tests for read, write and execute using access again. Summarize the results? 3. Before writing this program note the group id # of your alternate group (sharks, minnows, dolphins) using the id command. Implement the following: cmd file1 file2 file3 file4 ..... a. Attempt to create each of the files listed, report on your success using perror. At least one of these files should previously exist. b. Use truncate to alter the size of each file to a random size between 20 and 100 bytes. then change the group ownership of these files to your alternate group. c. End your program by using the system along with the ls command to show that this worked. 4. Provide a list of 5 or more absolute and relative directory references on the command line to your program including the name of a directory that does not exist. Use getcwd to obtain and then print the name of the current directory. Use chdir to switch between the directories. Each time you switch use getcwd to confirm which directory the program thinks is the current directory and perror to confirm that chdir worked using perror. Were you successful? What happened to the current directory when you tried switching to the directory that did not exist? ie: cmd /usr $HOME public_html /home/badDir .. #please create your own test. 5. Provide a list of 3 (or more) regular filenames (not directories) on the command line to your program. One should be in the current directory, one should be a relative file reference to a file in another directory and the third should be an absolute file reference in a completely different directory. You can use any Unix directory under / but not in the root directory itself. Use realpath, dirname and basename to print out the full path of each file, the directory it occurs in and the name of the file without the directory itself. (Warning make a copy of the result of realpath as the textbook warns that dirname will alter the string. 6. Apply the stat function to a list of files on the command line. The last file listed should be a symbolic link. Print out the file name, size in bytes, the total amount of space it takes up on the disk (block size * the number of blocks) the inode # and the latest value of atime, mtime or ctime printed out as a string. Your program should state which times is the latest. Where two times are the same it doesnt matter which one you choose.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started