1 /*
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
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.
16  */
17 
18 /*
19 FUNCTION
20 <<fgets>>, <<fgets_unlocked>>---get character string from a file or stream
21 
22 INDEX
23 	fgets
24 INDEX
25 	fgets_unlocked
26 INDEX
27 	_fgets_r
28 INDEX
29 	_fgets_unlocked_r
30 
31 SYNOPSIS
32         #include <stdio.h>
33 	char *fgets(char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
34 
35 	#define _GNU_SOURCE
36         #include <stdio.h>
37 	char *fgets_unlocked(char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
38 
39         #include <stdio.h>
40 	char *fgets( char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
41 
42         #include <stdio.h>
43 	char *fgets_unlocked( char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
44 
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.
49 
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>>.
58 
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]>.
62 
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.
68 
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.)
73 
74 	<<fgets_unlocked>> is a GNU extension.
75 
76 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
77 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
78 */
79 
80 #define _DEFAULT_SOURCE
81 #include <_ansi.h>
82 #include <stdio.h>
83 #include <string.h>
84 #include "local.h"
85 
86 #ifdef __IMPL_UNLOCKED__
87 #define _fgets_r _fgets_unlocked_r
88 #define fgets fgets_unlocked
89 #endif
90 
91 /*
92  * Read at most n-1 characters from the given file.
93  * Stop when a newline has been read, or the count runs out.
94  * Return first argument, or NULL if no characters were read.
95  */
96 
97 char *
fgets(char * __restrict buf,int n,FILE * __restrict fp)98 fgets (
99        char *__restrict buf,
100        int n,
101        FILE *__restrict fp)
102 {
103   size_t len;
104   char *s;
105   unsigned char *p, *t;
106 
107   if (n < 2)			/* sanity check */
108     return 0;
109 
110   s = buf;
111 
112   CHECK_INIT(ptr, fp);
113 
114   _newlib_flockfile_start (fp);
115 #ifdef __SCLE
116   if (fp->_flags & __SCLE)
117     {
118       int c = 0;
119       /* Sorry, have to do it the slow way */
120       while (--n > 0 && (c = _sgetc ( fp)) != EOF)
121 	{
122 	  *s++ = c;
123 	  if (c == '\n')
124 	    break;
125 	}
126       if (c == EOF && s == buf)
127         {
128           _newlib_flockfile_exit (fp);
129           return NULL;
130         }
131       *s = 0;
132       _newlib_flockfile_exit (fp);
133       return buf;
134     }
135 #endif
136 
137   n--;				/* leave space for NUL */
138   do
139     {
140       /*
141        * If the buffer is empty, refill it.
142        */
143       if ((len = fp->_r) <= 0)
144 	{
145 	  if (_srefill ( fp))
146 	    {
147 	      /* EOF: stop with partial or no line */
148 	      if (s == buf)
149                 {
150                   _newlib_flockfile_exit (fp);
151                   return 0;
152                 }
153 	      break;
154 	    }
155 	  len = fp->_r;
156 	}
157       p = fp->_p;
158 
159       /*
160        * Scan through at most n bytes of the current buffer,
161        * looking for '\n'.  If found, copy up to and including
162        * newline, and stop.  Otherwise, copy entire chunk
163        * and loop.
164        */
165       if (len > (size_t) n)     /* n is always non-negative here */
166         len = (size_t) n;
167       t = (unsigned char *) memchr ((void *) p, '\n', len);
168       if (t != 0)
169 	{
170 	  len = ++t - p;
171 	  fp->_r -= len;
172 	  fp->_p = t;
173 	  (void) memcpy ((void *) s, (void *) p, len);
174 	  s[len] = 0;
175           _newlib_flockfile_exit (fp);
176 	  return (buf);
177 	}
178       fp->_r -= len;
179       fp->_p += len;
180       (void) memcpy ((void *) s, (void *) p, len);
181       s += len;
182     }
183   while ((n -= len) != 0);
184   *s = 0;
185   _newlib_flockfile_end (fp);
186   return buf;
187 }
188