[439] | 1 | /* |
---|
[445] | 2 | * stdio.h - User level <stdio> library definition. |
---|
[439] | 3 | * |
---|
[637] | 4 | * Author Alain Greiner (2016,2017,2018,2019) |
---|
[439] | 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #ifndef _STDIO_H_ |
---|
| 25 | #define _STDIO_H_ |
---|
| 26 | |
---|
[445] | 27 | /********************************************************************************************* |
---|
| 28 | * This file defines the user level, TXT related <stdio> library. |
---|
| 29 | * These functions call the read() and write() functions defined in the <unistd> library |
---|
| 30 | * to access the TXT terminal. |
---|
| 31 | ********************************************************************************************/ |
---|
[439] | 32 | |
---|
[459] | 33 | #define MAX_OPEN_FILE_PER_PROCESS 256 |
---|
| 34 | #define VALID_OPEN_FILE 0x12345678 |
---|
| 35 | #define EOF -1 |
---|
| 36 | #define NULL (void *)0 |
---|
| 37 | |
---|
[473] | 38 | /********************************************************************************************* |
---|
| 39 | * This defines the user level FILE structure. |
---|
| 40 | ********************************************************************************************/ |
---|
| 41 | |
---|
[623] | 42 | typedef struct stream_s |
---|
[459] | 43 | { |
---|
[623] | 44 | int fd; // index in both kernel fd_array[], and user open_file_array[] |
---|
| 45 | int key; // entry valid in open_file_array[] when (key == VALID_OPEN_FILE) |
---|
[459] | 46 | } |
---|
| 47 | FILE; |
---|
| 48 | |
---|
[444] | 49 | /********************************************************************************************* |
---|
[610] | 50 | * This function causes the file/directory named <old> to be renamed as <new>. |
---|
| 51 | * If <new> exists, it is previously removed. |
---|
| 52 | ********************************************************************************************* |
---|
| 53 | * @ returns 0 if success / returns -1 if failure. |
---|
| 54 | ********************************************************************************************/ |
---|
| 55 | int rename( const char * old, |
---|
| 56 | const char * new ); |
---|
| 57 | |
---|
| 58 | /********************************************************************************************* |
---|
[444] | 59 | * This function writes a formated string to the standard "stdout" stream. |
---|
| 60 | ********************************************************************************************* |
---|
| 61 | * @ returns number of characters written if success / returns -1 if failure. |
---|
| 62 | ********************************************************************************************/ |
---|
| 63 | int printf( const char * format, ... ); |
---|
[439] | 64 | |
---|
[444] | 65 | /********************************************************************************************* |
---|
| 66 | * This function writes one single character to the standard "stdout" stream. |
---|
| 67 | ********************************************************************************************* |
---|
| 68 | * @ returns written character code if success / returns 0 (EOF) if failure. |
---|
| 69 | ********************************************************************************************/ |
---|
| 70 | int putchar( int c ); |
---|
[439] | 71 | |
---|
[444] | 72 | /********************************************************************************************* |
---|
| 73 | * This function returns one single character from the standard "stdin" stream. |
---|
| 74 | ********************************************************************************************* |
---|
| 75 | * @ returns read character code if success / returns 0 (EOF) if failure. |
---|
| 76 | ********************************************************************************************/ |
---|
[476] | 77 | int getchar( void ); |
---|
[439] | 78 | |
---|
[444] | 79 | /********************************************************************************************* |
---|
| 80 | * This function copies a formated string to a fixed size buffer. |
---|
| 81 | * it includes the NUL terminating character. |
---|
| 82 | * it cheks that the formated string fit in the buffer length. |
---|
| 83 | ********************************************************************************************* |
---|
[445] | 84 | * @ string : pointer on target buffer. |
---|
[444] | 85 | * @ length : max bumber of characters in target buffer. |
---|
| 86 | * @ format : formated string. |
---|
| 87 | * @ returns number of characters written if success / returns -1 if failure. |
---|
| 88 | ********************************************************************************************/ |
---|
| 89 | int snprintf( char * string, |
---|
| 90 | unsigned int length, |
---|
| 91 | const char * format, ... ); |
---|
[439] | 92 | |
---|
[459] | 93 | /********************************************************************************************* |
---|
| 94 | * This function opens the file identified by the <pathname> argument and associates |
---|
| 95 | * the stream pointed by <FILE> with it. |
---|
| 96 | * The <mode> argument is a string that can have the following values: |
---|
| 97 | * - "r" Open text file for reading. |
---|
| 98 | * The stream is positioned at the beginning of the file. |
---|
| 99 | * - "r+" Open for reading and writing. |
---|
| 100 | * The stream is positioned at the beginning of the file. |
---|
| 101 | * - "w" Truncate the file to zero length or create text file for writing. |
---|
| 102 | * The stream is positioned at the beginning of the file. |
---|
| 103 | * - "w+" Open for reading and writing. |
---|
| 104 | * The file is created if it does not exist, otherwise it is truncated. |
---|
| 105 | * The stream is positioned at the beginning of the file. |
---|
| 106 | * - "a" Open for writing. The file is created if it does not exist. |
---|
| 107 | * The stream is positioned at the end of the file. |
---|
| 108 | * Subsequent writes to the file will always end up at the current end of file, |
---|
| 109 | * irrespective of any intervening fseek() or similar. |
---|
| 110 | * - "a+" Open for reading and writing. |
---|
| 111 | * The file is created if it does not exist. |
---|
| 112 | * The stream is positioned at the end of the file. |
---|
| 113 | * Subsequent writes to the file will always end up at the current end of file, |
---|
| 114 | * irrespective of any intervening fseek() or similar. |
---|
| 115 | ********************************************************************************************* |
---|
| 116 | * @ pathname : file pathname. |
---|
| 117 | * @ mode : must be NULL <=> only "w+" mode is supported. |
---|
| 118 | * @ returns a stream pointer if success / returns NULL if file not found. |
---|
| 119 | ********************************************************************************************/ |
---|
| 120 | FILE * fopen( const char * pathname, |
---|
| 121 | const char * mode ); |
---|
| 122 | |
---|
| 123 | /********************************************************************************************* |
---|
| 124 | * This function dissociates the stream from its underlying file and close this file. |
---|
| 125 | * If the stream was being used for output, any buffered data is written first. |
---|
| 126 | ********************************************************************************************* |
---|
| 127 | * @ stream : pointer on a stream. |
---|
| 128 | * @ returns 0 if success / returns EOF if failure. |
---|
| 129 | ********************************************************************************************/ |
---|
| 130 | int fclose( FILE * stream ); |
---|
| 131 | |
---|
| 132 | /********************************************************************************************* |
---|
| 133 | * This function copies a formated string to an output stream identified by the <stream> |
---|
| 134 | * argument. It can be a regular file or a character oriented output device. |
---|
| 135 | ********************************************************************************************* |
---|
| 136 | * @ stream : pointer on a stream. |
---|
| 137 | * @ format : formated string. |
---|
| 138 | * @ returns number of characters written if success / returns -1 if failure. |
---|
| 139 | ********************************************************************************************/ |
---|
| 140 | int fprintf( FILE * stream, |
---|
| 141 | const char * format, ... ); |
---|
| 142 | |
---|
[643] | 143 | /********************************************************************************************* |
---|
| 144 | * This function moves <nitems> oblects, each <size> bytes long, from an input stream |
---|
| 145 | * identified by <stream>, to the user buffer identified by the <buffer> argument. |
---|
| 146 | * It can be a regular file or a character oriented input device. |
---|
| 147 | ********************************************************************************************* |
---|
[647] | 148 | * @ buffer : pointer on user buffer. |
---|
| 149 | * @ size : item size in bytes. |
---|
| 150 | * @ nitems : number of items. |
---|
[643] | 151 | * @ stream : pointer on a stream. |
---|
| 152 | * @ returns actual number of items read if success / returns 0 if failure. |
---|
| 153 | ********************************************************************************************/ |
---|
| 154 | unsigned int fread( void * buffer, |
---|
| 155 | unsigned int size, |
---|
| 156 | unsigned int nitems, |
---|
| 157 | FILE * stream ); |
---|
[459] | 158 | |
---|
[643] | 159 | /********************************************************************************************* |
---|
| 160 | * This function moves <nitems> oblects, each <size> bytes long, from an user buffer |
---|
| 161 | * identified by the <buffer> argument, to an output stream identified by <stream>. |
---|
| 162 | * It can be a regular file or a character oriented input device. |
---|
| 163 | ********************************************************************************************* |
---|
[647] | 164 | * @ buffer : pointer on user buffer. |
---|
| 165 | * @ size : item size in bytes. |
---|
| 166 | * @ nitems : number of items. |
---|
[643] | 167 | * @ stream : pointer on a stream. |
---|
| 168 | * @ returns actual number of written items if success / returns 0 if failure. |
---|
| 169 | ********************************************************************************************/ |
---|
| 170 | unsigned int fwrite( void * buffer, |
---|
| 171 | unsigned int size, |
---|
| 172 | unsigned int nitems, |
---|
| 173 | FILE * stream ); |
---|
| 174 | |
---|
[647] | 175 | /********************************************************************************************* |
---|
| 176 | * This function moves <nitems> oblects, each <size> bytes long, from an input stream |
---|
| 177 | * identified by <stream>, to the user buffer identified by the <buffer> argument. |
---|
| 178 | * It can be a regular file or a character oriented input device. |
---|
| 179 | ********************************************************************************************* |
---|
| 180 | * @ stream : pointer on a stream. |
---|
| 181 | * @ offset : used to compute new offset value. |
---|
| 182 | * @ whence : operation type (SEEK_SET / SEEK_CUR / SEEK_END) |
---|
| 183 | * @ return 0 if success / returns -1 if failure. |
---|
| 184 | ********************************************************************************************/ |
---|
| 185 | unsigned int fseek( FILE * stream, |
---|
| 186 | unsigned int offset, |
---|
| 187 | int whence ); |
---|
| 188 | |
---|
| 189 | |
---|
[439] | 190 | #endif // _STDIO_H_ |
---|