Week 08 Tutorial Questions
Objectives
- introduce methods of accessing and manipulating files in C
- introduce UNIX file metadata and how it is accessed in C
- develop an understanding of how the UNIX file permissions system works
-
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:
What is the full pathname of COMP1521's
web
directory?Which directory is
~jas/../..
?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?
What kind of filesystem object is
cat
?What kind of filesystem object is
home
?What kind of filesystem object is
tty0
?What kind of filesystem object is a symbolic link? What value does it contain?
Symbolic links change the filesystem from a tree structure to a graph structure. How do they do this?
-
What does fopen(3) do? What are its parameters?
-
What are some circumstances when fopen(3) returns NULL?
-
How do you print the specific reason that caused fopen(3) to return
NULL
? -
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 tostdout
. If given an incorrect number of arguments, or if there was an error opening the file, it should print a suitable error message. -
Write a C program,
write_line.c
, which is given one command-line argument, the name of a file, and which reads a line fromstdin
, and writes it to the specified file; if the file exists, it should be overwritten. -
Write a C program,
append_line.c
, which is given one command-line argument, the name of a file, and which reads a line fromstdin
and appends it to the specified file. -
Why should you not use fgets(3) or fputs(3) with binary data?
-
How many different values can fgetc(3) return?
-
Why are the names of fgetc(3), fputc(3), getc(3), putc(3), putchar(3), and getchar(3) misleading?
-
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.
-
The
stat()
andlstat()
functions both take an argument which is a pointer to astruct 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 thecs1521
group has group id 36820. -
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
Who can access the
17s2.work
directory?What operations can a typical user perform on the
public_html
directory?What is the file
web
?What is the difference between
stat("web", &info)
andlstat("web", &info)
?
(whereinfo
is an object of type(struct stat)
)
-
Explain how this program uses stat to get information about the permissions and type of a file. The inode man page may he helpful here.
Could you add another if statement to check if the file is publically writeable? How would you check if the file can be executed by the owner?#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> void print_some_file_info(char *filename); int main(int argc, char *argv[]) { // Loop over command line arguments for (int arg = 1; arg < argc; arg++) { print_some_file_info(argv[arg]); } } void print_some_file_info(char *filename) { // Use stat to get file information struct stat s; // check that stat worked if (stat(filename, &s) != 0) { perror(filename); return; } // get the mode (permissions) of the file mode_t mode = s.st_mode; // check if the file is a directory if ((mode & S_IFDIR) == S_IFDIR) { printf("The directory '%s' has mode 0x%x == 0%o\n", filename, mode, mode); // check if the file is a regular file } else if (S_ISREG(mode)) { printf("The file '%s' has mode 0x%x == 0%o\n", filename, mode, mode); } // check if the file is publically readable if (mode & S_IROTH) { printf("%s is publically readable\n", filename); } }
-
Write a C program, chmod_if_public_write.c, which is given 1+ command-line arguments which are the
pathnames of files or directories
If the file or directory is publically-writeable, it should change it to be not publically-writeable, leaving other permissions unchanged.
It also should print a line to stdout as in the example below
dcc chmod_if_public_write.c -o chmod_if_public_write ls -ld file_modes.c file_modes file_sizes.c file_sizes -rwxr-xrwx 1 z5555555 z5555555 116744 Nov 2 13:00 file_sizes -rw-r--r-- 1 z5555555 z5555555 604 Nov 2 12:58 file_sizes.c -rwxr-xr-x 1 z5555555 z5555555 222672 Nov 2 13:00 file_modes -rw-r--rw- 1 z5555555 z5555555 2934 Nov 2 12:59 file_modes.c ./file_modes file_modes file_modes.c file_sizes file_sizes.c removing public write from file_sizes file_sizes.c is not publically writable file_modes is not publically writable removing public write from file_modes.c ls -ld file_modes.c file_modes file_sizes.c file_sizes -rwxr-xr-x 1 z5555555 z5555555 116744 Nov 2 13:00 file_sizes -rw-r--r-- 1 z5555555 z5555555 604 Nov 2 12:58 file_sizes.c -rwxr-xr-x 1 z5555555 z5555555 222672 Nov 2 13:00 file_modes -rw-r--r-- 1 z5555555 z5555555 2934 Nov 2 12:59 file_modes.c
Make sure you handle errors.
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.