1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* and/or other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
FUNCTION
<<fgets>>, <<fgets_unlocked>>---get character string from a file or stream
INDEX
fgets
INDEX
fgets_unlocked
INDEX
_fgets_r
INDEX
_fgets_unlocked_r
SYNOPSIS
#include <stdio.h>
char *fgets(char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
#define _GNU_SOURCE
#include <stdio.h>
char *fgets_unlocked(char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
#include <stdio.h>
char *fgets( char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
#include <stdio.h>
char *fgets_unlocked( char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
DESCRIPTION
Reads at most <[n-1]> characters from <[fp]> until a newline
is found. The characters including to the newline are stored
in <[buf]>. The buffer is terminated with a 0.
<<fgets_unlocked>> is a non-thread-safe version of <<fgets>>.
<<fgets_unlocked>> may only safely be used within a scope
protected by flockfile() (or ftrylockfile()) and funlockfile(). This
function may safely be used in a multi-threaded program if and only
if they are called while the invoking thread owns the (FILE *)
object, as is the case after a successful call to the flockfile() or
ftrylockfile() functions. If threads are disabled, then
<<fgets_unlocked>> is equivalent to <<fgets>>.
The functions <<_fgets_r>> and <<_fgets_unlocked_r>> are simply
reentrant versions that are passed the additional reentrant structure
pointer argument: <[ptr]>.
RETURNS
<<fgets>> returns the buffer passed to it, with the data
filled in. If end of file occurs with some data already
accumulated, the data is returned with no other indication. If
no data are read, NULL is returned instead.
PORTABILITY
<<fgets>> should replace all uses of <<gets>>. Note however
that <<fgets>> returns all of the data, while <<gets>> removes
the trailing newline (with no indication that it has done so.)
<<fgets_unlocked>> is a GNU extension.
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
*/
#define _DEFAULT_SOURCE
#include <_ansi.h>
#include <stdio.h>
#include <string.h>
#include "local.h"
#ifdef __IMPL_UNLOCKED__
#define _fgets_r _fgets_unlocked_r
#define fgets fgets_unlocked
#endif
/*
* Read at most n-1 characters from the given file.
* Stop when a newline has been read, or the count runs out.
* Return first argument, or NULL if no characters were read.
*/
char *
fgets (
char *__restrict buf,
int n,
FILE *__restrict fp)
{
size_t len;
char *s;
unsigned char *p, *t;
if (n < 2) /* sanity check */
return 0;
s = buf;
CHECK_INIT(ptr, fp);
_newlib_flockfile_start (fp);
#ifdef __SCLE
if (fp->_flags & __SCLE)
{
int c = 0;
/* Sorry, have to do it the slow way */
while (--n > 0 && (c = _sgetc ( fp)) != EOF)
{
*s++ = c;
if (c == '\n')
break;
}
if (c == EOF && s == buf)
{
_newlib_flockfile_exit (fp);
return NULL;
}
*s = 0;
_newlib_flockfile_exit (fp);
return buf;
}
#endif
n--; /* leave space for NUL */
do
{
/*
* If the buffer is empty, refill it.
*/
if ((len = fp->_r) <= 0)
{
if (_srefill ( fp))
{
/* EOF: stop with partial or no line */
if (s == buf)
{
_newlib_flockfile_exit (fp);
return 0;
}
break;
}
len = fp->_r;
}
p = fp->_p;
/*
* Scan through at most n bytes of the current buffer,
* looking for '\n'. If found, copy up to and including
* newline, and stop. Otherwise, copy entire chunk
* and loop.
*/
if (len > (size_t) n) /* n is always non-negative here */
len = (size_t) n;
t = (unsigned char *) memchr ((void *) p, '\n', len);
if (t != 0)
{
len = ++t - p;
fp->_r -= len;
fp->_p = t;
(void) memcpy ((void *) s, (void *) p, len);
s[len] = 0;
_newlib_flockfile_exit (fp);
return (buf);
}
fp->_r -= len;
fp->_p += len;
(void) memcpy ((void *) s, (void *) p, len);
s += len;
}
while ((n -= len) != 0);
*s = 0;
_newlib_flockfile_end (fp);
return buf;
}
|