sl@0: /* FGETS.C sl@0: * sl@0: * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: */ sl@0: sl@0: /* sl@0: * Copyright (c) 1990 The Regents of the University of California. sl@0: * All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms are permitted sl@0: * provided that the above copyright notice and this paragraph are sl@0: * duplicated in all such forms and that any documentation, sl@0: * advertising materials, and other materials related to such sl@0: * distribution and use acknowledge that the software was developed sl@0: * by the University of California, Berkeley. The name of the sl@0: * University may not be used to endorse or promote products derived sl@0: * from this software without specific prior written permission. sl@0: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR sl@0: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED sl@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. sl@0: */ sl@0: sl@0: /* sl@0: sl@0: FUNCTION sl@0: <>---get character string from a file or stream sl@0: INDEX sl@0: fgets sl@0: sl@0: ANSI_SYNOPSIS sl@0: #include sl@0: char *fgets(char *<[buf]>, int <[n]>, FILE *<[fp]>); sl@0: sl@0: TRAD_SYNOPSIS sl@0: #include sl@0: char *fgets(<[buf]>,<[n]>,<[fp]>) sl@0: char *<[buf]>; sl@0: int <[n]>; sl@0: FILE *<[fp]>; sl@0: sl@0: DESCRIPTION sl@0: Reads at most <[n-1]> characters from <[fp]> until a newline sl@0: is found. The characters including to the newline are stored sl@0: in <[buf]>. The buffer is terminated with a 0. sl@0: sl@0: sl@0: RETURNS sl@0: <> returns the buffer passed to it, with the data sl@0: filled in. If end of file occurs with some data already sl@0: accumulated, the data is returned with no other indication. If sl@0: no data are read, NULL is returned instead. sl@0: sl@0: PORTABILITY sl@0: <> should replace all uses of <>. Note however sl@0: that <> returns all of the data, while <> removes sl@0: the trailing newline (with no indication that it has done so.) sl@0: sl@0: Supporting OS subroutines required: <>, <>, <>, sl@0: <>, <>, <>, <>. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include "LOCAL.H" sl@0: sl@0: /** sl@0: Get a string from a stream. sl@0: @return On success, the string read is returned. sl@0: On end-of-file or error, null pointer is returned. sl@0: @param buf pointer to a buffer where to store data read. sl@0: @param n max number of bytes to be read. sl@0: @param fp pointer to an open file. sl@0: */ sl@0: EXPORT_C char * sl@0: fgets (char *buf, int n, FILE * fp) sl@0: { sl@0: size_t len; sl@0: char *s; sl@0: unsigned char *p, *t; sl@0: sl@0: if (n < 2) /* sanity check */ sl@0: return 0; sl@0: sl@0: s = buf; sl@0: n--; /* leave space for NUL */ sl@0: do sl@0: { sl@0: /* sl@0: * If the buffer is empty, refill it. sl@0: */ sl@0: if ((len = fp->_r) <= 0) sl@0: { sl@0: if (__srefill (fp)) sl@0: { sl@0: /* EOF: stop with partial or no line */ sl@0: if (s == buf) sl@0: return 0; sl@0: break; sl@0: } sl@0: len = fp->_r; sl@0: } sl@0: p = fp->_p; sl@0: sl@0: /* sl@0: * Scan through at most n bytes of the current buffer, sl@0: * looking for '\n'. If found, copy up to and including sl@0: * newline, and stop. Otherwise, copy entire chunk sl@0: * and loop. sl@0: */ sl@0: if ((int)len > n) sl@0: len = n; sl@0: t = (unsigned char *) memchr ((void*) p, '\n', len); sl@0: if (t != 0) sl@0: { sl@0: len = ++t - p; sl@0: fp->_r -= len; sl@0: fp->_p = t; sl@0: (void) memcpy ((void*) s, (void*) p, len); sl@0: s[len] = 0; sl@0: return (buf); sl@0: } sl@0: fp->_r -= len; sl@0: fp->_p += len; sl@0: (void) memcpy ((void*) s, (void*) p, len); sl@0: s += len; sl@0: } sl@0: while ((n -= len) != 0); sl@0: *s = 0; sl@0: return buf; sl@0: }