bug-gnulib
[Top][All Lists]
Advanced

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

[RFE] function to read a file descriptor


From: Debarshi Ray
Subject: [RFE] function to read a file descriptor
Date: Mon, 18 Aug 2008 20:44:36 +0530

I was wondering whether gnulib has a function to safely read or recv
from a file desctiptor. I could not find one, and have had to hack
them up on numerous occasions. Here is one such version I wrote while
implementing a feature for GNU Inetutils:

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

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

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

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

      count += nread;

      if (nread < BUFSIZ)
        break;
      else
        *size += BUFSIZ;
    }

  return count;
}

Comments?

Happy hacking,
Debarshi




reply via email to

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