示例#1
0
文件: fileio.c 项目: Niichan/snircd
/** Get a line of input from a file.
 * @param[out] buf Output buffer to read to.
 * @param[in] len Maximum number of bytes to write to buffer
 * (including terminating NUL).
 * @param[in] fb File to read from.
 */
char *fbgets(char *buf, size_t len, FBFILE * fb)
{
  char *p = buf;
  assert(buf);
  assert(fb);
  assert(0 < len);

  if (fb->ptr == fb->endp && fbfill(fb) < 1)
    return 0;
  --len;
  while (len--) {
    *p = *fb->ptr++;
    if ('\n' == *p)
    {
      ++p;
      break;
    }
    /*
     * deal with CR's
     */
    else if ('\r' == *p) {
      if (fb->ptr < fb->endp || fbfill(fb) > 0) {
        if ('\n' == *fb->ptr)
          ++fb->ptr;
      }
      *p++ = '\n';
      break;
    }
    ++p;
    if (fb->ptr == fb->endp && fbfill(fb) < 1)
      break;
  }
  *p = '\0';
  return buf;
}
示例#2
0
char *
fbgets(char *buf, size_t len, FBFILE *fb)
{
  char *p = buf;
  assert(buf);
  assert(fb);
  assert(0 < len);

  if (fb == NULL || buf == NULL)
  {
    errno = EINVAL;
    return(NULL);
  }

  if(fb->pbptr != NULL)
  {
    strlcpy(buf,fb->pbptr,len);
    fb->pbptr = NULL;
    return(buf);
  }

  if (fb->ptr == fb->endp && fbfill(fb) < 1)
    return(NULL);

  --len;

  while (len--)
  {
    *p = *fb->ptr++;

    if ('\n' == *p)
    {
      ++p;
      break;
    }

    /* deal with CR's */
    else if ('\r' == *p)
    {
      if (fb->ptr < fb->endp || fbfill(fb) > 0)
      {
        if ('\n' == *fb->ptr)
          ++fb->ptr;
      }

      *p++ = '\n';
      break;
    }

    ++p;

    if (fb->ptr == fb->endp && fbfill(fb) < 1)
      break;
  }

  *p = '\0';
  return(buf);
}
示例#3
0
文件: fileio.c 项目: Niichan/snircd
/** Get a single character from a file.
 * @param[in] fb File to fetch from.
 * @return Character value read, or EOF on error or end-of-file.
 */
int fbgetc(FBFILE * fb)
{
  assert(fb);
  if (fb->ptr < fb->endp || fbfill(fb) > 0)
    return *fb->ptr++;
  return EOF;
}
示例#4
0
int
fbgetc(FBFILE * fb)
{
	s_assert(fb);
	if(fb == NULL)
	{
		errno = EINVAL;
		return -1;
	}
	if(fb->pbptr)
	{
		if((fb->pbptr == (fb->pbuf + BUFSIZ)) || (!*fb->pbptr))
			fb->pbptr = NULL;
	}

	if(fb->ptr < fb->endp || fbfill(fb) > 0)
		return *fb->ptr++;
	return EOF;
}
示例#5
0
int
fbgetc(FBFILE *fb)
{
  assert(fb);

  if (fb == NULL)
  {
    errno = EINVAL;
    return(-1);
  }

  if (fb->pbptr != NULL)
  {
    if ((fb->pbptr == (fb->pbuf + BUFSIZ)) ||
        (!*fb->pbptr))
      fb->pbptr = NULL;
  }

  if (fb->ptr < fb->endp || fbfill(fb) > 0)
    return(*fb->ptr++);

  return(EOF);
}