bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFE] function to read a file descriptor


From: Debarshi Ray
Subject: Re: [RFE] function to read a file descriptor
Date: Thu, 21 Aug 2008 01:10:13 +0530

Oops sorry! This one is hopefully better.

size_t
recvbuf (int sockfd, void **buffer, size_t *size)
{
  size_t block = BUFSIZ;
  size_t count = 0;
  ssize_t nread;

  if (*buffer == NULL)
    *size = block;

  for (;;)
    {
      *buffer = xrealloc (*buffer, *size);
      nread = recv (sockfd, *buffer + count, block, 0);

      if (nread == -1)
        error (EXIT_FAILURE, errno, "recv");

      count += nread;

      if (nread < block)
        break;
      else
        {
          block = (*size / 2 > BUFSIZ) ? *size / 2 : BUFSIZ;
          *size += block;
        }
    }

  return count;
}

Comments?

Happy hacking,
Debarshi




reply via email to

[Prev in Thread] Current Thread [Next in Thread]