Last Updated: 2021-03-01 Mon 08:29

CSCI 4061 Lab06: File Stat via stat() / lstat()


  • Download the code distribution
  • See further setup instructions below


1 Rationale

Gathering basic file statistics is accomplished in Unix systems through the stat() system call or its close relation lstat(). These are very compact system calls which utilize a struct statbuf to capture results and a series of macros to parse out information about a file. This lab briefly surveys them.

NOTE: There is no coding component to this lab, only a make test-quiz component as obtaining file information is very specific to file systems and difficult to automate in testing.

Grading Policy

Credit for this Lab is earned by completing the exercises here and submitting a Zip of the work to Gradescope. Students are responsible to check that the results produced locally via make test are reflected on Gradescope after submitting their completed Zip. Successful completion earns 1 Engagement Point.

Lab Exercises are open resource/open collaboration and students are encouraged to coopearte on labs. Students may submit work as groups of up to 5 to Gradescope: one person submits then adds the names of their group members to the submission.

See the full policies in the course syllabus.

2 Codepack

The codepack for this lab is linked at the top of this document. Always download it and unzip/unpack it. It should contain the following files which are briefly described.

File Use Description
QUESTIONS.txt EDIT Questions to answer: fill in the multiple choice selections in this file.
filestats.c Study C file to study to answer QUIZ questions
strmode.c Study Utility function to print a permisssions string for files
QUESTIONS.txt.bk Backup Backup copy of the original file to help revert if needed
Makefile Build Enables make test and make zip
testy Testing Test running scripts Testing Tests for this lab

3 QUESTIONS.txt File Contents

Below are the contents of the QUESTIONS.txt file for the lab. Follow the instructions in it to complete the QUIZ and CODE questions for the lab.


                            LAB 06 QUESTIONS

Lab Instructions

  Follow the instructions below to experiment with topics related to
  this lab.
  - For sections marked QUIZ, fill in an (X) for the appropriate
    response in this file. Use the command `make test-quiz' to see if
    all of your answers are correct.
  - For sections marked CODE, complete the code indicated. Use the
    command `make test-code' to check if your code is complete.
  - DO NOT CHANGE any parts of this file except the QUIZ sections as it
    may interfere with the tests otherwise.
  - If your `QUESTIONS.txt' file seems corrupted, restore it by copying
    over the `QUESTIONS.txt.bk' backup file.
  - When you complete the exercises, check your answers with `make test'
    and if all is well, create a zip file with `make zip' and upload it
    to Gradescope. Ensure that the Autograder there reflects your local
  - IF YOU WORK IN A GROUP only one member needs to submit and then add
    the names of their group.

QUIZ Questions on switch_stdout.c

  Analyze the `filestats.c' program. Compile and run it via
  | > make filestats
  | ...
  | > ./filestats somefile.txt
  | ...
  | > ./filestats a_dirname/

  You may wish to create some symbolic links to files as well and run
  `filestats' on the links to see the behavior using the file.
  | > touch myfile.txt
  | > ln -s myfile.txt symlink_to_myfile.txt
  | > ./filestats myfile.txt
  | ...
  | > ./filestats symlink_to_myfile.txt

  Finally, you can contrast the behavior of `filestats.c' to the shell
  command `stat' which provides similar functionality.

  Answer the following questions about how the system call works.

stat() vs lstat()

  Consult the manual pages / documentation and choose which of the
  following best describes the difference between `stat()' and
  - ( ) `stat()' reports integer data / size on files while `lstat()'
    will report "long" data in the form of `long' return types
  - ( ) `stat()' will follow symbolic links and report data on the
    linked file while `lstat()' will report information on the link
  - ( ) `stat()' reports all information on a file whereas `lstat()' is
    faster but "l-imited" in the information it returns.
  - ( ) `stat()' immediately produces the data on a file while `lstat()'
    is a "lazy" call which will only complete when the data is actually

File Size

  How does `stat() / lstat()' report the Size of a file?
  - ( ) An integer is passed to the calls to be set to the size
  - ( ) The return value is the number of bytes in the file
  - ( ) The size is printed to standard output during the function call
  - ( ) The field `sb.st_size' contains the number of bytes in file

File Kind

  `stat() / lstat()' report the "kind" of file being queried. How is
  this information used in `filestats.c'?
  - ( ) The field `sb.st_filetype' is set to a string like "file" or
    "pipe" to indicate the type of a file and that field can be printed
  - ( ) An integer passed in as an address to the call is set to the
    type of the file
  - ( ) The kind is encoded in the `sb.st_mode' field of the struct and
    macros are used to distinguish the kind and print an appropriate
  - ( ) The kind is encoded in the `sb.st_mode' field of the struct and
    functions are used to distinguish the kind and print an appropriate

  Which one of the following is NOT a file "kind" reported by `stat() /
  - ( ) Directory
  - ( ) Binary
  - ( ) Socket
  - ( ) Symbolic Link (symlink)

mtime / ctime

  Do some research and determine the difference between the `ctime' and
  `mtime' alteration times that are reported by `stat() /
  lstat()'. Which of the following best describes this difference:
  - ( ) `mtime' and `ctime' actually always report the same time and
    their redundancy is a historical oddity.
  - ( ) `mtime' indicates the last time that the file was moved from one
    directory to another while `ctime' indicates the last change to the
    data in the file
  - ( ) `mtime' is the last time of modification when data was altered
    in the file while `ctime' is the last access time when data was read
    from the file
  - ( ) `mtime' is when the actual data of a file changes while `ctime'
    is associated with permissions, links, or other meta data changes
    associated with the file.

Additional Items to Observe

  The function `ctime()' and `strmode()' both use an interesting
  technique to make it possible to easily produce a printable string for
  situations like those in `filestats.c'.  Time permitting, examine the
  source code for `strmode()' in `strmode.c' and discuss with a TA how a
  string is returned but there is no requirement to free that string.


  None for this lab

4 Submission

Follow the instructions at the end of Lab01 if you need a refresher on how to upload your completed lab zip to Gradescope.

Author: Chris Kauffman (
Date: 2021-03-01 Mon 08:29