Changeset 459 for trunk/libs/mini-libc


Ignore:
Timestamp:
Aug 13, 2018, 1:43:20 PM (4 years ago)
Author:
alain
Message:

Introduce the math library, to support the floating point
data used by the multi-thread fft application.
Fix several bugs regarding the FPU context save/restore.
Introduce support for the %f format in printf.

Location:
trunk/libs/mini-libc
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libs/mini-libc/stdio.c

    r457 r459  
    2626#include <almosmkh.h>
    2727#include <unistd.h>
     28#include <fcntl.h>
     29
     30////////////////////////////////////////////////////////////////////////////////////////
     31//          stdio library global variables
     32////////////////////////////////////////////////////////////////////////////////////////
     33
     34FILE open_file_array[MAX_OPEN_FILE_PER_PROCESS];  // array of open files structures
     35
     36////////////////////////////////////////////////////////////////////////////////////////
     37//          stdio library functions
     38////////////////////////////////////////////////////////////////////////////////////////
    2839
    2940//////////////////////////////////////////
     
    144155                break;
    145156            }
    146 /*
    147157            case ('f'):             // IEEE754 64 bits
    148158                                    // integer part : up to 10 decimal digits
     
    234244                break;
    235245            }
    236 */                   
    237246            default:       // unsupported argument type
    238247            {
     
    272281    {
    273282        string[count] = 0;
     283
    274284        return write( 1 , &string , count );
    275285    }
    276 }
     286}  // end printf()
    277287
    278288/////////////
     
    309319
    310320    return count;
    311 }
    312 
    313 
    314 
    315 
    316 
     321}  // end snprintf()
     322
     323////////////////////////////////////
     324FILE * fopen( const char * pathname,
     325              const char * mode )
     326{
     327    //TODO handle the "mode" argument
     328    if( mode != NULL )
     329    {
     330        printf("\n[ERROR] in %s : the mode argument must be NULL\n", __FUNCTION__ );
     331        return NULL;
     332    }
     333
     334    // get a file descriptor from kernel
     335    int fd = open( pathname,
     336                   O_CREAT | O_RDWR,
     337                   0 );
     338
     339    if( fd < 0 )
     340    {
     341        printf("\n[ERROR] in %s : file %s not found\n", __FUNCTION__ , pathname );
     342        return NULL;
     343    }
     344    if( fd > MAX_OPEN_FILE_PER_PROCESS )
     345    {
     346        printf("\n[ERROR] in %s : not enough space for file %s\n", __FUNCTION__ , pathname );
     347        return NULL;
     348    }
     349
     350    // register stream in open_file_array[]
     351    open_file_array[fd].fd  = fd;
     352    open_file_array[fd].key = VALID_OPEN_FILE;
     353
     354    return &open_file_array[fd];
     355}  // end fopen()
     356
     357///////////////////////////
     358int fclose( FILE * stream )
     359{
     360    // check stream valid
     361    if( stream->key != VALID_OPEN_FILE ) return EOF;
     362
     363    // get file descriptor from stream pointer
     364    int fd = stream->fd;
     365
     366    // remove stream from open_file_array[]
     367    open_file_array[fd].key = 0;
     368   
     369    return close( fd );
     370}  // end fclose()
     371
     372/////////////////////////////////
     373int fprintf( FILE       * stream,
     374             const char * format, ... )
     375{
     376    char      string[4096];
     377    va_list   args;
     378    int       count;
     379    int       fd;
     380   
     381    // check stream valid
     382    if( stream->key != VALID_OPEN_FILE ) return EOF;
     383
     384    va_start( args, format );
     385    count = xprintf( string , 4095 , format , &args );
     386    va_end( args );
     387
     388    if ( count == -1 )
     389    {
     390        display_string( "fprintf : xprintf failure" );
     391        return -1;
     392    }
     393    else
     394    {
     395        // get file descriptor from file pointer
     396        fd = stream->fd;
     397       
     398        string[count] = 0;
     399
     400        return write( fd , &string , count );
     401    }
     402}  // end fprintf()
     403
     404
     405
     406
     407
  • trunk/libs/mini-libc/stdio.h

    r445 r459  
    3131 ********************************************************************************************/
    3232
     33/*********************************************************************************************
     34 * This defines the user level FILE structure.
     35 ********************************************************************************************/
     36
     37#define  MAX_OPEN_FILE_PER_PROCESS  256
     38#define  VALID_OPEN_FILE            0x12345678
     39#define  EOF                        -1
     40#define  NULL                       (void *)0
     41
     42typedef struct file_s
     43{
     44    int fd;
     45    int key;
     46}
     47FILE;
    3348
    3449/*********************************************************************************************
     
    6782              const char   * format, ... );
    6883
     84/*********************************************************************************************
     85 * This function opens the file identified by the <pathname> argument and associates
     86 * the stream pointed by <FILE> with it.
     87 * The <mode> argument is a string that can have the following values:
     88 * - "r"   Open text file for reading.
     89 *         The stream is positioned at the beginning of the file.
     90 * - "r+"  Open for reading and writing.
     91 *         The stream is positioned at the beginning of the file.
     92 * - "w"   Truncate the file to zero length or create text file for writing.
     93 *         The stream is positioned at the beginning of the file.
     94 * - "w+"  Open for reading and writing.
     95 *         The file is created if it does not exist, otherwise it is truncated.
     96 *         The stream is positioned at the beginning of the file.
     97 * - "a"   Open for writing.  The file is created if it does not exist.
     98 *         The stream is positioned at the end of the file. 
     99 *         Subsequent writes to the file will always end up at the current end of file,
     100 *         irrespective of any intervening fseek() or similar.
     101 * - "a+"  Open for reading and writing. 
     102 *         The file is created if it does not exist. 
     103 *         The stream is positioned at the end of the file.
     104 *         Subsequent writes to the file will always end up at the current end of file,
     105 *         irrespective of any intervening fseek() or similar.
     106 *********************************************************************************************
     107 * @ pathname  : file pathname.
     108 * @ mode      : must be NULL <=> only "w+" mode is supported.
     109 * @ returns a stream pointer if success / returns NULL if file not found.
     110 ********************************************************************************************/
     111FILE * fopen( const char * pathname,
     112              const char * mode );
     113
     114/*********************************************************************************************
     115 * This function dissociates the stream from its underlying file and close this file.
     116 * If the stream was being used for output, any buffered data is written first.
     117 *********************************************************************************************
     118 * @ stream    : pointer on a stream.
     119 * @ returns 0 if success / returns EOF if failure.
     120 ********************************************************************************************/
     121int fclose( FILE * stream );
     122
     123/*********************************************************************************************
     124 * This function copies a formated string to an output stream identified by the <stream>
     125 * argument. It can be a  regular file or a character oriented output device.
     126 *********************************************************************************************
     127 * @ stream    : pointer on a stream.
     128 * @ format    : formated string.
     129 * @ returns number of characters written if success / returns -1 if failure.
     130 ********************************************************************************************/
     131int fprintf( FILE       * stream,
     132             const char * format, ... );
     133
     134
    69135#endif  // _STDIO_H_
  • trunk/libs/mini-libc/unistd.h

    r449 r459  
    4545
    4646/*****************************************************************************************
    47  * This function read bytes from an open file identified by its file descriptor.
     47 * This function read bytes from an open file identified by the <fd> file descriptor.
    4848 * This file can be a regular file or a character oriented device.
    4949 *****************************************************************************************
    50  * @ file_id  : open file index in fd_array.
     50 * @ fd       : open file index in fd_array.
    5151 * @ buf      : buffer virtual address in user space.
    5252 * @ count    : number of bytes.
     
    5858
    5959/*****************************************************************************************
    60  * This function writes bytes to an open file identified by its file descriptor.
     60 * This function writes bytes to an open file identified by the <fd> file descriptor.
    6161 * This file can be a regular file or character oriented device.
    6262 *****************************************************************************************
    63  * @ file_id  : open file index in fd_array.
     63 * @ fd       : open file index in fd_array.
    6464 * @ buf      : buffer virtual address in user space.
    6565 * @ count    : number of bytes.
     
    112112 * TODO not implemented yet...
    113113 *****************************************************************************************
    114  * @ file_id[0] : [out] read only file descriptor index.
    115  * @ file_id[1] : [out] write only file descriptor index.
     114 * @ fd[0] : [out] read only file descriptor index.
     115 * @ fd[1] : [out] write only file descriptor index.
    116116 * @ return 0 if success / return -1 if failure.
    117117 ****************************************************************************************/
Note: See TracChangeset for help on using the changeset viewer.