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 #include <sm/gen.h> 16*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: fget.c,v 1.22 2001/08/27 18:54:14 gshapiro Exp $") 17*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 18*7c478bd9Sstevel@tonic-gate #include <string.h> 19*7c478bd9Sstevel@tonic-gate #include <sm/io.h> 20*7c478bd9Sstevel@tonic-gate #include <sm/assert.h> 21*7c478bd9Sstevel@tonic-gate #include "local.h" 22*7c478bd9Sstevel@tonic-gate 23*7c478bd9Sstevel@tonic-gate /* 24*7c478bd9Sstevel@tonic-gate ** SM_IO_FGETS -- get a string from a file 25*7c478bd9Sstevel@tonic-gate ** 26*7c478bd9Sstevel@tonic-gate ** Read at most n-1 characters from the given file. 27*7c478bd9Sstevel@tonic-gate ** Stop when a newline has been read, or the count ('n') runs out. 28*7c478bd9Sstevel@tonic-gate ** 29*7c478bd9Sstevel@tonic-gate ** Parameters: 30*7c478bd9Sstevel@tonic-gate ** fp -- the file to read from 31*7c478bd9Sstevel@tonic-gate ** timeout -- time to complete reading the string in milliseconds 32*7c478bd9Sstevel@tonic-gate ** buf -- buffer to place read string in 33*7c478bd9Sstevel@tonic-gate ** n -- size of 'buf' 34*7c478bd9Sstevel@tonic-gate ** 35*7c478bd9Sstevel@tonic-gate ** Returns: 36*7c478bd9Sstevel@tonic-gate ** success: returns value of 'buf' 37*7c478bd9Sstevel@tonic-gate ** failure: NULL (no characters were read) 38*7c478bd9Sstevel@tonic-gate ** timeout: NULL and errno set to EAGAIN 39*7c478bd9Sstevel@tonic-gate ** 40*7c478bd9Sstevel@tonic-gate ** Side Effects: 41*7c478bd9Sstevel@tonic-gate ** may move the file pointer 42*7c478bd9Sstevel@tonic-gate */ 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate char * 45*7c478bd9Sstevel@tonic-gate sm_io_fgets(fp, timeout, buf, n) 46*7c478bd9Sstevel@tonic-gate register SM_FILE_T *fp; 47*7c478bd9Sstevel@tonic-gate int timeout; 48*7c478bd9Sstevel@tonic-gate char *buf; 49*7c478bd9Sstevel@tonic-gate register int n; 50*7c478bd9Sstevel@tonic-gate { 51*7c478bd9Sstevel@tonic-gate register int len; 52*7c478bd9Sstevel@tonic-gate register char *s; 53*7c478bd9Sstevel@tonic-gate register unsigned char *p, *t; 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate SM_REQUIRE_ISA(fp, SmFileMagic); 56*7c478bd9Sstevel@tonic-gate if (n <= 0) /* sanity check */ 57*7c478bd9Sstevel@tonic-gate return NULL; 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate s = buf; 60*7c478bd9Sstevel@tonic-gate n--; /* leave space for NUL */ 61*7c478bd9Sstevel@tonic-gate while (n > 0) 62*7c478bd9Sstevel@tonic-gate { 63*7c478bd9Sstevel@tonic-gate /* If the buffer is empty, refill it. */ 64*7c478bd9Sstevel@tonic-gate if ((len = fp->f_r) <= 0) 65*7c478bd9Sstevel@tonic-gate { 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate /* 68*7c478bd9Sstevel@tonic-gate ** Timeout is only passed if we can't get the data 69*7c478bd9Sstevel@tonic-gate ** from the buffer (which is counted as immediately). 70*7c478bd9Sstevel@tonic-gate */ 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate if (sm_refill(fp, timeout) != 0) 73*7c478bd9Sstevel@tonic-gate { 74*7c478bd9Sstevel@tonic-gate /* EOF/error: stop with partial or no line */ 75*7c478bd9Sstevel@tonic-gate if (s == buf) 76*7c478bd9Sstevel@tonic-gate return NULL; 77*7c478bd9Sstevel@tonic-gate break; 78*7c478bd9Sstevel@tonic-gate } 79*7c478bd9Sstevel@tonic-gate len = fp->f_r; 80*7c478bd9Sstevel@tonic-gate } 81*7c478bd9Sstevel@tonic-gate p = fp->f_p; 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate /* 84*7c478bd9Sstevel@tonic-gate ** Scan through at most n bytes of the current buffer, 85*7c478bd9Sstevel@tonic-gate ** looking for '\n'. If found, copy up to and including 86*7c478bd9Sstevel@tonic-gate ** newline, and stop. Otherwise, copy entire chunk 87*7c478bd9Sstevel@tonic-gate ** and loop. 88*7c478bd9Sstevel@tonic-gate */ 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate if (len > n) 91*7c478bd9Sstevel@tonic-gate len = n; 92*7c478bd9Sstevel@tonic-gate t = (unsigned char *) memchr((void *) p, '\n', len); 93*7c478bd9Sstevel@tonic-gate if (t != NULL) 94*7c478bd9Sstevel@tonic-gate { 95*7c478bd9Sstevel@tonic-gate len = ++t - p; 96*7c478bd9Sstevel@tonic-gate fp->f_r -= len; 97*7c478bd9Sstevel@tonic-gate fp->f_p = t; 98*7c478bd9Sstevel@tonic-gate (void) memcpy((void *) s, (void *) p, len); 99*7c478bd9Sstevel@tonic-gate s[len] = 0; 100*7c478bd9Sstevel@tonic-gate return buf; 101*7c478bd9Sstevel@tonic-gate } 102*7c478bd9Sstevel@tonic-gate fp->f_r -= len; 103*7c478bd9Sstevel@tonic-gate fp->f_p += len; 104*7c478bd9Sstevel@tonic-gate (void) memcpy((void *) s, (void *) p, len); 105*7c478bd9Sstevel@tonic-gate s += len; 106*7c478bd9Sstevel@tonic-gate n -= len; 107*7c478bd9Sstevel@tonic-gate } 108*7c478bd9Sstevel@tonic-gate *s = 0; 109*7c478bd9Sstevel@tonic-gate return buf; 110*7c478bd9Sstevel@tonic-gate } 111