--- a/progs/read.c Tue Dec 30 01:22:25 2014 +0000
+++ b/progs/read.c Tue Dec 30 21:59:47 2014 +0000
@@ -1,50 +1,47 @@
#include <stdio.h>
-#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
+FILE *f; //file pointer
+
+//tests return errno = 13 for permission denied
+void read_test(char *name)
+{
+ if ((f = fopen(name, "r")) == NULL) {
+ printf("%s is not readable, errno = %d\n", name, errno);
+ } else {
+ printf("%s is readable\n", name); fclose(f);
+ }
+}
+
+void write_test(char *name)
+{
+ if ((f = fopen(name, "r+")) == NULL) {
+ printf("%s is not writable, errno = %d\n", name, errno);
+ } else {
+ printf("%s is writable\n", name); fclose(f);
+ }
+}
+
int main(int argc, char *argv[])
{
- FILE *f; //file pointer
-
printf("Real UID = %d\n", getuid());
printf("Effective UID = %d\n", geteuid());
- //read test (error 13 is permission denied)
- if ((f = fopen(argv[1], "r")) == NULL) {
- 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], "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);
- }
+ read_test(argv[1]);
+ write_test(argv[1]);
//lowering the access rights to the caller
if (setuid(getuid())) {
- fprintf(stderr, "could not reset setuid, errno = %d\n", errno); return 1;
+ printf("could not reset setuid, errno = %d\n", errno);
+ return 1;
}
printf("Real UID = %d\n", getuid());
printf("Effective UID = %d\n", geteuid());
- //read test
- if ((f = fopen(argv[1], "r")) == NULL) {
- 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, errno = %d\n", argv[1], errno);
- } else {
- fprintf(stderr, "%s is writable\n", argv[1]); fclose(f);
- }
+ read_test(argv[1]);
+ write_test(argv[1]);
return 0;
}