Update contrib.
3 * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
8 * Copyright (c) 1990 The Regents of the University of California.
11 * Redistribution and use in source and binary forms are permitted
12 * provided that the above copyright notice and this paragraph are
13 * duplicated in all such forms and that any documentation,
14 * advertising materials, and other materials related to such
15 * distribution and use acknowledge that the software was developed
16 * by the University of California, Berkeley. The name of the
17 * University may not be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 <<fgets>>---get character string from a file or stream
33 char *fgets(char *<[buf]>, int <[n]>, FILE *<[fp]>);
37 char *fgets(<[buf]>,<[n]>,<[fp]>)
43 Reads at most <[n-1]> characters from <[fp]> until a newline
44 is found. The characters including to the newline are stored
45 in <[buf]>. The buffer is terminated with a 0.
49 <<fgets>> returns the buffer passed to it, with the data
50 filled in. If end of file occurs with some data already
51 accumulated, the data is returned with no other indication. If
52 no data are read, NULL is returned instead.
55 <<fgets>> should replace all uses of <<gets>>. Note however
56 that <<fgets>> returns all of the data, while <<gets>> removes
57 the trailing newline (with no indication that it has done so.)
59 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
60 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
68 Get a string from a stream.
69 @return On success, the string read is returned.
70 On end-of-file or error, null pointer is returned.
71 @param buf pointer to a buffer where to store data read.
72 @param n max number of bytes to be read.
73 @param fp pointer to an open file.
76 fgets (char *buf, int n, FILE * fp)
82 if (n < 2) /* sanity check */
86 n--; /* leave space for NUL */
90 * If the buffer is empty, refill it.
92 if ((len = fp->_r) <= 0)
96 /* EOF: stop with partial or no line */
106 * Scan through at most n bytes of the current buffer,
107 * looking for '\n'. If found, copy up to and including
108 * newline, and stop. Otherwise, copy entire chunk
113 t = (unsigned char *) memchr ((void*) p, '\n', len);
119 (void) memcpy ((void*) s, (void*) p, len);
125 (void) memcpy ((void*) s, (void*) p, len);
128 while ((n -= len) != 0);