Changeset 459 for trunk/libs/mini-libc/stdio.c
- Timestamp:
- Aug 13, 2018, 1:43:20 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libs/mini-libc/stdio.c
r457 r459 26 26 #include <almosmkh.h> 27 27 #include <unistd.h> 28 #include <fcntl.h> 29 30 //////////////////////////////////////////////////////////////////////////////////////// 31 // stdio library global variables 32 //////////////////////////////////////////////////////////////////////////////////////// 33 34 FILE open_file_array[MAX_OPEN_FILE_PER_PROCESS]; // array of open files structures 35 36 //////////////////////////////////////////////////////////////////////////////////////// 37 // stdio library functions 38 //////////////////////////////////////////////////////////////////////////////////////// 28 39 29 40 ////////////////////////////////////////// … … 144 155 break; 145 156 } 146 /*147 157 case ('f'): // IEEE754 64 bits 148 158 // integer part : up to 10 decimal digits … … 234 244 break; 235 245 } 236 */237 246 default: // unsupported argument type 238 247 { … … 272 281 { 273 282 string[count] = 0; 283 274 284 return write( 1 , &string , count ); 275 285 } 276 } 286 } // end printf() 277 287 278 288 ///////////// … … 309 319 310 320 return count; 311 } 312 313 314 315 316 321 } // end snprintf() 322 323 //////////////////////////////////// 324 FILE * 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 /////////////////////////// 358 int 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 ///////////////////////////////// 373 int 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
Note: See TracChangeset
for help on using the changeset viewer.