Out: Feb 7, 10
Due: Feb 28, Mar 1 (date of your lecture, start of class)
Reminder – It’s a 2 week assignment with reading week added on

CENG251: Lab 3: The Little Functions

Most of these exercises involve writing a short program to call a kernel function that does much of the same work as the command line version. Write the code and test it to make sure that it works – you should embed a test inside each program, ie: check the value of errno after calling the kernel function and verify the contents of the directory. (No cheating by using the system() call)
Note that you will need to give your program a different name to avoid using the existing Unix command as it is first in your PATH. Just to be safe add the following line to each program:
fprintf(stdout,”The name of this program is: %s\n”, argv[0]);
If the program does not print out its name then you are not executing your own program.

Part I (16 marks)
Write small programs to perform each of the following. All arguments must be passed from the command line and your testing should show at least 1 example of a success and a failure. Your answers for #1,#2,#5 & #6 should work on multiple files by looping through the entries in argv, just like the echo command. renameDemo.c is provided as an example.

  1. Write a small program that uses unlink to remove multiple files from their directories.
    Test by removing a hard link, a symbolic link, a directory and a file from a directory that you don't have write permission to. (2 marks)
  2. Write a small program that implements chmod where the file permissions are expressed as an octal number, ie: myMod 0612 file1 file2 …..
    To convert the octal string to an octal number, use the sscanf trick covered in class. (2 marks)
  3. The ln command is used to create both hard and symbolic links. It takes 2 files and the flags –s and –f . Implement a version of ln as follows using a small getopt loop. (4 marks - getopt_long is not needed here)
  4. if the -s flag appears then the command is used to create a symbolic link using the symlink function call. Otherwise the command uses link to create a hard link.
  5. If the –f flag appears then this means force the creation of the link. Do this by deleting the second file first, then creating the link.
  6. Write a small program that uses mktemp from section 3 of the manual to create a temporary file in the same manner that mktemp from section 2 of the manual does, without implementing any of the flags. Use the stat function to verify that the file was created. (2)
  7. Write a small program that creates multiple directories using mkdir(dirName, mode) on each of the files contained in argv. The mode should be a defined constant brought in from sys/stat.h – a list of these is on pp 107 of Robbins and or you can use man 2 open. Set the permissions of the directory so that it is readable, writable and executable by the owner and navigable (ie: executable) by others. Before calling mkdir you will have to call the function umask(0) otherwise your program will not have full permission to set permissions. (2 marks)
  8. Write a small program that uses rmdir to remove a directory. Show that it works. Test by removing an empty and a non-empty directory. Record the value of errno and the message generated by perror when the function fails. (2 marks)
  9. Write a small program to save and print out the name of the current working directory using the getcwd function. Use chdir to move to a different directory and then print out the name of that directory as well. (2 marks)

Part II

  1. Write a program that applies fstat to each of the files listed in the program’s command line and reports on each of the following: (8 marks)
  2. The number of entries that represent files that actually exist vs those that do not.
  3. The number of entries that are executable by you.
  4. The number of symbolic links, regular files and directories.
  5. The total size of all the files in bytes and in blocks
  6. The earliest and latest modification times presented both as the number of seconds and as a string.
  7. The total # of read entries in all the permission fields. ie: If the permissions are rw—wxr-x - count 2. r—r—r-- count 3, ------count 0 and so forth.
  8. Your test should consist of at least 10 arguments so that all of the above features are tested. Document your list of files indicating which feature(s) they test. (8 marks)
  9. In this exercise you are going to create a data structure similar to argv and envp out of the PATH environment variable. Write a program that: (4 marks)
  10. Loads the PATH environment variable into your program.
  11. Loop through the string and count the number of colons. We will discuss different strategies in class.
  12. Use calloc to allocate an array of pointers to character sufficiently large to point to all the individual directories + 1 extra location for a null value to end the list.
  13. Loop through the string a 2nd time, thiis time setting the pointers to the start of each string and replacing the colons with null.
  14. After you’ve finished creating the array of string points, loop through the array and print out the entire list of directories, 1 per line.