progs/read2.c
changeset 366 34a8f73b2c94
parent 365 942205605c30
child 367 3f0738fc8230
equal deleted inserted replaced
365:942205605c30 366:34a8f73b2c94
     1 #include <stdio.h>
       
     2 #include <unistd.h>
       
     3 #include <errno.h>
       
     4 
       
     5 FILE *f; //file pointer
       
     6 
       
     7 //tests return errno = 13 for permission denied
       
     8 void read_test(char *name) 
       
     9 {
       
    10     if ((f = fopen(name, "r")) == NULL) {
       
    11       printf("%s is not readable, errno = %d\n", name, errno);
       
    12     } else {
       
    13       printf("%s is readable\n", name); fclose(f);
       
    14     }
       
    15 }
       
    16 
       
    17 void write_test(char *name) 
       
    18 {
       
    19     if ((f = fopen(name, "r+")) == NULL) {
       
    20       printf("%s is not writable, errno = %d\n", name, errno);
       
    21     } else {
       
    22       printf("%s is writable\n", name); fclose(f);
       
    23     }
       
    24 }
       
    25 
       
    26 int main(int argc, char *argv[])
       
    27 {   
       
    28     printf("Real UID = %d\n", getuid());
       
    29     printf("Effective UID = %d\n", geteuid());
       
    30     
       
    31     read_test(argv[1]);
       
    32     write_test(argv[1]);
       
    33 
       
    34     //lowering the access rights to the caller
       
    35     if (setuid(getuid())) {  
       
    36       printf("could not reset setuid, errno = %d\n", errno); 
       
    37       return 1;
       
    38     }
       
    39         
       
    40     printf("Real UID = %d\n", getuid());
       
    41     printf("Effective UID = %d\n", geteuid());      
       
    42 
       
    43     read_test(argv[1]);
       
    44     write_test(argv[1]);
       
    45 
       
    46     return 0;
       
    47 }