xref: /titanic_50/usr/src/cmd/sendmail/libsm/fget.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *      All rights reserved.
4*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993
5*7c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
6*7c478bd9Sstevel@tonic-gate  *
7*7c478bd9Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
8*7c478bd9Sstevel@tonic-gate  * Chris Torek.
9*7c478bd9Sstevel@tonic-gate  *
10*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
11*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
12*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
13*7c478bd9Sstevel@tonic-gate  */
14*7c478bd9Sstevel@tonic-gate 
15*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
16*7c478bd9Sstevel@tonic-gate 
17*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
18*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: fget.c,v 1.22 2001/08/27 18:54:14 gshapiro Exp $")
19*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
20*7c478bd9Sstevel@tonic-gate #include <string.h>
21*7c478bd9Sstevel@tonic-gate #include <sm/io.h>
22*7c478bd9Sstevel@tonic-gate #include <sm/assert.h>
23*7c478bd9Sstevel@tonic-gate #include "local.h"
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate /*
26*7c478bd9Sstevel@tonic-gate **  SM_IO_FGETS -- get a string from a file
27*7c478bd9Sstevel@tonic-gate **
28*7c478bd9Sstevel@tonic-gate **  Read at most n-1 characters from the given file.
29*7c478bd9Sstevel@tonic-gate **  Stop when a newline has been read, or the count ('n') runs out.
30*7c478bd9Sstevel@tonic-gate **
31*7c478bd9Sstevel@tonic-gate **	Parameters:
32*7c478bd9Sstevel@tonic-gate **		fp -- the file to read from
33*7c478bd9Sstevel@tonic-gate **		timeout -- time to complete reading the string in milliseconds
34*7c478bd9Sstevel@tonic-gate **		buf -- buffer to place read string in
35*7c478bd9Sstevel@tonic-gate **		n -- size of 'buf'
36*7c478bd9Sstevel@tonic-gate **
37*7c478bd9Sstevel@tonic-gate **	Returns:
38*7c478bd9Sstevel@tonic-gate **		success: returns value of 'buf'
39*7c478bd9Sstevel@tonic-gate **		failure: NULL (no characters were read)
40*7c478bd9Sstevel@tonic-gate **		timeout: NULL and errno set to EAGAIN
41*7c478bd9Sstevel@tonic-gate **
42*7c478bd9Sstevel@tonic-gate **	Side Effects:
43*7c478bd9Sstevel@tonic-gate **		may move the file pointer
44*7c478bd9Sstevel@tonic-gate */
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate char *
47*7c478bd9Sstevel@tonic-gate sm_io_fgets(fp, timeout, buf, n)
48*7c478bd9Sstevel@tonic-gate 	register SM_FILE_T *fp;
49*7c478bd9Sstevel@tonic-gate 	int timeout;
50*7c478bd9Sstevel@tonic-gate 	char *buf;
51*7c478bd9Sstevel@tonic-gate 	register int n;
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	register int len;
54*7c478bd9Sstevel@tonic-gate 	register char *s;
55*7c478bd9Sstevel@tonic-gate 	register unsigned char *p, *t;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	SM_REQUIRE_ISA(fp, SmFileMagic);
58*7c478bd9Sstevel@tonic-gate 	if (n <= 0)		/* sanity check */
59*7c478bd9Sstevel@tonic-gate 		return NULL;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	s = buf;
62*7c478bd9Sstevel@tonic-gate 	n--;			/* leave space for NUL */
63*7c478bd9Sstevel@tonic-gate 	while (n > 0)
64*7c478bd9Sstevel@tonic-gate 	{
65*7c478bd9Sstevel@tonic-gate 		/* If the buffer is empty, refill it. */
66*7c478bd9Sstevel@tonic-gate 		if ((len = fp->f_r) <= 0)
67*7c478bd9Sstevel@tonic-gate 		{
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 			/*
70*7c478bd9Sstevel@tonic-gate 			**  Timeout is only passed if we can't get the data
71*7c478bd9Sstevel@tonic-gate 			**  from the buffer (which is counted as immediately).
72*7c478bd9Sstevel@tonic-gate 			*/
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 			if (sm_refill(fp, timeout) != 0)
75*7c478bd9Sstevel@tonic-gate 			{
76*7c478bd9Sstevel@tonic-gate 				/* EOF/error: stop with partial or no line */
77*7c478bd9Sstevel@tonic-gate 				if (s == buf)
78*7c478bd9Sstevel@tonic-gate 					return NULL;
79*7c478bd9Sstevel@tonic-gate 				break;
80*7c478bd9Sstevel@tonic-gate 			}
81*7c478bd9Sstevel@tonic-gate 			len = fp->f_r;
82*7c478bd9Sstevel@tonic-gate 		}
83*7c478bd9Sstevel@tonic-gate 		p = fp->f_p;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 		/*
86*7c478bd9Sstevel@tonic-gate 		**  Scan through at most n bytes of the current buffer,
87*7c478bd9Sstevel@tonic-gate 		**  looking for '\n'.  If found, copy up to and including
88*7c478bd9Sstevel@tonic-gate 		**  newline, and stop.  Otherwise, copy entire chunk
89*7c478bd9Sstevel@tonic-gate 		**  and loop.
90*7c478bd9Sstevel@tonic-gate 		*/
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 		if (len > n)
93*7c478bd9Sstevel@tonic-gate 			len = n;
94*7c478bd9Sstevel@tonic-gate 		t = (unsigned char *) memchr((void *) p, '\n', len);
95*7c478bd9Sstevel@tonic-gate 		if (t != NULL)
96*7c478bd9Sstevel@tonic-gate 		{
97*7c478bd9Sstevel@tonic-gate 			len = ++t - p;
98*7c478bd9Sstevel@tonic-gate 			fp->f_r -= len;
99*7c478bd9Sstevel@tonic-gate 			fp->f_p = t;
100*7c478bd9Sstevel@tonic-gate 			(void) memcpy((void *) s, (void *) p, len);
101*7c478bd9Sstevel@tonic-gate 			s[len] = 0;
102*7c478bd9Sstevel@tonic-gate 			return buf;
103*7c478bd9Sstevel@tonic-gate 		}
104*7c478bd9Sstevel@tonic-gate 		fp->f_r -= len;
105*7c478bd9Sstevel@tonic-gate 		fp->f_p += len;
106*7c478bd9Sstevel@tonic-gate 		(void) memcpy((void *) s, (void *) p, len);
107*7c478bd9Sstevel@tonic-gate 		s += len;
108*7c478bd9Sstevel@tonic-gate 		n -= len;
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 	*s = 0;
111*7c478bd9Sstevel@tonic-gate 	return buf;
112*7c478bd9Sstevel@tonic-gate }
113