Created
October 28, 2020 19:39
-
-
Save atamrawi/084e6ebb3b208576014154902493802f to your computer and use it in GitHub Desktop.
Assignment 4 - Problem 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <stdio.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <time.h> | |
#include <sys/time.h> | |
int main(int argc, char **argv) { | |
char *file; | |
char buffer[4096]; | |
int ffd, rc, i, j; | |
char answer = 'n'; | |
if(argc < 2) { | |
printf("%s file\n\tPrints file if you have access to it\n", argv[0]); | |
exit(1); | |
} | |
file = argv[1]; | |
if(access(argv[1], R_OK) == 0) { | |
printf("Do you really want to open [%s]? ", argv[1]); | |
scanf(" %c", &answer); | |
if(answer != 'y') { | |
exit(1); | |
} | |
ffd = open(file, O_RDONLY); | |
if(ffd == -1) { | |
printf("Unable to open file\n"); | |
exit(EXIT_FAILURE); | |
} | |
rc = read(ffd, buffer, sizeof(buffer)); | |
if(rc == -1) { | |
printf("Unable to read from file: %s\n", strerror(errno)); | |
exit(EXIT_FAILURE); | |
} | |
printf("%s\n", buffer); | |
} else { | |
printf("You don't have access to %s\n", file); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code snippet is based on Sam Bowne's lab on Race Conditions.