Week 08 Tutorial Questions

Objectives

  1. We say that the Unix filesystem is tree-structured, with the directory called / as the root of the tree, e.g.,

    Answer the following based on the above diagram:

    1. What is the full pathname of COMP1521's web directory?

    2. Which directory is ~jas/../..?

    3. Links to the children of a given directory are stored as entries in the directory structure. Where is the link to the parent directory stored?

    4. What kind of filesystem object is cat?

    5. What kind of filesystem object is home?

    6. What kind of filesystem object is tty0?

    7. What kind of filesystem object is a symbolic link? What value does it contain?

    8. Symbolic links change the filesystem from a tree structure to a graph structure. How do they do this?

  2. What does fopen(3) do? What are its parameters?

  3. What are some circumstances when fopen(3) returns NULL?

  4. How do you print the specific reason that caused fopen(3) to return NULL?

  5. Write a C program, first_line.c, which is given one command-line argument, the name of a file, and which prints the first line of that file to stdout. If given an incorrect number of arguments, or if there was an error opening the file, it should print a suitable error message.

  6. Write a C program, write_line.c, which is given one command-line argument, the name of a file, and which reads a line from stdin, and writes it to the specified file; if the file exists, it should be overwritten.

  7. Write a C program, append_line.c, which is given one command-line argument, the name of a file, and which reads a line from stdin and appends it to the specified file.

  8. For each of the following calls to the fopen() library function, give an open() system call that has equivalent semantics relative to the state of the file.

    1. fopen(FilePath, "r")
    2. fopen(FilePath, "a")
    3. fopen(FilePath, "w")
    4. fopen(FilePath, "r+")
    5. fopen(FilePath, "w+")

    Obviously, fopen() returns a FILE*, and open() returns an integer file descriptor. Ignore this for the purposes of the question; focus on the state of the open file.

  9. If a file xyz contains 2500 bytes, and it is scanned using the following code:

    int fd;         // open file descriptor
    int nb;         // # bytes read
    int ns = 0;     // # spaces
    char buf[BUFSIZ]; // input buffer
    
    fd = open ("xyz", O_RDONLY);
    assert (fd >= 0);
    while ((nb = read (fd, buf, 1000)) > 0) {
        for (int i = 0; i < nb; i++)
            if (isspace (buf[i]))
                ns++;
    }
    close (fd);
    

    Assume that all of the relevant #include's are done.

    How many calls with be made to the read() function, and what is the value of nb after each call?

  10. Why should you not use fgets(3) or fputs(3) with binary data?

  11. How many different values can fgetc(3) return?

  12. Why are the names of fgetc(3), fputc(3), getc(3), putc(3), putchar(3), and getchar(3) misleading?

  13. Write a C program, fgrep.c, which is given 1+ command-line arguments which is a string to search for.

    If there is only 1 command-line argument it should read lines from stdin and print them to stdout iff they contain the string specified as the first command line argument.

    If there are 2 or more command line arguments, it should treat arguments after the first as filenames and print any lines they contain which contain the string specified as the first command line arguments.

    When printing lines your program should prefix them with a line number.

    It should print suitable error messages if given an incorrect number of arguments or if there is an error opening a file.

  14. The stat() and lstat() functions both take an argument which is a pointer to a struct stat object, and fill it with the meta-data for a named file.

    On Linux, a struct stat contains the following fields (among others, which have omitted for simplicity):

    struct stat {
        ino_t st_ino;         /* inode number */
        mode_t st_mode;       /* protection */
        uid_t st_uid;         /* user ID of owner */
        gid_t st_gid;         /* group ID of owner */
        off_t st_size;        /* total size, in bytes */
        blksize_t st_blksize; /* blocksize for filesystem I/O */
        blkcnt_t st_blocks;   /* number of 512B blocks allocated */
        time_t st_atime;      /* time of last access */
        time_t st_mtime;      /* time of last modification */
        time_t st_ctime;      /* time of last status change */
    };
    

    Explain what each of the fields represents (in more detail than given in the comment!) and give a typical value for a regular file which appears as follows:

    ls -ls stat.c
    8 -rw-r--r--  1 jas  cs1521  1855  Sep  9 14:24 stat.c
    

    Assume that jas has user id 516, and the cs1521 group has group id 36820.

  15. Consider the following (edited) output from the command ls -l ~cs1521:

    drwxr-x--- 11 cs1521 cs1521 4096 Aug 27 11:59 17s2.work
    drwxr-xr-x  2 cs1521 cs1521 4096 Aug 20 13:20 bin
    -rw-r-----  1 cs1521 cs1521   38 Jul 20 14:28 give.spec
    drwxr-xr-x  3 cs1521 cs1521 4096 Aug 20 13:20 lib
    drwxr-x--x  3 cs1521 cs1521 4096 Jul 20 10:58 public_html
    drwxr-xr-x 12 cs1521 cs1521 4096 Aug 13 17:31 spim
    drwxr-x---  2 cs1521 cs1521 4096 Sep  4 15:18 tmp
    lrwxrwxrwx  1 cs1521 cs1521   11 Jul 16 18:33 web -> public_html
    
    1. Who can access the 17s2.work directory?

    2. What operations can a typical user perform on the public_html directory?

    3. What is the file web?

    4. What is the difference between stat("web", &info) and lstat("web", &info)?
      (where info is an object of type (struct stat))

Revision questions

The following questions are primarily intended for revision, either this week or later in session.
Your tutor may still choose to cover some of these questions, time permitting.