2 * Copyright (c) 2000-2001, 2013 Proofpoint, Inc. and its suppliers.
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * By using this file, you agree to the terms and conditions set
11 * forth in the LICENSE file which can be found at the top level of
12 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: fget.c,v 1.26 2013-11-22 20:51:42 ca Exp $")
20 #include <sm/assert.h>
24 ** SM_IO_FGETS -- get a string from a file
26 ** Read at most n-1 characters from the given file.
27 ** Stop when a newline has been read, or the count ('n') runs out.
30 ** fp -- the file to read from
31 ** timeout -- time to complete reading the string in milliseconds
32 ** buf -- buffer to place read string in
36 ** success: number of characters
38 ** timeout: -1 and errno set to EAGAIN
41 ** may move the file pointer
45 sm_io_fgets(fp, timeout, buf, n)
46 register SM_FILE_T *fp;
55 SM_REQUIRE_ISA(fp, SmFileMagic);
56 if (n <= 0) /* sanity check */
60 n--; /* leave space for NUL */
64 /* If the buffer is empty, refill it. */
65 if ((len = fp->f_r) <= 0)
69 ** Timeout is only passed if we can't get the data
70 ** from the buffer (which is counted as immediately).
73 if (sm_refill(fp, timeout) != 0)
75 /* EOF/error: stop with partial or no line */
85 ** Scan through at most n bytes of the current buffer,
86 ** looking for '\n'. If found, copy up to and including
87 ** newline, and stop. Otherwise, copy entire chunk
93 t = (unsigned char *) memchr((void *) p, '\n', len);
100 (void) memcpy((void *) s, (void *) p, len);
106 (void) memcpy((void *) s, (void *) p, len);