1 /***********************************************************************************************************************
\r
3 * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. No
\r
4 * other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
\r
5 * applicable laws, including copyright laws.
\r
6 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
\r
7 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
\r
8 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. TO THE MAXIMUM
\r
9 * EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES
\r
10 * SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS
\r
11 * SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
\r
12 * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of
\r
13 * this software. By using this software, you agree to the additional terms and conditions found by accessing the
\r
15 * http://www.renesas.com/disclaimer
\r
17 * Copyright (C) 2012 Renesas Electronics Corporation. All rights reserved.
\r
18 ***********************************************************************************************************************/
\r
19 /***********************************************************************************************************************
\r
20 * File Name : lowsrc.c
\r
21 * Description : Functions to support stream I/O
\r
22 ***********************************************************************************************************************/
\r
23 /***********************************************************************************************************************
\r
24 * History : DD.MM.YYYY Version Description
\r
25 * : 26.10.2011 1.00 First Release
\r
26 ***********************************************************************************************************************/
\r
27 /***********************************************************************************************************************
\r
28 Includes <System Includes> , "Project Includes"
\r
29 ***********************************************************************************************************************/
\r
34 /***********************************************************************************************************************
\r
36 ***********************************************************************************************************************/
\r
37 /*Number of I/O Stream*/
41 #define STDIN 0 /* Standard input (console) */
\r
42 #define STDOUT 1 /* Standard output (console) */
\r
43 #define STDERR 2 /* Standard error output (console) */
\r
45 #define FLMIN 0 /* Minimum file number */
\r
50 #define _MCREAT 0x10
\r
53 #define _MALBUF 0x40
\r
54 #define _MALFIL 0x80
\r
59 #define _MREAD 0x1000
\r
60 #define _MWRITE 0x2000
\r
61 #define _MBYTE 0x4000
\r
62 #define _MWIDE 0x8000
\r
64 #define O_RDONLY 0x0001 /* Read only */
\r
65 #define O_WRONLY 0x0002 /* Write only */
\r
66 #define O_RDWR 0x0004 /* Both read and Write */
\r
67 #define O_CREAT 0x0008 /* A file is created if it is not existed */
\r
68 #define O_TRUNC 0x0010 /* The file size is changed to 0 if it is existed. */
\r
69 #define O_APPEND 0x0020 /* The position is set for next reading/writing */
\r
70 /* 0: Top of the file 1: End of file */
\r
72 /* Special character code */
\r
73 #define CR 0x0d /* Carriage return */
\r
74 #define LF 0x0a /* Line feed */
\r
77 const long _nfiles = IOSTREAM; /* The number of files for input/output files */
\r
79 const int _nfiles = IOSTREAM; /* The number of files for input/output files */
\r
81 char flmod[IOSTREAM]; /* The location for the mode of opened file. */
\r
83 unsigned char sml_buf[IOSTREAM];
\r
85 #define FPATH_STDIN "C:\\stdin"
\r
86 #define FPATH_STDOUT "C:\\stdout"
\r
87 #define FPATH_STDERR "C:\\stderr"
\r
89 /* H8 Normal mode ,SH and RX */
\r
90 #if defined( __2000N__ ) || defined( __2600N__ ) || defined( __300HN__ ) || defined( _SH )
\r
91 /* Output one character to standard output */
\r
92 extern void charput(char);
\r
93 /* Input one character from standard input */
\r
94 extern char charget(void);
\r
95 /* Output one character to the file */
\r
96 extern char fcharput(char, unsigned char);
\r
97 /* Input one character from the file */
\r
98 extern char fcharget(char*, unsigned char);
\r
100 extern char fileopen(char*, unsigned char, unsigned char*);
\r
101 /* Close the file */
\r
102 extern char fileclose(unsigned char);
\r
103 /* Move the file offset */
\r
104 extern char fpseek(unsigned char, long, unsigned char);
\r
105 /* Get the file offset */
\r
106 extern char fptell(unsigned char, long*);
\r
109 #elif defined( __RX )
\r
110 /* Output one character to standard output */
\r
111 extern void charput(unsigned char);
\r
112 /* Input one character from standard input */
\r
113 extern unsigned char charget(void);
\r
115 /* H8 Advanced mode */
\r
116 #elif defined( __2000A__ ) || defined( __2600A__ ) || defined( __300HA__ ) || defined( __H8SXN__ ) || defined( __H8SXA__ ) || defined( __H8SXM__ ) || defined( __H8SXX__ )
\r
117 /* Output one character to standard output */
\r
118 extern void charput(char);
\r
119 /* Input one character from standard input */
\r
120 extern char charget(void);
\r
121 /* Output one character to the file */
\r
122 extern char fcharput(char, unsigned char);
\r
123 /* Input one character from the file */
\r
124 extern char fcharget(char*, unsigned char);
\r
125 /* Open the file */
\r
126 /* Specified as the number of register which stored paramter is 3 */
\r
127 extern char __regparam3 fileopen(char*, unsigned char, unsigned char*);
\r
128 /* Close the file */
\r
129 extern char fileclose(unsigned char);
\r
130 /* Move the file offset */
\r
131 extern char fpseek(unsigned char, long, unsigned char);
\r
132 /* Get the file offset */
\r
133 extern char fptell(unsigned char, long*);
\r
135 /* H8300 and H8300L */
\r
136 #elif defined( __300__ ) || defined( __300L__ )
\r
137 /* Output one character to standard output */
\r
138 extern void charput(char);
\r
139 /* Input one character from standard input */
\r
140 extern char charget(void);
\r
141 /* Output one character to the file */
\r
142 extern char fcharput(char, unsigned char);
\r
143 /* Input one character from the file */
\r
144 extern char fcharget(char*, unsigned char);
\r
145 /* Open the file */
\r
146 /* Specified as the number of register which stored paramter is 3 */
\r
147 extern char __regparam3 fileopen(char*, unsigned char, unsigned char*);
\r
148 /* Close the file */
\r
149 extern char fileclose(unsigned char);
\r
150 /* Move the file offset */
\r
151 /* Move the file offset */
\r
152 extern char __regparam3 fpseek(unsigned char, long, unsigned char);
\r
153 /* Get the file offset */
\r
154 extern char fptell(unsigned char, long*);
\r
158 FILE *_Files[IOSTREAM]; // structure for FILE
\r
159 char *env_list[] = { // Array for environment variables(**environ)
\r
163 '\0' // Terminal for environment variables
\r
166 char **environ = env_list;
\r
168 /****************************************************************************/
\r
170 /* Initialize C library Functions, if necessary. */
\r
171 /* Define USES_SIMIO on Assembler Option. */
\r
172 /****************************************************************************/
\r
173 void _INIT_IOLIB( void )
\r
175 /* A file for standard input/output is opened or created. Each FILE */
\r
176 /* structure members are initialized by the library. Each _Buf member */
\r
177 /* in it is re-set the end of buffer pointer. */
\r
179 /* Standard Input File */
\r
180 if( freopen( FPATH_STDIN, "r", stdin ) == NULL )
\r
181 stdin->_Mode = 0xffff; /* Not allow the access if it fails to open */
\r
182 stdin->_Mode = _MOPENR; /* Read only attribute */
\r
183 stdin->_Mode |= _MNBF; /* Non-buffering for data */
\r
184 stdin->_Bend = stdin->_Buf + 1; /* Re-set pointer to the end of buffer */
\r
186 /* Standard Output File */
\r
187 if( freopen( FPATH_STDOUT, "w", stdout ) == NULL )
\r
188 stdout->_Mode = 0xffff; /* Not allow the access if it fails to open */
\r
189 stdout->_Mode |= _MNBF; /* Non-buffering for data */
\r
190 stdout->_Bend = stdout->_Buf + 1;/* Re-set pointer to the end of buffer */
\r
192 /* Standard Error File */
\r
193 if( freopen( FPATH_STDERR, "w", stderr ) == NULL )
\r
194 stderr->_Mode = 0xffff; /* Not allow the access if it fails to open */
\r
195 stderr->_Mode |= _MNBF; /* Non-buffering for data */
\r
196 stderr->_Bend = stderr->_Buf + 1;/* Re-set pointer to the end of buffer */
\r
199 /****************************************************************************/
\r
201 /****************************************************************************/
\r
202 void _CLOSEALL( void )
\r
206 for( i=0; i < _nfiles; i++ )
\r
208 /* Checks if the file is opened or not */
\r
209 if( _Files[i]->_Mode & (_MOPENR | _MOPENW | _MOPENA ) )
\r
210 fclose( _Files[i] ); /* Closes the file */
\r
214 /**************************************************************************/
\r
215 /* open:file open */
\r
216 /* Return value:File number (Pass) */
\r
218 /**************************************************************************/
\r
219 #if defined( __RX )
\r
220 long open(const char *name, /* File name */
\r
221 long mode, /* Open mode */
\r
222 long flg) /* Open flag */
\r
224 int open(char *name, /* File name */
\r
225 int mode, /* Open mode */
\r
226 int flg) /* Open flag */
\r
231 if( strcmp( name, FPATH_STDIN ) == 0 ) /* Standard Input file? */
\r
233 if( ( mode & O_RDONLY ) == 0 ) return -1;
\r
234 flmod[STDIN] = mode;
\r
237 else if( strcmp( name, FPATH_STDOUT ) == 0 )/* Standard Output file? */
\r
239 if( ( mode & O_WRONLY ) == 0 ) return -1;
\r
240 flmod[STDOUT] = mode;
\r
243 else if(strcmp(name, FPATH_STDERR ) == 0 ) /* Standard Error file? */
\r
245 if( ( mode & O_WRONLY ) == 0 ) return -1;
\r
246 flmod[STDERR] = mode;
\r
249 else return -1; /*Others */
\r
252 #if defined( __RX )
\r
253 long close( long fileno )
\r
255 int close( int fileno )
\r
261 /**************************************************************************/
\r
262 /* write:Data write */
\r
263 /* Return value:Number of write characters (Pass) */
\r
265 /**************************************************************************/
\r
266 #if defined( __RX )
\r
267 long write(long fileno, /* File number */
\r
268 const unsigned char *buf, /* The address of destination buffer */
\r
269 long count) /* The number of chacter to write */
\r
271 int write(int fileno, /* File number */
\r
272 char *buf, /* The address of destination buffer */
\r
273 int count) /* The number of chacter to write */
\r
276 long i; /* A variable for counter */
\r
277 unsigned char c; /* An output character */
\r
279 /* Checking the mode of file , output each character */
\r
280 /* Checking the attribute for Write-Only, Read-Only or Read-Write */
\r
281 if(flmod[fileno]&O_WRONLY || flmod[fileno]&O_RDWR)
\r
283 if( fileno == STDIN ) return -1; /* Standard Input */
\r
284 else if( (fileno == STDOUT) || (fileno == STDERR) )
\r
285 /* Standard Error/output */
\r
287 for( i = count; i > 0; --i )
\r
292 return count; /*Return the number of written characters */
\r
294 else return -1; /* Incorrect file number */
\r
296 else return -1; /* An error */
\r
299 #if defined( __RX )
\r
300 long read( long fileno, unsigned char *buf, long count )
\r
302 int read( int fileno, char *buf, unsigned int count )
\r
307 /* Checking the file mode with the file number, each character is input and stored the buffer */
\r
309 if((flmod[fileno]&_MOPENR) || (flmod[fileno]&O_RDWR)){
\r
310 for(i = count; i > 0; i--){
\r
312 if(*buf==CR){ /* Replace the new line character */
\r
324 #if defined( __RX )
\r
325 long lseek( long fileno, long offset, long base )
\r
327 long lseek( int fileno, long offset, int base )
\r