2 * Copyright (c) 1990 The Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 <<fgets>>---get character string from a file or stream
27 char *fgets(char *<[buf]>, int <[n]>, FILE *<[fp]>);
31 char *fgets(<[buf]>,<[n]>,<[fp]>)
37 Reads at most <[n-1]> characters from <[fp]> until a newline
38 is found. The characters including to the newline are stored
39 in <[buf]>. The buffer is terminated with a 0.
43 <<fgets>> returns the buffer passed to it, with the data
44 filled in. If end of file occurs with some data already
45 accumulated, the data is returned with no other indication. If
46 no data are read, NULL is returned instead.
49 <<fgets>> should replace all uses of <<gets>>. Note however
50 that <<fgets>> returns all of the data, while <<gets>> removes
51 the trailing newline (with no indication that it has done so.)
53 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
54 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
60 extern int __srefill ();
63 * Read at most n-1 characters from the given file.
64 * Stop when a newline has been read, or the count runs out.
65 * Return first argument, or NULL if no characters were read.
69 _DEFUN (fgets, (buf, n, fp),
78 if (n < 2) /* sanity check */
82 n--; /* leave space for NUL */
86 * If the buffer is empty, refill it.
88 if ((len = fp->_r) <= 0)
92 /* EOF: stop with partial or no line */
102 * Scan through at most n bytes of the current buffer,
103 * looking for '\n'. If found, copy up to and including
104 * newline, and stop. Otherwise, copy entire chunk
109 t = (unsigned char *) memchr ((_PTR) p, '\n', len);
115 (void) memcpy ((_PTR) s, (_PTR) p, len);
121 (void) memcpy ((_PTR) s, (_PTR) p, len);
124 while ((n -= len) != 0);