Skip to content

Instantly share code, notes, and snippets.

@tomty89
Created December 20, 2020 13:26
Show Gist options
  • Save tomty89/1cb7da11a49ff162b415efb70d6ca2df to your computer and use it in GitHub Desktop.
Save tomty89/1cb7da11a49ff162b415efb70d6ca2df to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main() {
int ret;
off_t size;
ssize_t rw;
char buf;
ret = open("", O_RDWR);
if (ret > 0) {
if (ret < 3)
printf("odd...\n");
printf("open succeeded\n");
size = lseek(ret, 0, SEEK_SET);
if (size >= 0) {
printf("size: %ld\n", size);
rw = read(ret, &buf, 1);
if (rw == 0) {
buf = 0;
} else if (rw < 0) {
printf("read error %d\n", errno);
goto close;
} else {
printf("read: 0x%x at offset %x\n", buf, size);
size = lseek(ret, -1, SEEK_CUR);
}
rw = write(ret, &buf, 1);
if (rw < 0)
printf("write error %d\n", errno);
else if (rw > 0)
printf("write 0x%x at offset %x\n", buf, size);
else
printf("write returns 0\n");
}
close:
ret = close(ret);
if (ret == 0)
printf("close succeeded\n");
} else {
printf("open error: %d\n", errno);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment