| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jiri Zarevucky
|
|---|
| 3 | * Copyright (c) 2011 Petr Koupy
|
|---|
| 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 LIBPOSIX_INTERNAL
|
|---|
| 37 |
|
|---|
| 38 | #include <assert.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 |
|
|---|
| 41 | #include "internal/common.h"
|
|---|
| 42 | #include "stdio.h"
|
|---|
| 43 | #include "string.h"
|
|---|
| 44 |
|
|---|
| 45 | /* not the best of solutions, but freopen will eventually
|
|---|
| 46 | * need to be implemented in libc anyway
|
|---|
| 47 | */
|
|---|
| 48 | #include "../c/generic/private/stdio.h"
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | *
|
|---|
| 52 | * @param c
|
|---|
| 53 | * @param stream
|
|---|
| 54 | * @return
|
|---|
| 55 | */
|
|---|
| 56 | int posix_ungetc(int c, FILE *stream)
|
|---|
| 57 | {
|
|---|
| 58 | // TODO
|
|---|
| 59 | not_implemented();
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | *
|
|---|
| 64 | * @param filename
|
|---|
| 65 | * @param mode
|
|---|
| 66 | * @param stream
|
|---|
| 67 | * @return
|
|---|
| 68 | */
|
|---|
| 69 | FILE *posix_freopen(
|
|---|
| 70 | const char *restrict filename,
|
|---|
| 71 | const char *restrict mode,
|
|---|
| 72 | FILE *restrict stream)
|
|---|
| 73 | {
|
|---|
| 74 | assert(mode != NULL);
|
|---|
| 75 | assert(stream != NULL);
|
|---|
| 76 |
|
|---|
| 77 | if (filename == NULL) {
|
|---|
| 78 | // TODO
|
|---|
| 79 |
|
|---|
| 80 | /* print error to stderr as well, to avoid hard to find problems
|
|---|
| 81 | * with buggy apps that expect this to work
|
|---|
| 82 | */
|
|---|
| 83 | fprintf(stderr,
|
|---|
| 84 | "ERROR: Application wants to use freopen() to change mode of opened stream.\n"
|
|---|
| 85 | " libposix does not support that yet, the application may function improperly.\n");
|
|---|
| 86 | errno = ENOTSUP;
|
|---|
| 87 | return NULL;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | FILE* copy = malloc(sizeof(FILE));
|
|---|
| 91 | if (copy == NULL) {
|
|---|
| 92 | errno = ENOMEM;
|
|---|
| 93 | return NULL;
|
|---|
| 94 | }
|
|---|
| 95 | memcpy(copy, stream, sizeof(FILE));
|
|---|
| 96 | fclose(copy); /* copy is now freed */
|
|---|
| 97 |
|
|---|
| 98 | copy = fopen(filename, mode); /* open new stream */
|
|---|
| 99 | if (copy == NULL) {
|
|---|
| 100 | /* fopen() sets errno */
|
|---|
| 101 | return NULL;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /* move the new stream to the original location */
|
|---|
| 105 | memcpy(stream, copy, sizeof (FILE));
|
|---|
| 106 | free(copy);
|
|---|
| 107 |
|
|---|
| 108 | /* update references in the file list */
|
|---|
| 109 | stream->link.next->prev = &stream->link;
|
|---|
| 110 | stream->link.prev->next = &stream->link;
|
|---|
| 111 |
|
|---|
| 112 | return stream;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | *
|
|---|
| 117 | * @param s
|
|---|
| 118 | */
|
|---|
| 119 | void posix_perror(const char *s)
|
|---|
| 120 | {
|
|---|
| 121 | // TODO
|
|---|
| 122 | not_implemented();
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | *
|
|---|
| 127 | * @param stream
|
|---|
| 128 | * @param offset
|
|---|
| 129 | * @param whence
|
|---|
| 130 | * @return
|
|---|
| 131 | */
|
|---|
| 132 | int posix_fseeko(FILE *stream, posix_off_t offset, int whence)
|
|---|
| 133 | {
|
|---|
| 134 | // TODO
|
|---|
| 135 | not_implemented();
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /**
|
|---|
| 139 | *
|
|---|
| 140 | * @param stream
|
|---|
| 141 | * @return
|
|---|
| 142 | */
|
|---|
| 143 | posix_off_t posix_ftello(FILE *stream)
|
|---|
| 144 | {
|
|---|
| 145 | // TODO
|
|---|
| 146 | not_implemented();
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | *
|
|---|
| 151 | * @param s
|
|---|
| 152 | * @param format
|
|---|
| 153 | * @param ...
|
|---|
| 154 | * @return
|
|---|
| 155 | */
|
|---|
| 156 | int posix_sprintf(char *s, const char *format, ...)
|
|---|
| 157 | {
|
|---|
| 158 | // TODO
|
|---|
| 159 | not_implemented();
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /**
|
|---|
| 163 | *
|
|---|
| 164 | * @param s
|
|---|
| 165 | * @param format
|
|---|
| 166 | * @param ...
|
|---|
| 167 | * @return
|
|---|
| 168 | */
|
|---|
| 169 | int posix_sscanf(const char *s, const char *format, ...)
|
|---|
| 170 | {
|
|---|
| 171 | // TODO
|
|---|
| 172 | not_implemented();
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /** @}
|
|---|
| 176 | */
|
|---|