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.
20 <<fgets>>---get character string from a file or stream
29 char *fgets(char *<[buf]>, int <[n]>, FILE *<[fp]>);
32 char *_fgets_r(struct _reent *<[ptr]>, char *<[buf]>, int <[n]>, FILE *<[fp]>);
36 char *fgets(<[buf]>,<[n]>,<[fp]>)
42 char *_fgets_r(<[ptr]>, <[buf]>,<[n]>,<[fp]>)
43 struct _reent *<[ptr]>;
49 Reads at most <[n-1]> characters from <[fp]> until a newline
50 is found. The characters including to the newline are stored
51 in <[buf]>. The buffer is terminated with a 0.
53 The <<_fgets_r>> function is simply the reentrant version of
54 <<fgets>> and is passed an additional reentrancy structure
58 <<fgets>> returns the buffer passed to it, with the data
59 filled in. If end of file occurs with some data already
60 accumulated, the data is returned with no other indication. If
61 no data are read, NULL is returned instead.
64 <<fgets>> should replace all uses of <<gets>>. Note however
65 that <<fgets>> returns all of the data, while <<gets>> removes
66 the trailing newline (with no indication that it has done so.)
68 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
69 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
78 * Read at most n-1 characters from the given file.
79 * Stop when a newline has been read, or the count runs out.
80 * Return first argument, or NULL if no characters were read.
84 _DEFUN(_fgets_r, (ptr, buf, n, fp),
85 struct _reent * ptr _AND
94 if (n < 2) /* sanity check */
101 __sfp_lock_acquire ();
104 if (fp->_flags & __SCLE)
107 /* Sorry, have to do it the slow way */
108 while (--n > 0 && (c = __sgetc_r (ptr, fp)) != EOF)
114 if (c == EOF && s == buf)
117 __sfp_lock_release ();
122 __sfp_lock_release ();
127 n--; /* leave space for NUL */
131 * If the buffer is empty, refill it.
133 if ((len = fp->_r) <= 0)
135 if (__srefill_r (ptr, fp))
137 /* EOF: stop with partial or no line */
141 __sfp_lock_release ();
151 * Scan through at most n bytes of the current buffer,
152 * looking for '\n'. If found, copy up to and including
153 * newline, and stop. Otherwise, copy entire chunk
158 t = (unsigned char *) memchr ((_PTR) p, '\n', len);
164 _CAST_VOID memcpy ((_PTR) s, (_PTR) p, len);
167 __sfp_lock_release ();
172 _CAST_VOID memcpy ((_PTR) s, (_PTR) p, len);
175 while ((n -= len) != 0);
178 __sfp_lock_release ();
185 _DEFUN(fgets, (buf, n, fp),
190 return _fgets_r (_REENT, buf, n, fp);
193 #endif /* !_REENT_ONLY */