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 _GNU_SOURCE
81 #include <stdio.h>
82 #include <string.h>
83 #include "local.h"
84 
85 #ifdef __IMPL_UNLOCKED__
86 #define _fgets_r _fgets_unlocked_r
87 #define fgets fgets_unlocked
88 #endif
89 
90 /*
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.
94  */
95 
96 char *
fgets(char * __restrict buf,int n,FILE * __restrict fp)97 fgets (
98        char *__restrict buf,
99        int n,
100        FILE *__restrict fp)
101 {
102   size_t len;
103   char *s;
104   unsigned char *p, *t;
105 
106   if (n < 2)			/* sanity check */
107     return 0;
108 
109   s = buf;
110 
111   CHECK_INIT(ptr, fp);
112 
113   _newlib_flockfile_start (fp);
114 #ifdef __SCLE
115   if (fp->_flags & __SCLE)
116     {
117       int c = 0;
118       /* Sorry, have to do it the slow way */
119       while (--n > 0 && (c = _sgetc ( fp)) != EOF)
120 	{
121 	  *s++ = c;
122 	  if (c == '\n')
123 	    break;
124 	}
125       if (c == EOF && s == buf)
126         {
127           _newlib_flockfile_exit (fp);
128           return NULL;
129         }
130       *s = 0;
131       _newlib_flockfile_exit (fp);
132       return buf;
133     }
134 #endif
135 
136   n--;				/* leave space for NUL */
137   do
138     {
139       /*
140        * If the buffer is empty, refill it.
141        */
142       if ((len = fp->_r) <= 0)
143 	{
144 	  if (_srefill ( fp))
145 	    {
146 	      /* EOF: stop with partial or no line */
147 	      if (s == buf)
148                 {
149                   _newlib_flockfile_exit (fp);
150                   return 0;
151                 }
152 	      break;
153 	    }
154 	  len = fp->_r;
155 	}
156       p = fp->_p;
157 
158       /*
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.
163        */
164       if (len > (size_t) n)     /* n is always non-negative here */
165         len = (size_t) n;
166       t = (unsigned char *) memchr ((void *) p, '\n', len);
167       if (t != 0)
168 	{
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);
176 	}
177       fp->_r -= len;
178       fp->_p += len;
179       (void) memcpy ((void *) s, (void *) p, len);
180       s += len;
181     }
182   while ((n -= len) != 0);
183   *s = 0;
184   _newlib_flockfile_end (fp);
185   return buf;
186 }
187