Subversion Repositories Kolibri OS

Rev

Rev 4874 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4349 Serge 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
 * advertising materials, and 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
<>---get character string from a file or stream
21
 
22
INDEX
23
	fgets
24
INDEX
25
	_fgets_r
26
 
27
ANSI_SYNOPSIS
28
        #include 
4921 Serge 29
	char *fgets(char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
4349 Serge 30
 
31
        #include 
4921 Serge 32
	char *_fgets_r(struct _reent *<[ptr]>, char *restrict <[buf]>, int <[n]>, FILE *restrict <[fp]>);
4349 Serge 33
 
34
TRAD_SYNOPSIS
35
	#include 
36
	char *fgets(<[buf]>,<[n]>,<[fp]>)
37
        char *<[buf]>;
38
	int <[n]>;
39
	FILE *<[fp]>;
40
 
41
	#include 
42
	char *_fgets_r(<[ptr]>, <[buf]>,<[n]>,<[fp]>)
43
	struct _reent *<[ptr]>;
44
        char *<[buf]>;
45
	int <[n]>;
46
	FILE *<[fp]>;
47
 
48
DESCRIPTION
49
	Reads at most <[n-1]> characters from <[fp]> until a newline
50
	is found. The characters including to the newline are stored
51
	in <[buf]>. The buffer is terminated with a 0.
52
 
53
	The <<_fgets_r>> function is simply the reentrant version of
54
	<> and is passed an additional reentrancy structure
55
	pointer: <[ptr]>.
56
 
57
RETURNS
58
	<> returns the buffer passed to it, with the data
59
	filled in. If end of file occurs with some data already
60
	accumulated, the data is returned with no other indication. If
61
	no data are read, NULL is returned instead.
62
 
63
PORTABILITY
64
	<> should replace all uses of <>. Note however
65
	that <> returns all of the data, while <> removes
66
	the trailing newline (with no indication that it has done so.)
67
 
68
Supporting OS subroutines required: <>, <>, <>,
69
<>, <>, <>, <>.
70
*/
71
 
72
#include <_ansi.h>
73
#include 
74
#include 
75
#include "local.h"
76
 
77
/*
78
 * Read at most n-1 characters from the given file.
79
 * Stop when a newline has been read, or the count runs out.
80
 * Return first argument, or NULL if no characters were read.
81
 */
82
 
83
char *
84
_DEFUN(_fgets_r, (ptr, buf, n, fp),
85
       struct _reent * ptr _AND
4921 Serge 86
       char *__restrict buf _AND
4349 Serge 87
       int n     _AND
4921 Serge 88
       FILE *__restrict fp)
4349 Serge 89
{
90
  size_t len;
91
  char *s;
92
  unsigned char *p, *t;
93
 
94
  if (n < 2)			/* sanity check */
95
    return 0;
96
 
97
  s = buf;
98
 
99
  CHECK_INIT(ptr, fp);
100
 
4921 Serge 101
  _newlib_flockfile_start (fp);
4349 Serge 102
#ifdef __SCLE
103
  if (fp->_flags & __SCLE)
104
    {
4921 Serge 105
      int c = 0;
4349 Serge 106
      /* Sorry, have to do it the slow way */
107
      while (--n > 0 && (c = __sgetc_r (ptr, fp)) != EOF)
108
	{
109
	  *s++ = c;
110
	  if (c == '\n')
111
	    break;
112
	}
113
      if (c == EOF && s == buf)
114
        {
4921 Serge 115
          _newlib_flockfile_exit (fp);
4349 Serge 116
          return NULL;
117
        }
118
      *s = 0;
4921 Serge 119
      _newlib_flockfile_exit (fp);
4349 Serge 120
      return buf;
121
    }
122
#endif
123
 
124
  n--;				/* leave space for NUL */
125
  do
126
    {
127
      /*
128
       * If the buffer is empty, refill it.
129
       */
130
      if ((len = fp->_r) <= 0)
131
	{
132
	  if (__srefill_r (ptr, fp))
133
	    {
134
	      /* EOF: stop with partial or no line */
135
	      if (s == buf)
136
                {
4921 Serge 137
                  _newlib_flockfile_exit (fp);
4349 Serge 138
                  return 0;
139
                }
140
	      break;
141
	    }
142
	  len = fp->_r;
143
	}
144
      p = fp->_p;
145
 
146
      /*
147
       * Scan through at most n bytes of the current buffer,
148
       * looking for '\n'.  If found, copy up to and including
149
       * newline, and stop.  Otherwise, copy entire chunk
150
       * and loop.
151
       */
152
      if (len > n)
153
	len = n;
154
      t = (unsigned char *) memchr ((_PTR) p, '\n', len);
155
      if (t != 0)
156
	{
157
	  len = ++t - p;
158
	  fp->_r -= len;
159
	  fp->_p = t;
160
	  _CAST_VOID memcpy ((_PTR) s, (_PTR) p, len);
161
	  s[len] = 0;
4921 Serge 162
          _newlib_flockfile_exit (fp);
4349 Serge 163
	  return (buf);
164
	}
165
      fp->_r -= len;
166
      fp->_p += len;
167
      _CAST_VOID memcpy ((_PTR) s, (_PTR) p, len);
168
      s += len;
169
    }
170
  while ((n -= len) != 0);
171
  *s = 0;
4921 Serge 172
  _newlib_flockfile_end (fp);
4349 Serge 173
  return buf;
174
}
175
 
176
#ifndef _REENT_ONLY
177
 
178
char *
179
_DEFUN(fgets, (buf, n, fp),
4921 Serge 180
       char *__restrict buf _AND
4349 Serge 181
       int n     _AND
4921 Serge 182
       FILE *__restrict fp)
4349 Serge 183
{
184
  return _fgets_r (_REENT, buf, n, fp);
185
}
186
 
187
#endif /* !_REENT_ONLY */