[09b0b1fb] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
[4f4b4e7] | 3 | * Copyright (c) 2011 Petr Koupy
|
---|
[09b0b1fb] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup libposix
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #define POSIX_INTERNAL
|
---|
| 37 |
|
---|
[ab547063] | 38 | #include <assert.h>
|
---|
[4f4b4e7] | 39 | #include <errno.h>
|
---|
| 40 |
|
---|
[09b0b1fb] | 41 | #include "common.h"
|
---|
| 42 | #include "stdio.h"
|
---|
[ab547063] | 43 | #include "string.h"
|
---|
[4f4b4e7] | 44 |
|
---|
[ab547063] | 45 | /* not the best of solutions, but freopen will eventually
|
---|
[4f4b4e7] | 46 | * need to be implemented in libc anyway
|
---|
| 47 | */
|
---|
[ab547063] | 48 | #include "../c/generic/private/stdio.h"
|
---|
[09b0b1fb] | 49 |
|
---|
[4f4b4e7] | 50 | /**
|
---|
| 51 | *
|
---|
| 52 | * @param filename
|
---|
| 53 | * @param mode
|
---|
| 54 | * @param stream
|
---|
| 55 | * @return
|
---|
| 56 | */
|
---|
| 57 | FILE *posix_freopen(
|
---|
| 58 | const char *restrict filename,
|
---|
| 59 | const char *restrict mode,
|
---|
| 60 | FILE *restrict stream)
|
---|
[09b0b1fb] | 61 | {
|
---|
[ab547063] | 62 | assert(mode != NULL);
|
---|
| 63 | assert(stream != NULL);
|
---|
| 64 |
|
---|
| 65 | if (filename == NULL) {
|
---|
| 66 | // TODO
|
---|
| 67 |
|
---|
| 68 | /* print error to stderr as well, to avoid hard to find problems
|
---|
[4f4b4e7] | 69 | * with buggy apps that expect this to work
|
---|
| 70 | */
|
---|
| 71 | fprintf(stderr,
|
---|
| 72 | "ERROR: Application wants to use freopen() to change mode of opened stream.\n"
|
---|
| 73 | " libposix does not support that yet, the application may function improperly.\n");
|
---|
[ab547063] | 74 | errno = ENOTSUP;
|
---|
| 75 | return NULL;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | FILE* copy = malloc(sizeof(FILE));
|
---|
| 79 | if (copy == NULL) {
|
---|
| 80 | errno = ENOMEM;
|
---|
| 81 | return NULL;
|
---|
| 82 | }
|
---|
| 83 | memcpy(copy, stream, sizeof(FILE));
|
---|
[4f4b4e7] | 84 | fclose(copy); /* copy is now freed */
|
---|
[ab547063] | 85 |
|
---|
[4f4b4e7] | 86 | copy = fopen(filename, mode); /* open new stream */
|
---|
[ab547063] | 87 | if (copy == NULL) {
|
---|
| 88 | /* fopen() sets errno */
|
---|
| 89 | return NULL;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /* move the new stream to the original location */
|
---|
| 93 | memcpy(stream, copy, sizeof (FILE));
|
---|
| 94 | free(copy);
|
---|
| 95 |
|
---|
| 96 | /* update references in the file list */
|
---|
| 97 | stream->link.next->prev = &stream->link;
|
---|
| 98 | stream->link.prev->next = &stream->link;
|
---|
| 99 |
|
---|
| 100 | return stream;
|
---|
[09b0b1fb] | 101 | }
|
---|
| 102 |
|
---|
[4f4b4e7] | 103 | /**
|
---|
| 104 | *
|
---|
| 105 | * @param s
|
---|
| 106 | */
|
---|
[09b0b1fb] | 107 | void posix_perror(const char *s)
|
---|
| 108 | {
|
---|
| 109 | // TODO
|
---|
| 110 | not_implemented();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[b08ef1fd] | 113 | /**
|
---|
| 114 | *
|
---|
| 115 | * @param stream
|
---|
| 116 | * @param offset
|
---|
| 117 | * @param whence
|
---|
| 118 | * @return
|
---|
| 119 | */
|
---|
| 120 | int posix_fseeko(FILE *stream, posix_off_t offset, int whence)
|
---|
| 121 | {
|
---|
| 122 | // TODO
|
---|
| 123 | not_implemented();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | *
|
---|
| 128 | * @param stream
|
---|
| 129 | * @return
|
---|
| 130 | */
|
---|
| 131 | posix_off_t posix_ftello(FILE *stream)
|
---|
| 132 | {
|
---|
| 133 | // TODO
|
---|
| 134 | not_implemented();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[09b0b1fb] | 137 | /** @}
|
---|
| 138 | */
|
---|