comparison packages/language/c/libc/stdio/current/src/input/fgets.cxx @ 156:b939e9cada46

Merge from eCos master repository on 2001-03-29-21:44:39-BST
author jlarmour
date Fri, 06 Apr 2001 17:20:13 +0000
parents 6ed91473a1cd
children e0c0827131d1
comparison
equal deleted inserted replaced
155:70a4cc6d69d2 156:b939e9cada46
1 //======================================================================== 1 //========================================================================
2 // 2 //
3 // fgets.cxx 3 // fgets.cxx
4 // 4 //
5 // ANSI Stdio fgets() function 5 // ISO C standard I/O fgets() function
6 // 6 //
7 //======================================================================== 7 //========================================================================
8 //####COPYRIGHTBEGIN#### 8 //####COPYRIGHTBEGIN####
9 // 9 //
10 // ------------------------------------------- 10 // -------------------------------------------
21 // The Original Code is eCos - Embedded Configurable Operating System, 21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998. 22 // released September 30, 1998.
23 // 23 //
24 // The Initial Developer of the Original Code is Red Hat. 24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are 25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. 26 // Copyright (C) 1998, 1999, 2000, 2001 Red Hat, Inc.
27 // All Rights Reserved. 27 // All Rights Reserved.
28 // ------------------------------------------- 28 // -------------------------------------------
29 // 29 //
30 //####COPYRIGHTEND#### 30 //####COPYRIGHTEND####
31 //======================================================================== 31 //========================================================================
32 //#####DESCRIPTIONBEGIN#### 32 //#####DESCRIPTIONBEGIN####
33 // 33 //
34 // Author(s): jlarmour 34 // Author(s): jlarmour
35 // Contributors: 35 // Contributors:
36 // Date: 2000-04-20 36 // Date: 2001-03-14
37 // Purpose: 37 // Purpose: Implementation of ISO C standard I/O fgets() function
38 // Description: 38 // Description:
39 // Usage: 39 // Usage:
40 // 40 //
41 //####DESCRIPTIONEND#### 41 //####DESCRIPTIONEND####
42 // 42 //
46 46
47 #include <pkgconf/libc_stdio.h> // Configuration header 47 #include <pkgconf/libc_stdio.h> // Configuration header
48 48
49 // INCLUDES 49 // INCLUDES
50 50
51 #include <cyg/infra/cyg_type.h> // Common project-wide type definitions 51 #include <cyg/infra/cyg_type.h> // Common type definitions
52 #include <cyg/infra/cyg_ass.h> // Assertion support
53 #include <cyg/infra/cyg_trac.h> // Tracing support
52 #include <stddef.h> // NULL and size_t from compiler 54 #include <stddef.h> // NULL and size_t from compiler
53 #include <stdio.h> // header for this file 55 #include <stdio.h> // header for this file
54 #include <errno.h> // error codes 56 #include <errno.h> // error codes
55 #include <cyg/libc/stdio/stream.hxx>// Cyg_StdioStream 57 #include <cyg/libc/stdio/stream.hxx>// Cyg_StdioStream
56 58
57 // FUNCTIONS 59 // FUNCTIONS
58 60
61 // FIXME: should be reworked to read buffer at a time, and scan that
62 // for newlines, rather than reading byte at a time.
63
59 externC char * 64 externC char *
60 fgets( char *s, int n, FILE *stream ) 65 fgets( char *s, int n, FILE *stream )
61 { 66 {
62 Cyg_StdioStream *real_stream = (Cyg_StdioStream *)stream; 67 Cyg_StdioStream *real_stream = (Cyg_StdioStream *)stream;
63 cyg_ucount32 bytes_read; 68 Cyg_ErrNo err=ENOERR;
64 Cyg_ErrNo err; 69 cyg_uint8 c;
70 cyg_uint8 *str=(cyg_uint8 *)s;
71 int nch;
72
73 CYG_CHECK_DATA_PTRC( s );
74 CYG_CHECK_DATA_PTRC( stream );
75 CYG_PRECONDITION( n > 0, "requested 0 or negative chars");
76
77 CYG_REPORT_FUNCTYPE( "returning string %08x");
78 CYG_REPORT_FUNCARG3( "s=%08x, n=%d, stream=%08x", s, n, stream );
65 79
80 for (nch=1; nch < n; nch++) {
81 err = real_stream->read_byte( &c );
82
83 // if nothing to read, try again ONCE after refilling buffer
84 if (EAGAIN == err) {
85 err = real_stream->refill_read_buffer();
86 if ( !err )
87 err = real_stream->read_byte( &c );
66 88
67 err = real_stream->read( (cyg_uint8 *)s, n-1, &bytes_read ); 89 if (EAGAIN == err) {
68 90 if (1 == nch) { // indicates EOF at start
69 if (!err && !bytes_read) { // if no err, but nothing to read, try again 91 CYG_REPORT_RETVAL( NULL );
70 err = real_stream->refill_read_buffer(); 92 return NULL;
71 if ( !err ) 93 } else
72 err = real_stream->read( (cyg_uint8 *)s, n-1, &bytes_read ); 94 break; // EOF
73 95 } // if
96 } // if
97
98 *str++ = c;
99 if ('\n' == c)
100 break;
101 } // while
102
103 *str = '\0'; // NULL terminate it
104
105 if (err && EAGAIN != err) {
106 real_stream->set_error( err );
107 errno = err;
108 CYG_REPORT_RETVAL( NULL );
109 return NULL;
74 } // if 110 } // if
75 111
76 if (err) { 112 CYG_REPORT_RETVAL( s );
77 real_stream->set_error( err );
78 errno = err;
79 return NULL;
80 } // if
81
82 if (bytes_read > 0)
83 s[bytes_read]='\0'; // NULL terminate it
84
85 return s; 113 return s;
86 114
87 } // fgets() 115 } // fgets()
88 116
89 // EOF fgets.cxx 117 // EOF fgets.cxx