Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <limits.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8. #include <string.h>
  9.  
  10.  
  11. #define FILE_NAME "big"
  12. #define GB (1024*1024*1024)
  13. #define PLINE printf("=================================\n")
  14.  
  15. int try_read(uint64_t size) {
  16.     int exit = 0;
  17.  
  18.     printf("Trying to get %lld from %s\n", size, FILE_NAME);
  19.     int fd = open(FILE_NAME, O_RDONLY);
  20.     if(fd == -1) {
  21.         printf("open() failed\n");
  22.         exit = -1; goto ex_ret;
  23.     }
  24.     printf("File \"%s\" opened, allocating memory...\n", FILE_NAME);
  25.  
  26.     char* buffer = (char*)calloc(size, sizeof(char));
  27.     if (!buffer) {
  28.         printf("!! calloc(%lld) failed\n", size);
  29.         exit = -1; goto ex_ret;
  30.     }
  31.     printf("Memory allocated, attempting read...\n");
  32.  
  33.     int64_t bytesRead = read(fd, buffer, size);
  34.     if (bytesRead == -1) {
  35.         printf("!! read() failed - errno=%d err=%s\n", errno, strerror(errno));
  36.         exit = bytesRead; goto ex_clean;
  37.     }
  38.     printf("Did read %lld bytes ($req-$actual=%lld)\n", bytesRead, (size - bytesRead));
  39.  
  40.     ex_clean:
  41.     free(buffer);
  42.  
  43.     ex_ret:
  44.     PLINE;
  45.     return exit;
  46. }
  47.  
  48. int main() {
  49.     printf("Platform SSIZE_MAX=%ld\n", SSIZE_MAX);
  50.     printf("Platform INT_MAX=%d\n", INT_MAX);
  51.     PLINE;
  52.  
  53.     try_read((uint64_t)2 * GB);
  54.     try_read((uint64_t)2 * GB - 1);
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement