freebsd-dev/contrib/sendmail/libsm/fget.c

114 lines
2.4 KiB
C
Raw Normal View History

2002-02-17 21:56:45 +00:00
/*
2014-01-26 20:46:55 +00:00
* Copyright (c) 2000-2001, 2013 Proofpoint, Inc. and its suppliers.
2002-02-17 21:56:45 +00:00
* All rights reserved.
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*/
#include <sm/gen.h>
2014-05-22 03:45:17 +00:00
SM_RCSID("@(#)$Id: fget.c,v 1.26 2013-11-22 20:51:42 ca Exp $")
2002-02-17 21:56:45 +00:00
#include <stdlib.h>
#include <string.h>
#include <sm/io.h>
#include <sm/assert.h>
#include "local.h"
/*
** SM_IO_FGETS -- get a string from a file
**
** Read at most n-1 characters from the given file.
** Stop when a newline has been read, or the count ('n') runs out.
**
** Parameters:
** fp -- the file to read from
** timeout -- time to complete reading the string in milliseconds
** buf -- buffer to place read string in
** n -- size of 'buf'
**
** Returns:
2013-04-21 16:35:04 +00:00
** success: number of characters
** failure: -1
** timeout: -1 and errno set to EAGAIN
2002-02-17 21:56:45 +00:00
**
** Side Effects:
** may move the file pointer
*/
2013-04-21 16:35:04 +00:00
int
2002-02-17 21:56:45 +00:00
sm_io_fgets(fp, timeout, buf, n)
register SM_FILE_T *fp;
int timeout;
char *buf;
register int n;
{
2013-04-21 16:35:04 +00:00
int len, r;
char *s;
unsigned char *p, *t;
2002-02-17 21:56:45 +00:00
SM_REQUIRE_ISA(fp, SmFileMagic);
if (n <= 0) /* sanity check */
2013-04-21 16:35:04 +00:00
return -1;
2002-02-17 21:56:45 +00:00
s = buf;
n--; /* leave space for NUL */
2013-04-21 16:35:04 +00:00
r = 0;
2002-02-17 21:56:45 +00:00
while (n > 0)
{
/* If the buffer is empty, refill it. */
if ((len = fp->f_r) <= 0)
{
/*
** Timeout is only passed if we can't get the data
** from the buffer (which is counted as immediately).
*/
if (sm_refill(fp, timeout) != 0)
{
/* EOF/error: stop with partial or no line */
if (s == buf)
2013-04-21 16:35:04 +00:00
return -1;
2002-02-17 21:56:45 +00:00
break;
}
len = fp->f_r;
}
p = fp->f_p;
/*
** Scan through at most n bytes of the current buffer,
** looking for '\n'. If found, copy up to and including
** newline, and stop. Otherwise, copy entire chunk
** and loop.
*/
if (len > n)
len = n;
t = (unsigned char *) memchr((void *) p, '\n', len);
if (t != NULL)
{
len = ++t - p;
2013-04-21 16:35:04 +00:00
r += len;
2002-02-17 21:56:45 +00:00
fp->f_r -= len;
fp->f_p = t;
(void) memcpy((void *) s, (void *) p, len);
s[len] = 0;
2013-04-21 16:35:04 +00:00
return r;
2002-02-17 21:56:45 +00:00
}
fp->f_r -= len;
fp->f_p += len;
(void) memcpy((void *) s, (void *) p, len);
s += len;
2013-04-21 16:35:04 +00:00
r += len;
2002-02-17 21:56:45 +00:00
n -= len;
}
*s = 0;
2013-04-21 16:35:04 +00:00
return r;
2002-02-17 21:56:45 +00:00
}