2 * AcessOS Basic C Library
\r
6 #include <acess/sys.h>
\r
11 #include "stdio_int.h"
\r
13 #define DEBUG_BUILD 0
\r
15 // === CONSTANTS ===
\r
19 // === PROTOTYPES ===
\r
20 EXPORT void itoa(char *buf, uint64_t num, uint base, int minLength, char pad, int bSigned);
\r
21 struct sFILE *get_file_struct();
\r
24 struct sFILE _iob[STDIO_MAX_STREAMS]; // IO Buffer
\r
25 struct sFILE *stdin; // Standard Input
\r
26 struct sFILE *stdout; // Standard Output
\r
27 struct sFILE *stderr; // Standard Error
\r
28 ///\note Initialised in SoMain
\r
32 * \fn FILE *freopen(char *file, char *mode, FILE *fp)
\r
34 EXPORT FILE *freopen(const char *file, const char *mode, FILE *fp)
\r
39 // Sanity Check Arguments
\r
40 if(!fp || !file || !mode) return NULL;
\r
50 case 'r': fp->Flags = FILE_FLAG_MODE_READ; break;
\r
51 case 'w': fp->Flags = FILE_FLAG_MODE_WRITE; break;
\r
52 case 'a': fp->Flags = FILE_FLAG_MODE_APPEND; break;
\r
53 case 'x': fp->Flags = FILE_FLAG_MODE_EXEC; break;
\r
58 for(i=1;mode[i];i++)
\r
62 case '+': fp->Flags |= FILE_FLAG_M_EXT;
\r
70 case 'r': openFlags = OPENFLAG_READ;
\r
71 if(fp->Flags & FILE_FLAG_M_EXT)
\r
72 openFlags |= OPENFLAG_WRITE;
\r
75 case 'w': openFlags = OPENFLAG_WRITE;
\r
76 if(fp->Flags & FILE_FLAG_M_EXT)
\r
77 openFlags |= OPENFLAG_READ;
\r
80 case 'x': openFlags = OPENFLAG_EXEC;
\r
86 fp->FD = reopen(fp->FD, file, openFlags);
\r
88 fp->FD = open(file, openFlags);
\r
94 if(mode[0] == 'a') {
\r
95 seek(fp->FD, 0, SEEK_END); //SEEK_END
\r
101 \fn FILE *fopen(const char *file, const char *mode)
\r
102 \brief Opens a file and returns the pointer
\r
103 \param file String - Filename to open
\r
104 \param mode Mode to open in
\r
106 EXPORT FILE *fopen(const char *file, const char *mode)
\r
110 // Sanity Check Arguments
\r
111 if(!file || !mode) return NULL;
\r
113 // Create Return Structure
\r
114 retFile = get_file_struct();
\r
116 return freopen(file, mode, retFile);
\r
119 EXPORT void fclose(FILE *fp)
\r
125 EXPORT void fflush(FILE *fp)
\r
130 EXPORT long int ftell(FILE *fp)
\r
132 if(!fp || !fp->FD) return -1;
\r
134 return tell(fp->FD);
\r
137 EXPORT int fseek(FILE *fp, long int amt, int whence)
\r
139 if(!fp || !fp->FD) return -1;
\r
141 return seek(fp->FD, amt, whence);
\r
146 * \fn EXPORT int vfprintf(FILE *fp, const char *format, va_list args)
\r
147 * \brief Print to a file from a variable argument list
\r
149 EXPORT int vfprintf(FILE *fp, const char *format, va_list args)
\r
151 va_list tmpList = args;
\r
156 if(!fp || !format) return -1;
\r
158 size = vsnprintf(sbuf, 1024, (char*)format, tmpList);
\r
162 buf = (char*)malloc(size+1);
\r
164 write(_stdout, 31, "vfprintf ERROR: malloc() failed");
\r
170 vsnprintf(buf, size+1, (char*)format, args);
\r
174 write(fp->FD, size, buf);
\r
179 // Return written byte count
\r
184 * \fn int fprintf(FILE *fp, const char *format, ...)
\r
185 * \brief Print a formatted string to a stream
\r
187 EXPORT int fprintf(FILE *fp, const char *format, ...)
\r
193 va_start(args, format);
\r
194 ret = vfprintf(fp, (char*)format, args);
\r
201 * \fn EXPORT size_t fwrite(void *ptr, size_t size, size_t num, FILE *fp)
\r
202 * \brief Write to a stream
\r
204 EXPORT size_t fwrite(void *ptr, size_t size, size_t num, FILE *fp)
\r
207 if(!fp || !fp->FD) return -1;
\r
209 ret = write(fp->FD, size*num, ptr);
\r
215 * \fn EXPORT size_t fread(void *ptr, size_t size, size_t num, FILE *fp)
\r
216 * \brief Read from a stream
\r
218 EXPORT size_t fread(void *ptr, size_t size, size_t num, FILE *fp)
\r
221 if(!fp || !fp->FD) return -1;
\r
223 ret = read(fp->FD, size*num, ptr);
\r
229 * \fn EXPORT int fputc(int c, FILE *fp)
\r
230 * \brief Write a single character to the stream
\r
232 EXPORT int fputc(int c, FILE *fp)
\r
234 if(!fp || !fp->FD) return -1;
\r
235 return write(fp->FD, 1, &c);
\r
239 * \fn EXPORT int fgetc(FILE *fp)
\r
240 * \brief Read a character from the stream
\r
242 EXPORT int fgetc(FILE *fp)
\r
246 if(read(fp->FD, 1, &ret) == -1) return -1;
\r
250 // --- INTERNAL ---
\r
252 * \fn FILE *get_file_struct()
\r
253 * \brief Returns a file descriptor structure
\r
255 FILE *get_file_struct()
\r
258 for(i=0;i<STDIO_MAX_STREAMS;i++)
\r
260 if(_iob[i].Flags == 0) return &_iob[i];
\r
265 EXPORT int putchar(int ch)
\r
267 return write(_stdout, 1, (char*)&ch);
\r
270 EXPORT int puts(const char *str)
\r
277 len = write(_stdout, len, (char*)str);
\r
278 write(_stdout, 1, "\n");
\r
282 EXPORT int vsprintf(char * __s, const char *__format, va_list __args)
\r
284 return vsnprintf(__s, 0x7FFFFFFF, __format, __args);
\r
289 \fn EXPORT void vsnprintf(char *buf, const char *format, va_list args)
\r
290 \brief Prints a formatted string to a buffer
\r
291 \param buf Pointer - Destination Buffer
\r
292 \param format String - Format String
\r
293 \param args VarArgs List - Arguments
\r
295 EXPORT int vsnprintf(char *buf, size_t __maxlen, const char *format, va_list args)
\r
307 while((c = *format++) != 0)
\r
309 // Non-control character
\r
311 if(buf && pos < __maxlen) buf[pos] = c;
\r
316 // Control Character
\r
318 if(c == '%') { // Literal %
\r
319 if(buf && pos < __maxlen) buf[pos] = '%';
\r
331 if('1' <= c && c <= '9')
\r
333 while('0' <= c && c <= '9')
\r
336 minSize += c - '0';
\r
341 // Check for long long
\r
357 case 'd': case 'i':
\r
359 if(bLongLong) arg = va_arg(args, int64_t);
\r
360 else arg = va_arg(args, int32_t);
\r
361 itoa(tmp, arg, 10, minSize, pad, 1);
\r
364 // Unsigned Integer
\r
367 if(bLongLong) arg = va_arg(args, uint64_t);
\r
368 else arg = va_arg(args, uint32_t);
\r
369 itoa(tmp, arg, 10, minSize, pad, 0);
\r
374 if(buf && pos+2 < __maxlen) {
\r
380 arg = va_arg(args, uint32_t);
\r
381 itoa(tmp, arg, 16, minSize, pad, 0);
\r
383 // Fall through to hex
\r
384 // Unsigned Hexadecimal
\r
386 if(bLongLong) arg = va_arg(args, uint64_t);
\r
387 else arg = va_arg(args, uint32_t);
\r
388 itoa(tmp, arg, 16, minSize, pad, 0);
\r
393 if(bLongLong) arg = va_arg(args, uint64_t);
\r
394 else arg = va_arg(args, uint32_t);
\r
395 itoa(tmp, arg, 8, minSize, pad, 0);
\r
400 if(bLongLong) arg = va_arg(args, uint64_t);
\r
401 else arg = va_arg(args, uint32_t);
\r
402 itoa(tmp, arg, 2, minSize, pad, 0);
\r
407 arg = va_arg(args, uint32_t);
\r
408 p = (void*)(intptr_t)arg;
\r
410 if(!p) p = "(null)";
\r
411 //_SysDebug("vsnprintf: p = '%s'", p);
\r
414 if(pos < __maxlen) buf[pos] = *p;
\r
425 // Unknown, just treat it as a character
\r
427 arg = va_arg(args, uint32_t);
\r
428 if(buf && pos < __maxlen) buf[pos] = arg;
\r
433 if(buf && pos < __maxlen) buf[pos] = '\0';
\r
435 //_SysDebug("vsnprintf: buf = '%s'", buf);
\r
440 const char cUCDIGITS[] = "0123456789ABCDEF";
\r
442 * \fn static void itoa(char *buf, uint64_t num, int base, int minLength, char pad, int bSigned)
\r
443 * \brief Convert an integer into a character string
\r
445 EXPORT void itoa(char *buf, uint64_t num, size_t base, int minLength, char pad, int bSigned)
\r
451 if(base > 16 || base < 2) {
\r
456 if(bSigned && (int64_t)num < 0)
\r
463 while(num > base-1) {
\r
464 tmpBuf[pos++] = cUCDIGITS[ num % base ];
\r
465 num = (uint64_t) num / (uint64_t)base; // Shift {number} right 1 digit
\r
468 tmpBuf[pos++] = cUCDIGITS[ num % base ]; // Last digit of {number}
\r
469 if(bSigned) tmpBuf[pos++] = '-'; // Append sign symbol if needed
\r
473 while(minLength-- > 0) buf[i++] = pad;
\r
474 while(pos-- > 0) buf[i++] = tmpBuf[pos]; // Reverse the order of characters
\r
479 * \fn EXPORT int printf(const char *format, ...)
\r
480 * \brief Print a string to stdout
\r
482 EXPORT int printf(const char *format, ...)
\r
491 va_start(args, format);
\r
492 size = vsnprintf(sbuf, 1024, (char*)format, args);
\r
495 if( size >= 1024 ) {
\r
497 buf = (char*)malloc(size+1);
\r
499 write(_stdout, 29, "PRINTF ERROR: malloc() failed");
\r
505 va_start(args, format);
\r
506 vsnprintf(buf, size+1, (char*)format, args);
\r
511 write(_stdout, size+1, buf);
\r
522 va_start(args, format);
\r
523 ret = fprintfv(stdout, (char*)format, args);
\r
530 * \fn EXPORT int sprintf(const char *buf, char *format, ...)
\r
531 * \brief Print a formatted string to a buffer
\r
533 EXPORT int sprintf(char *buf, const char *format, ...)
\r
537 va_start(args, format);
\r
538 ret = vsprintf((char*)buf, (char*)format, args);
\r