1 /***********************************************************************/
\r
4 /* DATE :Wed, Jun 16, 2010 */
\r
5 /* DESCRIPTION :Program of I/O Stream */
\r
6 /* CPU TYPE :Other */
\r
8 /* This file is generated by Renesas Project Generator (Ver.4.50). */
\r
9 /* NOTE:THIS IS A TYPICAL EXAMPLE. */
\r
11 /***********************************************************************/
\r
15 /*********************************************************************
\r
19 * File Name : lowsrc.c
\r
21 * Abstract : Program of I/O Stream.
\r
23 * History : 1.00 (2009-08-07)
\r
25 * NOTE : THIS IS A TYPICAL EXAMPLE.
\r
27 * Copyright(c) 2009 Renesas Technology Corp.
\r
28 * And Renesas Solutions Corp.,All Rights Reserved.
\r
30 *********************************************************************/
\r
38 #define STDIN 0 /* Standard input (console) */
\r
39 #define STDOUT 1 /* Standard output (console) */
\r
40 #define STDERR 2 /* Standard error output (console) */
\r
42 #define FLMIN 0 /* Minimum file number */
\r
47 #define _MCREAT 0x10
\r
50 #define _MALBUF 0x40
\r
51 #define _MALFIL 0x80
\r
56 #define _MREAD 0x1000
\r
57 #define _MWRITE 0x2000
\r
58 #define _MBYTE 0x4000
\r
59 #define _MWIDE 0x8000
\r
61 #define O_RDONLY 0x0001 /* Read only */
\r
62 #define O_WRONLY 0x0002 /* Write only */
\r
63 #define O_RDWR 0x0004 /* Both read and Write */
\r
64 #define O_CREAT 0x0008 /* A file is created if it is not existed */
\r
65 #define O_TRUNC 0x0010 /* The file size is changed to 0 if it is existed. */
\r
66 #define O_APPEND 0x0020 /* The position is set for next reading/writing */
\r
67 /* 0: Top of the file 1: End of file */
\r
69 /* Special character code */
\r
70 #define CR 0x0d /* Carriage return */
\r
71 #define LF 0x0a /* Line feed */
\r
74 const long _nfiles = IOSTREAM; /* The number of files for input/output files */
\r
76 const int _nfiles = IOSTREAM; /* The number of files for input/output files */
\r
78 char flmod[IOSTREAM]; /* The location for the mode of opened file. */
\r
80 unsigned char sml_buf[IOSTREAM];
\r
82 #define FPATH_STDIN "C:\\stdin"
\r
83 #define FPATH_STDOUT "C:\\stdout"
\r
84 #define FPATH_STDERR "C:\\stderr"
\r
86 /* H8 Normal mode ,SH and RX */
\r
87 #if defined( __2000N__ ) || defined( __2600N__ ) || defined( __300HN__ ) || defined( _SH )
\r
88 /* Output one character to standard output */
\r
89 extern void charput(char);
\r
90 /* Input one character from standard input */
\r
91 extern char charget(void);
\r
92 /* Output one character to the file */
\r
93 extern char fcharput(char, unsigned char);
\r
94 /* Input one character from the file */
\r
95 extern char fcharget(char*, unsigned char);
\r
97 extern char fileopen(char*, unsigned char, unsigned char*);
\r
98 /* Close the file */
\r
99 extern char fileclose(unsigned char);
\r
100 /* Move the file offset */
\r
101 extern char fpseek(unsigned char, long, unsigned char);
\r
102 /* Get the file offset */
\r
103 extern char fptell(unsigned char, long*);
\r
106 #elif defined( __RX )
\r
107 /* Output one character to standard output */
\r
108 extern void charput(unsigned char);
\r
109 /* Input one character from standard input */
\r
110 extern unsigned char charget(void);
\r
112 /* H8 Advanced mode */
\r
113 #elif defined( __2000A__ ) || defined( __2600A__ ) || defined( __300HA__ ) || defined( __H8SXN__ ) || defined( __H8SXA__ ) || defined( __H8SXM__ ) || defined( __H8SXX__ )
\r
114 /* Output one character to standard output */
\r
115 extern void charput(char);
\r
116 /* Input one character from standard input */
\r
117 extern char charget(void);
\r
118 /* Output one character to the file */
\r
119 extern char fcharput(char, unsigned char);
\r
120 /* Input one character from the file */
\r
121 extern char fcharget(char*, unsigned char);
\r
122 /* Open the file */
\r
123 /* Specified as the number of register which stored paramter is 3 */
\r
124 extern char __regparam3 fileopen(char*, unsigned char, unsigned char*);
\r
125 /* Close the file */
\r
126 extern char fileclose(unsigned char);
\r
127 /* Move the file offset */
\r
128 extern char fpseek(unsigned char, long, unsigned char);
\r
129 /* Get the file offset */
\r
130 extern char fptell(unsigned char, long*);
\r
132 /* H8300 and H8300L */
\r
133 #elif defined( __300__ ) || defined( __300L__ )
\r
134 /* Output one character to standard output */
\r
135 extern void charput(char);
\r
136 /* Input one character from standard input */
\r
137 extern char charget(void);
\r
138 /* Output one character to the file */
\r
139 extern char fcharput(char, unsigned char);
\r
140 /* Input one character from the file */
\r
141 extern char fcharget(char*, unsigned char);
\r
142 /* Open the file */
\r
143 /* Specified as the number of register which stored paramter is 3 */
\r
144 extern char __regparam3 fileopen(char*, unsigned char, unsigned char*);
\r
145 /* Close the file */
\r
146 extern char fileclose(unsigned char);
\r
147 /* Move the file offset */
\r
148 /* Move the file offset */
\r
149 extern char __regparam3 fpseek(unsigned char, long, unsigned char);
\r
150 /* Get the file offset */
\r
151 extern char fptell(unsigned char, long*);
\r
155 FILE *_Files[IOSTREAM]; // structure for FILE
\r
156 char *env_list[] = { // Array for environment variables(**environ)
\r
160 '\0' // Terminal for environment variables
\r
163 char **environ = env_list;
\r
165 /****************************************************************************/
\r
167 /* Initialize C library Functions, if necessary. */
\r
168 /* Define USES_SIMIO on Assembler Option. */
\r
169 /****************************************************************************/
\r
170 void _INIT_IOLIB( void )
\r
172 /* A file for standard input/output is opened or created. Each FILE */
\r
173 /* structure members are initialized by the library. Each _Buf member */
\r
174 /* in it is re-set the end of buffer pointer. */
\r
176 /* Standard Input File */
\r
177 if( freopen( FPATH_STDIN, "r", stdin ) == NULL )
\r
178 stdin->_Mode = 0xffff; /* Not allow the access if it fails to open */
\r
179 stdin->_Mode = _MOPENR; /* Read only attribute */
\r
180 stdin->_Mode |= _MNBF; /* Non-buffering for data */
\r
181 stdin->_Bend = stdin->_Buf + 1; /* Re-set pointer to the end of buffer */
\r
183 /* Standard Output File */
\r
184 if( freopen( FPATH_STDOUT, "w", stdout ) == NULL )
\r
185 stdout->_Mode = 0xffff; /* Not allow the access if it fails to open */
\r
186 stdout->_Mode |= _MNBF; /* Non-buffering for data */
\r
187 stdout->_Bend = stdout->_Buf + 1;/* Re-set pointer to the end of buffer */
\r
189 /* Standard Error File */
\r
190 if( freopen( FPATH_STDERR, "w", stderr ) == NULL )
\r
191 stderr->_Mode = 0xffff; /* Not allow the access if it fails to open */
\r
192 stderr->_Mode |= _MNBF; /* Non-buffering for data */
\r
193 stderr->_Bend = stderr->_Buf + 1;/* Re-set pointer to the end of buffer */
\r
196 /****************************************************************************/
\r
198 /****************************************************************************/
\r
199 void _CLOSEALL( void )
\r
203 for( i=0; i < _nfiles; i++ )
\r
205 /* Checks if the file is opened or not */
\r
206 if( _Files[i]->_Mode & (_MOPENR | _MOPENW | _MOPENA ) )
\r
207 fclose( _Files[i] ); /* Closes the file */
\r
211 /**************************************************************************/
\r
212 /* open:file open */
\r
213 /* Return value:File number (Pass) */
\r
215 /**************************************************************************/
\r
216 #if defined( __RX )
\r
217 long open(const char *name, /* File name */
\r
218 long mode, /* Open mode */
\r
219 long flg) /* Open flag */
\r
221 int open(char *name, /* File name */
\r
222 int mode, /* Open mode */
\r
223 int flg) /* Open flag */
\r
228 if( strcmp( name, FPATH_STDIN ) == 0 ) /* Standard Input file? */
\r
230 if( ( mode & O_RDONLY ) == 0 ) return -1;
\r
231 flmod[STDIN] = mode;
\r
234 else if( strcmp( name, FPATH_STDOUT ) == 0 )/* Standard Output file? */
\r
236 if( ( mode & O_WRONLY ) == 0 ) return -1;
\r
237 flmod[STDOUT] = mode;
\r
240 else if(strcmp(name, FPATH_STDERR ) == 0 ) /* Standard Error file? */
\r
242 if( ( mode & O_WRONLY ) == 0 ) return -1;
\r
243 flmod[STDERR] = mode;
\r
246 else return -1; /*Others */
\r
249 #if defined( __RX )
\r
250 long close( long fileno )
\r
252 int close( int fileno )
\r
258 /**************************************************************************/
\r
259 /* write:Data write */
\r
260 /* Return value:Number of write characters (Pass) */
\r
262 /**************************************************************************/
\r
263 #if defined( __RX )
\r
264 long write(long fileno, /* File number */
\r
265 const unsigned char *buf, /* The address of destination buffer */
\r
266 long count) /* The number of chacter to write */
\r
268 int write(int fileno, /* File number */
\r
269 char *buf, /* The address of destination buffer */
\r
270 int count) /* The number of chacter to write */
\r
273 long i; /* A variable for counter */
\r
274 unsigned char c; /* An output character */
\r
276 /* Checking the mode of file , output each character */
\r
277 /* Checking the attribute for Write-Only, Read-Only or Read-Write */
\r
278 if(flmod[fileno]&O_WRONLY || flmod[fileno]&O_RDWR)
\r
280 if( fileno == STDIN ) return -1; /* Standard Input */
\r
281 else if( (fileno == STDOUT) || (fileno == STDERR) )
\r
282 /* Standard Error/output */
\r
284 for( i = count; i > 0; --i )
\r
289 return count; /*Return the number of written characters */
\r
291 else return -1; /* Incorrect file number */
\r
293 else return -1; /* An error */
\r
296 #if defined( __RX )
\r
297 long read( long fileno, unsigned char *buf, long count )
\r
299 int read( int fileno, char *buf, unsigned int count )
\r
304 /* Checking the file mode with the file number, each character is input and stored the buffer */
\r
306 if((flmod[fileno]&_MOPENR) || (flmod[fileno]&O_RDWR)){
\r
307 for(i = count; i > 0; i--){
\r
309 if(*buf==CR){ /* Replace the new line character */
\r
321 #if defined( __RX )
\r
322 long lseek( long fileno, long offset, long base )
\r
324 long lseek( int fileno, long offset, int base )
\r