Ignore:
Timestamp:
Nov 20, 2020, 12:18:00 AM (3 years ago)
Author:
alain
Message:

Introduce the fgetc() and fputc() functions.

File:
1 edited

Legend:

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

    r647 r677  
    3535////////////////////////////////////////////////////////////////////////////////////////
    3636
    37 // This user space array registers all FILE descriptors open by a given process
    38 FILE open_file_array[MAX_OPEN_FILE_PER_PROCESS];  // array of open files structures
     37// This user space array registers all FILE descriptors that can be simultaneously
     38// open by a given process. The structure FILE is defined in the <stdio.h> file.
     39
     40FILE open_file_array[MAX_OPEN_FILE_PER_PROCESS]; 
    3941
    4042////////////////////////////////////////////////////////////////////////////////////////
     
    139141                break;
    140142            }
    141             case ('x'):             // 32 bits hexadecimal
    142             case ('l'):             // 64 bits hexadecimal
    143             {
    144                 unsigned int       imax;
    145                 unsigned long long val;
    146                
    147                 if ( *format == 'l' )   // 64 bits
    148                 {
    149                     val = va_arg( *args, unsigned long long);
    150                     imax = 16;
    151                 }
    152                 else                    // 32 bits
    153                 {
    154                     val = va_arg( *args, unsigned int);
    155                     imax = 8;
    156                 }
    157                
     143            case ('x'):             // up to 8 digits hexa after "0x"
     144            case ('X'):             // exactly 8 digits hexa after "0x"
     145            {
     146                unsigned int val = va_arg( *args , unsigned int );
    158147                TO_STREAM( '0' );
    159148                TO_STREAM( 'x' );
    160                
    161                 for(i = 0; i < imax; i++)
    162                 {
    163                     buf[(imax-1) - i] = HexaTab[val % 16];
    164                     if (!(val /= 16))  break;
     149                for(i = 0 ; i < 8 ; i++)
     150                {
     151                    buf[7 - i] = HexaTab[val & 0xF];
     152                    if( (*format == 'x') && ((val >> 4) == 0) )  break;
     153                    val = val >> 4;
    165154                }
    166155                len =  i + 1;
    167                 pbuf = &buf[(imax-1) - i];
     156                pbuf = &buf[7 - i];
     157                break;
     158            }
     159            case ('l'):             // up to 16 digits hexa after "0x"
     160            case ('L'):             // exactly 16 digits hexa after "0x"
     161            {
     162                unsigned long long val = ((unsigned long long)va_arg( *args, unsigned int)) |
     163                                         ((unsigned long long)va_arg( *args, unsigned int) << 32);
     164                TO_STREAM( '0' );
     165                TO_STREAM( 'x' );
     166                for(i = 0 ; i < 16 ; i++)
     167                {
     168                    buf[15 - i] = HexaTab[val & 0xF];
     169                    if( (*format == 'l') && ((val >> 4) == 0) )  break;
     170                    val = val >> 4;
     171                }
     172                len =  i + 1;
     173                pbuf = &buf[15 - i];
    168174                break;
    169175            }
    170176            case ('s'):             /* string */
    171177            {
    172                 char* str = va_arg( *args, char* );
     178                char * str = va_arg( *args , char* );
    173179                while (str[len]) { len++; }
    174180                pbuf = str;
     
    272278        format++;
    273279
    274         // copy argument to string
     280        // copy argument sub-string to the string buffer
    275281        for( i = 0 ; i < len ; i++ )
    276282        {
     
    285291int printf( const char * format, ... )
    286292{
    287     char               string[4096];
    288     va_list            args;
    289     unsigned int       count;
     293    char      string[4096];
     294    va_list   args;
     295    int       count;
    290296   
    291297    va_start( args, format );
     
    293299    va_end( args );
    294300
    295     if ( count == 0xFFFFFFFF )
     301    if ( count < 0 )
    296302    {
    297303        display_string( "printf : xprintf failure" );
     
    329335              const char     * format, ... )
    330336{
    331     va_list            args;
    332     unsigned int       count;
     337    va_list   args;
     338    int       count;
    333339
    334340    va_start( args, format );
     
    336342    va_end( args );
    337343
    338     if( count < length ) string[count] = 0;
    339 
    340     return count;
     344    if( (count < 0) || (count == (int)length) )  // failure
     345    {
     346        return -1;
     347    }
     348    else                                        // success
     349    {
     350        // add NUL character
     351        string[count] = 0;
     352
     353        return count;
     354    }
    341355}  // end snprintf()
     356
     357
     358
     359
    342360
    343361////////////////////////////////////
     
    398416
    399417}  // end fclose()
     418
     419//////////////////////////
     420int fgetc( FILE * stream )
     421{
     422    // check stream valid
     423    if( stream->key != VALID_OPEN_FILE ) return EOF;
     424
     425    // get file descriptor from stream pointer
     426    int fd = stream->fd;
     427
     428    char byte;
     429
     430    if ( read( fd , &byte , 1 ) != 1 ) return 0;
     431    else                               return (int)byte;
     432
     433}
     434
     435/////////////////
     436int fputc( int c,
     437           FILE * stream)
     438{
     439    // check stream valid
     440    if( stream->key != VALID_OPEN_FILE ) return EOF;
     441
     442    // get file descriptor from stream pointer
     443    int fd = stream->fd;
     444
     445    char byte = (char)c;
     446
     447    if( write( fd , &byte , 1 ) != 1 ) return 0;
     448    else                               return c;
     449
     450}
    400451
    401452//////////////////////////////////////////
Note: See TracChangeset for help on using the changeset viewer.