Search for question
Question

Q1 – About File Handling on Unix (6%)

a) Based on the sample program #3 (Prog3_stat_chmod.c) of Mod-1, an ELEC 3543 student wrote the following C program named as “Ass1.c” to demonstrate the uses of chmod(…) and stat(…) to change file permissions on Unix.

#include

#include

#include

int

main(void)

{

struct stat statbuf;

/* get the stat record of the file "chmod-TST.log" */

if (stat("chmod-TST.log", &statbuf) < 0)

perror("stat error for chmod-TST.log");

if (chmod("chmod-TST.log",

(statbuf.st_mode & ~S_IXGRP & S_IXOTH)) < 0)

perror("chmod error for chmod-TST.log");

if (chmod("chmod-TST.log", S_IRUSR | S_IWUSR | S_IRGRP |

S_IXGRP | S_IWOTH | S_IXOTH ) < 0)

perror("chmod error for chmod-TST.log");

return 0;

}

After the successful compilation of the above C program into “Ass1.exe” on the Cygwin platform, the student typed in the following commands/lines:

$ cat - > chmod-TST.log

Hi, it is a chmod test

$ chmod 726 chmod-TST.log

$ ./Ass1

$ ls –al chmod-TST.log

In the space provided as below, clearly state the resulting file permissions of “chmod-TST.log”, e.g. “r-- r-x --x”, as shown by the ls command. (2 marks)

Answer:

b) A student enrolled in ELEC 3543 modified the Python program “os_lseek_prog.py” [specified on Page 11 of the “Integrated LAB #1”] as “os_lseek_prog1.py” to display all the results in the decoded byte-strings as follows.

$ python os_lseek_prog1.py

Read String is : This is a test on lseek()

Read String is : s a test on lseek()

Read String is : eek()

Modify the C program “lseek_prog.c” [also specified on Page 11 of the “Integrated LAB #1”] as “lseek_prog_fix.c” so that the same output can be generated by calling the lseek(..) and read(..) function in C.

$ ./lseek_out_fix

Read String is : This is a test on lseek()

Read String is : s a test on lseek()

Read String is : eek()

In the space provided below, show the COMPLETE program code of your modified C program (“lseek_prog_fix.c”).

Note that no mark will be given if any of the output strings is hard-coded in your modified C program.