Changeset 677 for trunk


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

Introduce the fgetc() and fputc() functions.

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

Legend:

Unmodified
Added
Removed
  • trunk/libs/mini-libc/Makefile

    r661 r677  
    66
    77ifeq ($(ARCH_NAME),)
    8 $(error Please define in ARCH_NAME parameter in params-soft.mk!)
     8$(error Please define ARCH_NAME parameter in params-soft.mk!)
    99endif
    1010
  • 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//////////////////////////////////////////
  • trunk/libs/mini-libc/stdio.h

    r647 r677  
    2727/*********************************************************************************************
    2828 * This file defines the user level, TXT related <stdio> library.
     29 *
    2930 * These functions call the read() and write() functions defined in the <unistd> library
    3031 * to access the TXT terminal.
     
    3839/*********************************************************************************************
    3940 * This defines the user level FILE structure.
     41 * The open_file_array[] of files is a global variable allocated in the <stdio.c> file
    4042 ********************************************************************************************/
    4143
     
    6466
    6567/*********************************************************************************************
    66  * This function writes one single character to the standard "stdout" stream.
     68 * This function writes the <c> character to the standard "stdout" stream.
    6769 *********************************************************************************************
    6870 * @ returns written character code if success / returns 0 (EOF) if failure.
     
    8587 * @ length    : max bumber of characters in target buffer.
    8688 * @ format    : formated string.
    87  * @ returns number of characters written if success / returns -1 if failure.
     89 * @ returns string length (not including NUL) if success / returns -1 if failure.
    8890 ********************************************************************************************/
    8991int snprintf( char         * string,
    9092              unsigned int   length,
    9193              const char   * format, ... );
     94
     95
     96
     97
     98
    9299
    93100/*********************************************************************************************
     
    131138
    132139/*********************************************************************************************
     140 * This function returns one single character from the FILE identified by <stream>.
     141 *********************************************************************************************
     142 * @ returns read character code if success / returns 0 (EOF) if failure.
     143 ********************************************************************************************/
     144int fgetc( FILE * stream );
     145
     146/*********************************************************************************************
     147 * This function writes the <c> character to the FILE identified by <stream>.
     148 *********************************************************************************************
     149 * @ returns written character code if success / returns 0 (EOF) if failure.
     150 ********************************************************************************************/
     151int fputc( int c,
     152           FILE * stream);
     153
     154/*********************************************************************************************
    133155 * This function copies a formated string to an output stream identified by the <stream>
    134156 * argument. It can be a  regular file or a character oriented output device.
  • trunk/libs/mini-libc/string.h

    r619 r677  
    22 * string.h - User level <string> library definition.
    33 *
    4  * Author     Alain Greiner (2016,2017,2018)
     4 * Author     Alain Greiner (2016,2017,2018,2019?2020)
    55 *
    66 * Copyright (c) UPMC Sorbonne Universites
  • trunk/libs/mini-libc/strings.h

    r449 r677  
    22 * strings.h - User level <strings> library definition.
    33 *
    4  * Author     Alain Greiner (2016,2017,2018)
     4 * Author     Alain Greiner (2016,2017,2018,2019,2020)
    55 *
    66 * Copyright (c) UPMC Sorbonne Universites
  • trunk/libs/mini-libc/unistd.h

    r650 r677  
    3535
    3636/*****************************************************************************************
    37  * This function implements the "alarm" system call.
    38  * sets a timer to deliver the signal SIGALRM to the calling process,
     37 * This function implements the <alarm> system call.
     38 * It sets a timer to deliver the signal SIGALRM to the calling process,
    3939 * after the specified number of seconds.
    4040 * If an alarm has already been set with alarm() but has not been delivered,
     
    4949
    5050/*****************************************************************************************
    51  * This function implements the "chdir" system call.
     51 * This function implements the <chdir> system call.
    5252 * It changes the current working directory in the reference process descriptor.
    5353 *****************************************************************************************
     
    5858
    5959/*****************************************************************************************
    60  * This function implements the "close" system call.
     60 * This function implements the <close> system call.
    6161 * It releases the memory allocated for the file identified by the <fd> argument,
    6262 * and remove the fd array_entry in all process descriptor copies.
     
    6868
    6969/*****************************************************************************************
    70  * This function implement the "exec" system call.
     70 * This function implement the <exec> system call.
    7171 * It creates, in the same cluster as the calling thread, a new process descriptor,
    7272 * and a new associated main thread descriptor, executing a new memory image defined
     
    7878 * as defined by the <arv> argument, and the new process environment variables,
    7979 * as defined by the <envp>  argument.
    80  * TODO : the <argv> and <envp> arguments are not supported yet (both must be NULL).
     80 * TODO : the <envs> argument is not supported yet (must be NULL).
    8181 *****************************************************************************************
    8282 * @ filename : string pointer on .elf filename (virtual pointer in user space)
    83  * @ argv     : array of strings on process arguments (virtual pointers in user space)
    84  * @ envp     : array of strings on environment variables (virtual pointers in user space)
     83 * @ args     : array of pointers on process arguments (strings in user space)
     84 * @ envs     : array of pointers on environment variables (strings in user space)
    8585 * @ does not return if success / returns -1 if failure.
    8686 ****************************************************************************************/
    8787int execve( char  * filename,
    88             char ** argv,
    89             char ** envp );
    90 
    91 /*****************************************************************************************
    92  * This function implement the "fork" system call.
     88            char ** args,
     89            char ** envs );
     90
     91/*****************************************************************************************
     92 * This function implement the <fork> system call.
    9393 * The calling process descriptor (parent process), and the associated thread descriptor
    9494 * are replicated in a - likely - remote cluster, that becomes the new process owner.
     
    106106
    107107/*****************************************************************************************
    108  * This function implements the "fsync" system call.
     108 * This function implements the <fsync> system call.
    109109 * It causes all the modified data and attributes of file identified by the <fd> argument
    110110 * to be copied from the file mapper and file descriptor to the IOC device.
     
    116116
    117117/*****************************************************************************************
    118  * This function implements the "getcwd" system call.
     118 * This function implements the <getcwd> system call.
    119119 * It returns the pathname of the current working directory.
    120120 *****************************************************************************************
     
    127127
    128128/*****************************************************************************************
    129  * This function implements the "getpid" system call.
     129 * This function implements the <getpid> system call.
    130130 * It returns the process identifier.
    131131 *****************************************************************************************
     
    135135
    136136/*****************************************************************************************
    137  * This function implements the "isatty" system call.
     137 * This function implements the <isatty> system call.
    138138 * It test whether a file descriptor refers to a terminal.
    139139 *****************************************************************************************
     
    144144
    145145/*****************************************************************************************
    146  * This function implements the "lseek" system call.
     146 * This function implements the <lseek> system call.
    147147 * It repositions the offset of the file descriptor identified by <fd>,
    148148 * according to the operation type defined by the <whence> argument.
     
    158158
    159159/*****************************************************************************************
    160  * This function implements the "pause" system call.
     160 * This function implements the <pause> system call.
    161161 * It stops the calling process until a signal is received.
    162162 *****************************************************************************************
     
    166166
    167167/*****************************************************************************************
    168  * This function implements the "pipe" system call.
     168 * This function implements the <pipe> system call.
    169169 * It creates in the calling thread cluster an unnamed pipe, and two (read and write)
    170170 * file descriptors to access this pipe. The argument is a pointer a fd[] array.
    171  * TODO not implemented yet...
    172  *****************************************************************************************
    173  * @ fd[0] : [out] read only file descriptor index.
    174  * @ fd[1] : [out] write only file descriptor index.
     171 *****************************************************************************************
     172 * @ fd[0] : [out] buffer for read only file descriptor index.
     173 * @ fd[1] : [out] buffer for write only file descriptor index.
    175174 * @ return 0 if success / return -1 if failure.
    176175 ****************************************************************************************/
     
    178177
    179178/*****************************************************************************************
    180  * This function implements the "read" system call.
     179 * This function implements the <read> system call.
    181180 * It reads bytes from an open file identified by the <fd> file descriptor.
    182181 * This file can be a regular file or a character oriented device.
    183182 *****************************************************************************************
    184  * @ fd       : open file index in fd_array.
     183 * @ fd       : file index in fd_array.
    185184 * @ buf      : buffer virtual address in user space.
    186185 * @ count    : number of bytes.
     
    192191
    193192/*****************************************************************************************
    194  * This function implements the "rmdir" system call.
     193 * This function implements the <rmdir> system call.
    195194 * It removes a directory file whose name is given by <pathname>.
    196195 * The directory must not contain any entries other than `.' and `..'.
     
    202201
    203202/*****************************************************************************************
    204  * This function implements the "sync" system call.
     203 * This function implements the <sync> system call.
    205204 * It forces all kernel mappers (file caches) to be copied to the IOC device.
    206205 ****************************************************************************************/
     
    208207
    209208/*****************************************************************************************
    210  * This function implements the "unlink" system call.
     209 * This function implements the <unlink> system call.
    211210 * It removes a directory entry identified by the <pathname> from the parent directory,
    212211 * and decrement the link count of the file referenced by the link.
     
    222221
    223222/*****************************************************************************************
    224  * This function implements the "write" system call.
     223 * This function implements the <write> system call.
    225224 * It writes bytes to an open file identified by the <fd> file descriptor.
    226225 * This file can be a regular file or character oriented device.
    227226 *****************************************************************************************
    228  * @ fd       : open file index in fd_array.
     227 * @ fd       : file index in fd_array.
    229228 * @ buf      : buffer virtual address in user space.
    230229 * @ count    : number of bytes.
Note: See TracChangeset for help on using the changeset viewer.