Created
December 20, 2020 13:26
-
-
Save tomty89/1cb7da11a49ff162b415efb70d6ca2df to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <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