title: Partial write()
body: <p>I can't seem to make partial write() work. It goes out of the memory and I don't know why.</p>
  <pre><code>int fd = open(path, O_RDONLY);
  if(fd == -1) {error handling}
  
  const size_t read_size = 100;
  size_t size = read_size;
  size_t offset = 0;
  size_t res = 0;
  char *buff = malloc(size+1);
  int lines = 0;
  int pos = 0;
  
  while((res = read(fd, buff + offset, read_size)) &gt; 0)
      {
          if(res == -1){error handling}
          offset += res;
          buff[offset] = '\0';
          if (offset + read_size &gt; size)
          {
                  size *= 2;
                  buff = realloc(buff, size+1);
          }
      }
  
  for(size_t i = 0;buff[i] != '\0'; i++) // counting the buff lines
      {
          if(buff[i] == '\n')
          {
              lines++;
          }
      }
  
  size = read_size;
  offset = 0;
  res = 0;
  
  if(lines &lt; 10)
  {
      while((res = write(STDOUT_FILENO, buff+offset, read_size)) &gt; 0)
      {
          offset += res;
      }
  }
  buff[offset] = '\0';
  else{another case where the position is found where the write() needs to start printing}
  </code></pre>
  <p>This is a part of a tail implementation in c. There is also another function which handles stdin and does the same thing (this one handles files).</p>