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 * and/or 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>>, <<fgets_unlocked>>---get character string from a file or stream
33 char *fgets(char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
37 char *fgets_unlocked(char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
40 char *_fgets_r(struct _reent *<[ptr]>, char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
43 char *_fgets_unlocked_r(struct _reent *<[ptr]>, char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
46 Reads at most <[n-1]> characters from <[fp]> until a newline
47 is found. The characters including to the newline are stored
48 in <[buf]>. The buffer is terminated with a 0.
50 <<fgets_unlocked>> is a non-thread-safe version of <<fgets>>.
51 <<fgets_unlocked>> may only safely be used within a scope
52 protected by flockfile() (or ftrylockfile()) and funlockfile(). This
53 function may safely be used in a multi-threaded program if and only
54 if they are called while the invoking thread owns the (FILE *)
55 object, as is the case after a successful call to the flockfile() or
56 ftrylockfile() functions. If threads are disabled, then
57 <<fgets_unlocked>> is equivalent to <<fgets>>.
59 The functions <<_fgets_r>> and <<_fgets_unlocked_r>> are simply
60 reentrant versions that are passed the additional reentrant structure
61 pointer argument: <[ptr]>.
64 <<fgets>> returns the buffer passed to it, with the data
65 filled in. If end of file occurs with some data already
66 accumulated, the data is returned with no other indication. If
67 no data are read, NULL is returned instead.
70 <<fgets>> should replace all uses of <<gets>>. Note however
71 that <<fgets>> returns all of the data, while <<gets>> removes
72 the trailing newline (with no indication that it has done so.)
74 <<fgets_unlocked>> is a GNU extension.
76 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
77 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
85 #ifdef __IMPL_UNLOCKED__
86 #define _fgets_r _fgets_unlocked_r
87 #define fgets fgets_unlocked
91 * Read at most n-1 characters from the given file.
92 * Stop when a newline has been read, or the count runs out.
93 * Return first argument, or NULL if no characters were read.
97 _fgets_r (struct _reent
* ptr
,
104 unsigned char *p
, *t
;
106 if (n
< 2) /* sanity check */
113 _newlib_flockfile_start (fp
);
115 if (fp
->_flags
& __SCLE
)
118 /* Sorry, have to do it the slow way */
119 while (--n
> 0 && (c
= __sgetc_r (ptr
, fp
)) != EOF
)
125 if (c
== EOF
&& s
== buf
)
127 _newlib_flockfile_exit (fp
);
131 _newlib_flockfile_exit (fp
);
136 n
--; /* leave space for NUL */
140 * If the buffer is empty, refill it.
142 if ((len
= fp
->_r
) <= 0)
144 if (__srefill_r (ptr
, fp
))
146 /* EOF: stop with partial or no line */
149 _newlib_flockfile_exit (fp
);
159 * Scan through at most n bytes of the current buffer,
160 * looking for '\n'. If found, copy up to and including
161 * newline, and stop. Otherwise, copy entire chunk
166 t
= (unsigned char *) memchr ((void *) p
, '\n', len
);
172 (void) memcpy ((void *) s
, (void *) p
, len
);
174 _newlib_flockfile_exit (fp
);
179 (void) memcpy ((void *) s
, (void *) p
, len
);
182 while ((n
-= len
) != 0);
184 _newlib_flockfile_end (fp
);
191 fgets (char *__restrict buf
,
195 return _fgets_r (_REENT
, buf
, n
, fp
);
198 #endif /* !_REENT_ONLY */