updated
authorChristian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 30 Dec 2014 01:22:25 +0000
changeset 364 e4a879d442d5
parent 363 253fde62056a
child 365 942205605c30
updated
hws/so04.pdf
progs/read.c
Binary file hws/so04.pdf has changed
--- a/progs/read.c	Sat Dec 27 11:58:07 2014 +0000
+++ b/progs/read.c	Tue Dec 30 01:22:25 2014 +0000
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <errno.h>
 
 int main(int argc, char *argv[])
 {   
@@ -9,23 +10,23 @@
     printf("Real UID = %d\n", getuid());
     printf("Effective UID = %d\n", geteuid());
     
-    //read test
+    //read test (error 13 is permission denied)
     if ((f = fopen(argv[1], "r")) == NULL) {
-      fprintf(stderr, "%s is not readable\n", argv[1]);
+      fprintf(stderr, "%s is not readable, errno = %d\n", argv[1], errno);
     } else {
       fprintf(stderr, "%s is readable\n", argv[1]); fclose(f);
     }
 
     //write test
-    if ((f = fopen(argv[1], "w")) == NULL) {
-      fprintf(stderr, "%s is not writable\n", argv[1]);
+    if ((f = fopen(argv[1], "r+")) == NULL) {
+      fprintf(stderr, "%s is not writable, errno = %d\n", argv[1], errno);
     } else {
       fprintf(stderr, "%s is writable\n", argv[1]); fclose(f);
     }
 
     //lowering the access rights to the caller
     if (setuid(getuid())) {  
-      fprintf(stderr, "Could not reset setuid\n"); return 1;
+      fprintf(stderr, "could not reset setuid, errno = %d\n", errno); return 1;
     }
         
     printf("Real UID = %d\n", getuid());
@@ -33,14 +34,14 @@
 
     //read test
     if ((f = fopen(argv[1], "r")) == NULL) {
-      fprintf(stderr, "%s is not readable\n", argv[1]);
+      fprintf(stderr, "%s is not readable, errno = %d\n", argv[1], errno);
     } else {
       fprintf(stderr, "%s is readable\n", argv[1]); fclose(f);
     }
 
     //write test
     if ((f = fopen(argv[1], "w")) == NULL) {
-      fprintf(stderr, "%s is not writable\n", argv[1]);
+      fprintf(stderr, "%s is not writable, errno = %d\n", argv[1], errno);
     } else {
       fprintf(stderr, "%s is writable\n", argv[1]); fclose(f);
     }