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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdio/] [fgets.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 1010 ivang
/*
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
 
20
FUNCTION
21
        <<fgets>>---get character string from a file or stream
22
INDEX
23
        fgets
24
 
25
ANSI_SYNOPSIS
26
        #include <stdio.h>
27
        char *fgets(char *<[buf]>, int <[n]>, FILE *<[fp]>);
28
 
29
TRAD_SYNOPSIS
30
        #include <stdio.h>
31
        char *fgets(<[buf]>,<[n]>,<[fp]>)
32
        char *<[buf]>;
33
        int <[n]>;
34
        FILE *<[fp]>;
35
 
36
DESCRIPTION
37
        Reads at most <[n-1]> characters from <[fp]> until a newline
38
        is found. The characters including to the newline are stored
39
        in <[buf]>. The buffer is terminated with a 0.
40
 
41
 
42
RETURNS
43
        <<fgets>> returns the buffer passed to it, with the data
44
        filled in. If end of file occurs with some data already
45
        accumulated, the data is returned with no other indication. If
46
        no data are read, NULL is returned instead.
47
 
48
PORTABILITY
49
        <<fgets>> should replace all uses of <<gets>>. Note however
50
        that <<fgets>> returns all of the data, while <<gets>> removes
51
        the trailing newline (with no indication that it has done so.)
52
 
53
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
54
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
55
*/
56
 
57
#include <stdio.h>
58
#include <string.h>
59
 
60
extern int __srefill ();
61
 
62
/*
63
 * Read at most n-1 characters from the given file.
64
 * Stop when a newline has been read, or the count runs out.
65
 * Return first argument, or NULL if no characters were read.
66
 */
67
 
68
char *
69
_DEFUN (fgets, (buf, n, fp),
70
        char *buf _AND
71
        int n _AND
72
        FILE * fp)
73
{
74
  size_t len;
75
  char *s;
76
  unsigned char *p, *t;
77
 
78
  if (n < 2)                    /* sanity check */
79
    return 0;
80
 
81
  s = buf;
82
 
83
#ifdef __SCLE
84
  if (fp->_flags & __SCLE)
85
    {
86
      int c;
87
      /* Sorry, have to do it the slow way */
88
      while (--n > 0 && (c = __sgetc(fp)) != EOF)
89
        {
90
          *s++ = c;
91
          if (c == '\n')
92
            break;
93
        }
94
      if (c == EOF && s == buf)
95
        return NULL;
96
      *s = 0;
97
      return buf;
98
    }
99
#endif
100
 
101
  n--;                          /* leave space for NUL */
102
  do
103
    {
104
      /*
105
       * If the buffer is empty, refill it.
106
       */
107
      if ((len = fp->_r) <= 0)
108
        {
109
          if (__srefill (fp))
110
            {
111
              /* EOF: stop with partial or no line */
112
              if (s == buf)
113
                return 0;
114
              break;
115
            }
116
          len = fp->_r;
117
        }
118
      p = fp->_p;
119
 
120
      /*
121
       * Scan through at most n bytes of the current buffer,
122
       * looking for '\n'.  If found, copy up to and including
123
       * newline, and stop.  Otherwise, copy entire chunk
124
       * and loop.
125
       */
126
      if (len > n)
127
        len = n;
128
      t = (unsigned char *) memchr ((_PTR) p, '\n', len);
129
      if (t != 0)
130
        {
131
          len = ++t - p;
132
          fp->_r -= len;
133
          fp->_p = t;
134
          (void) memcpy ((_PTR) s, (_PTR) p, len);
135
          s[len] = 0;
136
          return (buf);
137
        }
138
      fp->_r -= len;
139
      fp->_p += len;
140
      (void) memcpy ((_PTR) s, (_PTR) p, len);
141
      s += len;
142
    }
143
  while ((n -= len) != 0);
144
  *s = 0;
145
  return buf;
146
}

powered by: WebSVN 2.1.0

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