Skip to content

Instantly share code, notes, and snippets.

@atamrawi
Created October 28, 2020 19:39
Show Gist options
  • Save atamrawi/084e6ebb3b208576014154902493802f to your computer and use it in GitHub Desktop.
Save atamrawi/084e6ebb3b208576014154902493802f to your computer and use it in GitHub Desktop.
Assignment 4 - Problem 2
#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);
}
}
@atamrawi
Copy link
Author

This code snippet is based on Sam Bowne's lab on Race Conditions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment