Changeset 80bee81 in mainline


Ignore:
Timestamp:
2015-09-21T22:40:52Z (9 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a955fcc
Parents:
7db5cfd
Message:

freopen() goes home too.

Location:
uspace/lib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/io/io.c

    r7db5cfd r80bee81  
    299299}
    300300
    301 int fclose(FILE *stream)
     301
     302static int _fclose_nofree(FILE *stream)
    302303{
    303304        int rc = 0;
     
    312313       
    313314        list_remove(&stream->link);
     315       
     316        if (rc != 0) {
     317                /* errno was set by close() */
     318                return EOF;
     319        }
     320       
     321        return 0;
     322}
     323
     324int fclose(FILE *stream)
     325{
     326        int rc = _fclose_nofree(stream);
    314327       
    315328        if ((stream != &stdin_null)
     
    318331                free(stream);
    319332       
    320         stream = NULL;
    321        
    322         if (rc != 0) {
    323                 /* errno was set by close() */
    324                 return EOF;
    325         }
    326        
    327         return 0;
     333        return rc;
     334}
     335
     336FILE *freopen(const char *path, const char *mode, FILE *stream)
     337{
     338        FILE *nstr;
     339       
     340        if (path == NULL) {
     341                /* Changing mode is not supported */
     342                return NULL;
     343        }
     344       
     345        (void) _fclose_nofree(stream);
     346        nstr = fopen(path, mode);
     347        if (nstr == NULL) {
     348                free(stream);
     349                return NULL;
     350        }
     351       
     352        list_remove(&nstr->link);
     353        *stream = *nstr;
     354        list_append(&stream->link, &files);
     355       
     356        free(nstr);
     357       
     358        return stream;
    328359}
    329360
  • uspace/lib/c/include/stdio.h

    r7db5cfd r80bee81  
    134134extern FILE *fopen(const char *, const char *);
    135135extern FILE *fdopen(int, const char *);
     136extern FILE *freopen(const char *, const char *, FILE *);
    136137extern int fclose(FILE *);
    137138
  • uspace/lib/posix/source/stdio.c

    r7db5cfd r80bee81  
    5555#include "libc/sys/stat.h"
    5656
    57 
    58 /* not the best of solutions, but freopen and ungetc will eventually
    59  * need to be implemented in libc anyway
    60  */
    61 #include "../../c/generic/private/stdio.h"
    62 
    6357/** Clears the stream's error and end-of-file indicators.
    6458 *
     
    6761void posix_clearerr(FILE *stream)
    6862{
    69         stream->error = 0;
    70         stream->eof = 0;
     63        clearerr(stream);
    7164}
    7265
     
    221214    const char *restrict mode, FILE *restrict stream)
    222215{
    223         assert(mode != NULL);
    224         assert(stream != NULL);
    225        
    226         if (filename == NULL) {
    227                 /* POSIX allows this to be imlementation-defined. HelenOS currently
    228                  * does not support changing the mode. */
    229                 // FIXME: handle mode change once it is supported
    230                 return stream;
    231         }
    232        
    233         /* Open a new stream. */
    234         FILE* new = fopen(filename, mode);
    235         if (new == NULL) {
    236                 fclose(stream);
    237                 /* errno was set by fopen() */
    238                 return NULL;
    239         }
    240        
    241         /* Close the original stream without freeing it (ignoring errors). */
    242         if (stream->buf != NULL) {
    243                 fflush(stream);
    244         }
    245         if (stream->sess != NULL) {
    246                 async_hangup(stream->sess);
    247         }
    248         if (stream->fd >= 0) {
    249                 close(stream->fd);
    250         }
    251         list_remove(&stream->link);
    252        
    253         /* Move the new stream to the original location. */
    254         memcpy(stream, new, sizeof (FILE));
    255         free(new);
    256        
    257         /* Update references in the file list. */
    258         stream->link.next->prev = &stream->link;
    259         stream->link.prev->next = &stream->link;
    260        
    261         return stream;
     216        return freopen(filename, mode, stream);
    262217}
    263218
Note: See TracChangeset for help on using the changeset viewer.