Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <limits.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <string.h>
- #define FILE_NAME "big"
- #define GB (1024*1024*1024)
- #define PLINE printf("=================================\n")
- int try_read(uint64_t size) {
- int exit = 0;
- printf("Trying to get %lld from %s\n", size, FILE_NAME);
- int fd = open(FILE_NAME, O_RDONLY);
- if(fd == -1) {
- printf("open() failed\n");
- exit = -1; goto ex_ret;
- }
- printf("File \"%s\" opened, allocating memory...\n", FILE_NAME);
- char* buffer = (char*)calloc(size, sizeof(char));
- if (!buffer) {
- printf("!! calloc(%lld) failed\n", size);
- exit = -1; goto ex_ret;
- }
- printf("Memory allocated, attempting read...\n");
- int64_t bytesRead = read(fd, buffer, size);
- if (bytesRead == -1) {
- printf("!! read() failed - errno=%d err=%s\n", errno, strerror(errno));
- exit = bytesRead; goto ex_clean;
- }
- printf("Did read %lld bytes ($req-$actual=%lld)\n", bytesRead, (size - bytesRead));
- ex_clean:
- free(buffer);
- ex_ret:
- PLINE;
- return exit;
- }
- int main() {
- printf("Platform SSIZE_MAX=%ld\n", SSIZE_MAX);
- printf("Platform INT_MAX=%d\n", INT_MAX);
- PLINE;
- try_read((uint64_t)2 * GB);
- try_read((uint64_t)2 * GB - 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement