CS810D Advanced Programming in the UNIX Environment Quiz 1 FULL NAME ______________________________________ USERNAME ______________________________________ 1) Which of the following is NOT (normally) found in the inode information: (3 points) a) timestamp of file creation b) file name c) pointer to parent directory d) pointer to next file in directory e) all of the above d) none of the above Correct answer: (e) all of the above 2) Consider the following directory listing: $ ls -laR total 32 drwxrwxrwt 3 jschauma wheel 512 Sep 23 14:15 . drwxrwxrwt 22 root wheel 5632 Sep 20 22:02 .. drwx------ 2 jschauma wheel 512 Sep 23 14:16 d prw--w---- 1 jdoe wheel 0 Sep 16 15:34 fifo lrwx------ 1 jschauma wheel 4 Sep 16 15:34 notalifo -> fifo ---------- 1 jschauma wheel 0 Sep 16 15:33 null ./d: total 16 drwx------ 2 jschauma wheel 512 Sep 23 14:16 . drwxrwxrwt 3 jschauma wheel 512 Sep 23 14:15 .. -rw-rw-rw- 1 jschauma wheel 0 Sep 23 14:14 blah -rw------- 1 jdoe wheel 0 Sep 23 14:16 mottle 2.1) The following people may remove the file 'null': a) "jschauma" b) "jdoe" c) "root" / superuser d) anybody in the group 'wheel' e) anybody f) nobody Correct answer: (a) and (c) (note sticky bit on the directory) 2.2) The following people may read the file 'null': a) "jschauma" b) "jdoe" c) "root" / superuser d) anybody in the group 'wheel' e) anybody f) nobody Correct answer: (c) I would have accepted (a) and (c) if you noted that jschauma being the owner of the file could change permissions such that he can read the file. 2.3) The following people may modify the file 'notalifo': a) "jschauma" b) "jdoe" c) "root" / superuser d) anybody in the group 'wheel' e) anybody f) nobody Correct answer: (a) and (c) 2.4) The following people may remove the file 'd/blah': a) "jschauma" b) "jdoe" c) "root" / superuser d) anybody in the group 'wheel' e) anybody f) nobody Correct answer: (a) and (c) 2.5) The following people may remove the file 'd/mottle': a) "jschauma" b) "jdoe" c) "root" / superuser d) anybody in the group 'wheel' e) anybody f) nobody Correct answer: (a) and (c) 'jdoe' cannot remove the file because the directory is owned by jschauma. 3.) Consider the following permissions on the file: -r-s--x--x 1 root wheel 1234 Sep 23 15:00 oink This executable contains the call 'access("oink", W_OK|X_OK)'. What will it return? (3 points) Correct answer: -1 (the file does not have write permissions for 'other' (or anybody for that matter)) 4.) Consider the following code: if ( creat("bar", S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) == -1 ) { perror("create error for bar"); exit(1); } If the user runs the command umask 172 before running this program, what will be the permissions on the file "bar"? (3 points) Solution: The creat call asks for permissions 766, umask turns off exec for owner, all for group and write for other, so we end up with permissions 604 or "rw----r--". 5.) Give two different methods of determining the current size of a file, with brief code examples in C. (3 points for each correct solution) Solution: I) lseek to end of file if ((fdesc = open("/path/to/file", O_RDONLY)) == -1) { fprintf(stderr, "%s: Can't open file: %s\n", argv[0], strerror(errno)); return 1; } if ((n = lseek(fdesc, 0, SEEK_END)) == -1) { fprintf(stderr, "%s: Can't seek to end: %s\n", argv[0], strerror(errno)); return 1; } printf("File contains %d bytes\n", n); II) stat the file: struct stat sb; int n; if ((n = stat("/path/to/file", &sb)) == -1) { fprintf(stderr, "%s: Can't stat the file: %s\n", argv[0], strerror(errno)); return 1; } printf("File contains %d bytes\n", (int)sb.st_size);