SHARE
TWEET

readlineWithPrimitive

deni-parvanov May 29th, 2020 603 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ssize_t readLine(char *buffer, char *filename, int size, off_t *offset) {
  2.     int fd = open(filename, O_RDONLY);
  3.  
  4.     if (fd == -1) return -1;
  5.  
  6.     ssize_t charCount = 0, counter = 0;
  7.     char *p = NULL;
  8.     if ((charCount = lseek (fd, *offset, SEEK_SET)) != -1) {
  9.         charCount = read (fd, buffer, size);
  10.     }
  11.  
  12.     close(fd);
  13.     if (charCount == -1) return -1;
  14.  
  15.     p = buffer;
  16.     while (counter < charCount && *p != '\n') p++, counter++;
  17.  
  18.     *p = 0;
  19.     if (counter == charCount) {
  20.         *offset += charCount;
  21.  
  22.         return charCount < (ssize_t)size ? charCount : 0;
  23.     }
  24.  
  25.     *offset += counter + 1;
  26.  
  27.     return counter;
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top