Skip to content

Instantly share code, notes, and snippets.

@oscardelben
Created April 30, 2013 15:53
Show Gist options
  • Save oscardelben/5489625 to your computer and use it in GitHub Desktop.
Save oscardelben/5489625 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
// Goal: What should this print?
#define FILE_NAME "foo"
void checkFD(int fd);
void checkOffset(off_t offset);
int main(int argc, char *argv[]) {
off_t offset;
int file1, file2;
file1 = open(FILE_NAME, O_CREAT | O_WRONLY);
checkFD(file1);
offset = lseek(file1, 0, SEEK_CUR);
checkOffset(offset);
printf("file1 at open: %ld\n", offset);
// push forward
offset = lseek(file1, 100, SEEK_CUR);
printf("file1 after lseek 100: %ld\n", offset);
file2 = open(FILE_NAME, O_CREAT | O_WRONLY);
checkFD(file2);
offset = lseek(file1, 0, SEEK_CUR);
printf("file1 after opening file2: %ld\n", offset);
offset = lseek(file2, 0, SEEK_CUR);
printf("file2 at open: %ld\n", offset);
close(file1);
close(file2);
return 0;
}
void checkFD(int fd) {
if (fd == -1) {
printf("Error opening file\n");
exit(1);
}
}
void checkOffset(off_t offset) {
if (offset == -1) {
printf("lseek error\n");
exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment