Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8.  
  9. int max(int a, int b) {
  10.     if (a < b) {
  11.         return b;
  12.     }
  13.     return a;
  14. }
  15.  
  16. int fill_buffer(int fd, char* buffer, int size) {
  17.     int result = read(fd, buffer, size);
  18.     int total_read = 0;
  19.     if (result > 0) {
  20.         total_read += result;
  21.         while (total_read != size && result != 0) {
  22.             result = read(fd, buffer + total_read, size - total_read);
  23.             if (result < 0) {
  24.                 return result;
  25.             }
  26.             total_read += result;
  27.         }
  28.         return total_read;
  29.     }
  30.     return result;
  31. }
  32.  
  33. struct FileContent read_file(int fd) {
  34.     int SIZE = 4096;
  35.     char buffer[SIZE];
  36.  
  37.     int result = fill_buffer(fd, buffer, SIZE);
  38.     if (result == -1) {
  39.         struct FileContent error;
  40.         error.size = -1;
  41.         error.data = NULL;
  42.         return error;
  43.     }
  44.  
  45.     struct FileContent content;
  46.     content.size = 0;
  47.     size_t capacity = result + 1;
  48.     content.data = malloc(capacity * sizeof(*content.data));
  49.     if (content.data == NULL) {
  50.         content.size = -1;
  51.         return content;
  52.     }
  53.  
  54.     while (result > 0) {
  55.         if (result + content.size > capacity) {
  56.             int new_capacity = content.size + max(SIZE, capacity * 2);
  57.             char* tmp = realloc(content.data, new_capacity);
  58.             if (!tmp) {
  59.                 free(content.data);
  60.                 content.data = NULL;
  61.                 content.size = -1;
  62.                 return content;
  63.             }
  64.             content.data = tmp;
  65.             capacity = new_capacity;
  66.         }
  67.         memcpy(content.data + content.size, buffer, result);
  68.         content.size += result;
  69.         result = fill_buffer(fd, buffer, SIZE);
  70.         if (result == -1) {
  71.             free(content.data);
  72.             content.data = NULL;
  73.             content.size = -1;
  74.             return content;
  75.         }
  76.     }
  77.  
  78.     if (content.size >= capacity) {
  79.         int new_capacity = content.size + 1;
  80.         char* tmp = realloc(content.data, new_capacity);
  81.         if (!tmp) {
  82.             free(content.data);
  83.             content.data = NULL;
  84.             content.size = -1;
  85.             return content;
  86.         }
  87.         content.data = tmp;
  88.         capacity = new_capacity;
  89.     }
  90.  
  91.     content.data[content.size] = '\0';
  92.     return content;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement