OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [stdio/] [fgets.c] - Blame information for rev 308

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/*
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
<<fgets>>---get character string from a file or stream
21
 
22
INDEX
23
        fgets
24
INDEX
25
        _fgets_r
26
 
27
ANSI_SYNOPSIS
28
        #include <stdio.h>
29
        char *fgets(char *<[buf]>, int <[n]>, FILE *<[fp]>);
30
 
31
        #include <stdio.h>
32
        char *_fgets_r(struct _reent *<[ptr]>, char *<[buf]>, int <[n]>, FILE *<[fp]>);
33
 
34
TRAD_SYNOPSIS
35
        #include <stdio.h>
36
        char *fgets(<[buf]>,<[n]>,<[fp]>)
37
        char *<[buf]>;
38
        int <[n]>;
39
        FILE *<[fp]>;
40
 
41
        #include <stdio.h>
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
        <<fgets>> and is passed an additional reentrancy structure
55
        pointer: <[ptr]>.
56
 
57
RETURNS
58
        <<fgets>> 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
        <<fgets>> should replace all uses of <<gets>>. Note however
65
        that <<fgets>> returns all of the data, while <<gets>> removes
66
        the trailing newline (with no indication that it has done so.)
67
 
68
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
69
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
70
*/
71
 
72
#include <_ansi.h>
73
#include <stdio.h>
74
#include <string.h>
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
86
       char *buf _AND
87
       int n     _AND
88
       FILE * fp)
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
 
101
  _flockfile (fp);
102
#ifdef __SCLE
103
  if (fp->_flags & __SCLE)
104
    {
105
      int c;
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
        {
115
          _funlockfile (fp);
116
          return NULL;
117
        }
118
      *s = 0;
119
      _funlockfile (fp);
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
                {
137
                  _funlockfile (fp);
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;
162
          _funlockfile (fp);
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;
172
  _funlockfile (fp);
173
  return buf;
174
}
175
 
176
#ifndef _REENT_ONLY
177
 
178
char *
179
_DEFUN(fgets, (buf, n, fp),
180
       char *buf _AND
181
       int n     _AND
182
       FILE * fp)
183
{
184
  return _fgets_r (_REENT, buf, n, fp);
185
}
186
 
187
#endif /* !_REENT_ONLY */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.