Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / fgets.c
blob96a44a765901319f5a8931a5a5f04ecc828d35e9
1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
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.
19 FUNCTION
20 <<fgets>>, <<fgets_unlocked>>---get character string from a file or stream
22 INDEX
23 fgets
24 INDEX
25 fgets_unlocked
26 INDEX
27 _fgets_r
28 INDEX
29 _fgets_unlocked_r
31 SYNOPSIS
32 #include <stdio.h>
33 char *fgets(char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
35 #define _GNU_SOURCE
36 #include <stdio.h>
37 char *fgets_unlocked(char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
39 #include <stdio.h>
40 char *_fgets_r(struct _reent *<[ptr]>, char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
42 #include <stdio.h>
43 char *_fgets_unlocked_r(struct _reent *<[ptr]>, char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
45 DESCRIPTION
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]>.
63 RETURNS
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.
69 PORTABILITY
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>>.
80 #include <_ansi.h>
81 #include <stdio.h>
82 #include <string.h>
83 #include "local.h"
85 #ifdef __IMPL_UNLOCKED__
86 #define _fgets_r _fgets_unlocked_r
87 #define fgets fgets_unlocked
88 #endif
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.
96 char *
97 _fgets_r (struct _reent * ptr,
98 char *__restrict buf,
99 int n,
100 FILE *__restrict fp)
102 size_t len;
103 char *s;
104 unsigned char *p, *t;
106 if (n < 2) /* sanity check */
107 return 0;
109 s = buf;
111 CHECK_INIT(ptr, fp);
113 _newlib_flockfile_start (fp);
114 #ifdef __SCLE
115 if (fp->_flags & __SCLE)
117 int c = 0;
118 /* Sorry, have to do it the slow way */
119 while (--n > 0 && (c = __sgetc_r (ptr, fp)) != EOF)
121 *s++ = c;
122 if (c == '\n')
123 break;
125 if (c == EOF && s == buf)
127 _newlib_flockfile_exit (fp);
128 return NULL;
130 *s = 0;
131 _newlib_flockfile_exit (fp);
132 return buf;
134 #endif
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 */
147 if (s == buf)
149 _newlib_flockfile_exit (fp);
150 return 0;
152 break;
154 len = fp->_r;
156 p = fp->_p;
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
162 * and loop.
164 if (len > n)
165 len = n;
166 t = (unsigned char *) memchr ((void *) p, '\n', len);
167 if (t != 0)
169 len = ++t - p;
170 fp->_r -= len;
171 fp->_p = t;
172 (void) memcpy ((void *) s, (void *) p, len);
173 s[len] = 0;
174 _newlib_flockfile_exit (fp);
175 return (buf);
177 fp->_r -= len;
178 fp->_p += len;
179 (void) memcpy ((void *) s, (void *) p, len);
180 s += len;
182 while ((n -= len) != 0);
183 *s = 0;
184 _newlib_flockfile_end (fp);
185 return buf;
188 #ifndef _REENT_ONLY
190 char *
191 fgets (char *__restrict buf,
192 int n,
193 FILE *__restrict fp)
195 return _fgets_r (_REENT, buf, n, fp);
198 #endif /* !_REENT_ONLY */