Changeset ab547063 in mainline


Ignore:
Timestamp:
2011-06-10T10:54:07Z (13 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4f4b4e7, 9ec25a6
Parents:
41811af
Message:

Partially implement freopen()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/stdio.c

    r41811af rab547063  
    3535#define POSIX_INTERNAL
    3636
     37#include <assert.h>
     38#include "errno.h"
    3739#include "common.h"
    3840#include "stdio.h"
     41#include "string.h"
     42/* not the best of solutions, but freopen will eventually
     43   need to be implemented in libc anyway */
     44#include "../c/generic/private/stdio.h"
    3945
    4046FILE *posix_freopen(const char *restrict filename,
     
    4248                    FILE *restrict stream)
    4349{
    44         // TODO
    45         not_implemented();
     50        assert(mode != NULL);
     51        assert(stream != NULL);
     52
     53        if (filename == NULL) {
     54                // TODO
     55               
     56                /* print error to stderr as well, to avoid hard to find problems
     57                   with buggy apps that expect this to work */
     58                fprintf(stderr, "ERROR: Application wants to use freopen() to change mode of opened stream.\n"
     59                                "       libposix does not support that yet, the application may function improperly.\n");
     60                errno = ENOTSUP;
     61                return NULL;
     62        }
     63
     64        FILE* copy = malloc(sizeof(FILE));
     65        if (copy == NULL) {
     66                errno = ENOMEM;
     67                return NULL;
     68        }
     69        memcpy(copy, stream, sizeof(FILE));
     70        fclose(copy);   /* copy is now freed */
     71       
     72        copy = fopen(filename, mode);         /* open new stream */
     73        if (copy == NULL) {
     74                /* fopen() sets errno */
     75                return NULL;
     76        }
     77       
     78        /* move the new stream to the original location */
     79        memcpy(stream, copy, sizeof (FILE));
     80        free(copy);
     81       
     82        /* update references in the file list */
     83        stream->link.next->prev = &stream->link;
     84        stream->link.prev->next = &stream->link;
     85       
     86        return stream;
    4687}
    4788
Note: See TracChangeset for help on using the changeset viewer.