1N/A/*
1N/A * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
1N/A * All rights reserved.
1N/A * Copyright (c) 1990, 1993
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * This code is derived from software contributed to Berkeley by
1N/A * Chris Torek.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level of
1N/A * the sendmail distribution.
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include <sm/gen.h>
1N/ASM_RCSID("@(#)$Id: fget.c,v 1.22 2001/08/27 18:54:14 gshapiro Exp $")
1N/A#include <stdlib.h>
1N/A#include <string.h>
1N/A#include <sm/io.h>
1N/A#include <sm/assert.h>
1N/A#include "local.h"
1N/A
1N/A/*
1N/A** SM_IO_FGETS -- get a string from a file
1N/A**
1N/A** Read at most n-1 characters from the given file.
1N/A** Stop when a newline has been read, or the count ('n') runs out.
1N/A**
1N/A** Parameters:
1N/A** fp -- the file to read from
1N/A** timeout -- time to complete reading the string in milliseconds
1N/A** buf -- buffer to place read string in
1N/A** n -- size of 'buf'
1N/A**
1N/A** Returns:
1N/A** success: returns value of 'buf'
1N/A** failure: NULL (no characters were read)
1N/A** timeout: NULL and errno set to EAGAIN
1N/A**
1N/A** Side Effects:
1N/A** may move the file pointer
1N/A*/
1N/A
1N/Achar *
1N/Asm_io_fgets(fp, timeout, buf, n)
1N/A register SM_FILE_T *fp;
1N/A int timeout;
1N/A char *buf;
1N/A register int n;
1N/A{
1N/A register int len;
1N/A register char *s;
1N/A register unsigned char *p, *t;
1N/A
1N/A SM_REQUIRE_ISA(fp, SmFileMagic);
1N/A if (n <= 0) /* sanity check */
1N/A return NULL;
1N/A
1N/A s = buf;
1N/A n--; /* leave space for NUL */
1N/A while (n > 0)
1N/A {
1N/A /* If the buffer is empty, refill it. */
1N/A if ((len = fp->f_r) <= 0)
1N/A {
1N/A
1N/A /*
1N/A ** Timeout is only passed if we can't get the data
1N/A ** from the buffer (which is counted as immediately).
1N/A */
1N/A
1N/A if (sm_refill(fp, timeout) != 0)
1N/A {
1N/A /* EOF/error: stop with partial or no line */
1N/A if (s == buf)
1N/A return NULL;
1N/A break;
1N/A }
1N/A len = fp->f_r;
1N/A }
1N/A p = fp->f_p;
1N/A
1N/A /*
1N/A ** Scan through at most n bytes of the current buffer,
1N/A ** looking for '\n'. If found, copy up to and including
1N/A ** newline, and stop. Otherwise, copy entire chunk
1N/A ** and loop.
1N/A */
1N/A
1N/A if (len > n)
1N/A len = n;
1N/A t = (unsigned char *) memchr((void *) p, '\n', len);
1N/A if (t != NULL)
1N/A {
1N/A len = ++t - p;
1N/A fp->f_r -= len;
1N/A fp->f_p = t;
1N/A (void) memcpy((void *) s, (void *) p, len);
1N/A s[len] = 0;
1N/A return buf;
1N/A }
1N/A fp->f_r -= len;
1N/A fp->f_p += len;
1N/A (void) memcpy((void *) s, (void *) p, len);
1N/A s += len;
1N/A n -= len;
1N/A }
1N/A *s = 0;
1N/A return buf;
1N/A}