[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 |
|
---|
[491e1ee] | 36 | #define LIBPOSIX_INTERNAL
|
---|
[09b0b1fb] | 37 |
|
---|
[ab547063] | 38 | #include <assert.h>
|
---|
[4f4b4e7] | 39 | #include <errno.h>
|
---|
| 40 |
|
---|
[9b1503e] | 41 | #include "internal/common.h"
|
---|
[09b0b1fb] | 42 | #include "stdio.h"
|
---|
[8b5fb5e] | 43 | #include "libc/io/printf_core.h"
|
---|
[ab547063] | 44 | #include "string.h"
|
---|
[8b5fb5e] | 45 | #include "libc/str.h"
|
---|
[4f4b4e7] | 46 |
|
---|
[ab547063] | 47 | /* not the best of solutions, but freopen will eventually
|
---|
[4f4b4e7] | 48 | * need to be implemented in libc anyway
|
---|
| 49 | */
|
---|
[ab547063] | 50 | #include "../c/generic/private/stdio.h"
|
---|
[09b0b1fb] | 51 |
|
---|
[8b5fb5e] | 52 | /** Clears the stream's error and end-of-file indicators.
|
---|
| 53 | *
|
---|
| 54 | * @param stream
|
---|
| 55 | */
|
---|
| 56 | void posix_clearerr(FILE *stream)
|
---|
| 57 | {
|
---|
| 58 | stream->error = 0;
|
---|
| 59 | stream->eof = 0;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | *
|
---|
| 64 | * @param s
|
---|
| 65 | * @return
|
---|
| 66 | */
|
---|
| 67 | char *posix_ctermid(char *s)
|
---|
| 68 | {
|
---|
| 69 | /* Currently always returns an error value (empty string). */
|
---|
| 70 | // TODO: return a real terminal path
|
---|
| 71 |
|
---|
| 72 | static char dummy_path[L_ctermid] = {'\0'};
|
---|
| 73 |
|
---|
| 74 | if (s == NULL) {
|
---|
| 75 | return dummy_path;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | s[0] = '\0';
|
---|
| 79 | return s;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[59f799b] | 82 | /**
|
---|
| 83 | *
|
---|
| 84 | * @param c
|
---|
| 85 | * @param stream
|
---|
| 86 | * @return
|
---|
| 87 | */
|
---|
| 88 | int posix_ungetc(int c, FILE *stream)
|
---|
| 89 | {
|
---|
| 90 | // TODO
|
---|
| 91 | not_implemented();
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[8b5fb5e] | 94 | ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
|
---|
| 95 | int delimiter, FILE *restrict stream)
|
---|
| 96 | {
|
---|
| 97 | // TODO
|
---|
| 98 | not_implemented();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
|
---|
| 102 | FILE *restrict stream)
|
---|
| 103 | {
|
---|
| 104 | // TODO
|
---|
| 105 | not_implemented();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[4f4b4e7] | 108 | /**
|
---|
| 109 | *
|
---|
| 110 | * @param filename
|
---|
| 111 | * @param mode
|
---|
| 112 | * @param stream
|
---|
| 113 | * @return
|
---|
| 114 | */
|
---|
| 115 | FILE *posix_freopen(
|
---|
| 116 | const char *restrict filename,
|
---|
| 117 | const char *restrict mode,
|
---|
| 118 | FILE *restrict stream)
|
---|
[09b0b1fb] | 119 | {
|
---|
[ab547063] | 120 | assert(mode != NULL);
|
---|
| 121 | assert(stream != NULL);
|
---|
| 122 |
|
---|
| 123 | if (filename == NULL) {
|
---|
| 124 | // TODO
|
---|
| 125 |
|
---|
| 126 | /* print error to stderr as well, to avoid hard to find problems
|
---|
[4f4b4e7] | 127 | * with buggy apps that expect this to work
|
---|
| 128 | */
|
---|
| 129 | fprintf(stderr,
|
---|
| 130 | "ERROR: Application wants to use freopen() to change mode of opened stream.\n"
|
---|
| 131 | " libposix does not support that yet, the application may function improperly.\n");
|
---|
[ab547063] | 132 | errno = ENOTSUP;
|
---|
| 133 | return NULL;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | FILE* copy = malloc(sizeof(FILE));
|
---|
| 137 | if (copy == NULL) {
|
---|
| 138 | errno = ENOMEM;
|
---|
| 139 | return NULL;
|
---|
| 140 | }
|
---|
| 141 | memcpy(copy, stream, sizeof(FILE));
|
---|
[4f4b4e7] | 142 | fclose(copy); /* copy is now freed */
|
---|
[ab547063] | 143 |
|
---|
[4f4b4e7] | 144 | copy = fopen(filename, mode); /* open new stream */
|
---|
[ab547063] | 145 | if (copy == NULL) {
|
---|
| 146 | /* fopen() sets errno */
|
---|
| 147 | return NULL;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /* move the new stream to the original location */
|
---|
| 151 | memcpy(stream, copy, sizeof (FILE));
|
---|
| 152 | free(copy);
|
---|
| 153 |
|
---|
| 154 | /* update references in the file list */
|
---|
| 155 | stream->link.next->prev = &stream->link;
|
---|
| 156 | stream->link.prev->next = &stream->link;
|
---|
| 157 |
|
---|
| 158 | return stream;
|
---|
[09b0b1fb] | 159 | }
|
---|
| 160 |
|
---|
[8b5fb5e] | 161 | FILE *posix_fmemopen(void *restrict buf, size_t size,
|
---|
| 162 | const char *restrict mode)
|
---|
| 163 | {
|
---|
| 164 | // TODO
|
---|
| 165 | not_implemented();
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | FILE *posix_open_memstream(char **bufp, size_t *sizep)
|
---|
| 169 | {
|
---|
| 170 | // TODO
|
---|
| 171 | not_implemented();
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[4f4b4e7] | 174 | /**
|
---|
| 175 | *
|
---|
| 176 | * @param s
|
---|
| 177 | */
|
---|
[09b0b1fb] | 178 | void posix_perror(const char *s)
|
---|
| 179 | {
|
---|
[8b5fb5e] | 180 | if (s == NULL || s[0] == '\0') {
|
---|
| 181 | fprintf(stderr, "%s\n", posix_strerror(errno));
|
---|
| 182 | } else {
|
---|
| 183 | fprintf(stderr, "%s: %s\n", s, posix_strerror(errno));
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | struct _posix_fpos {
|
---|
| 188 | off64_t offset;
|
---|
| 189 | };
|
---|
| 190 |
|
---|
| 191 | /** Restores stream a to position previously saved with fgetpos().
|
---|
| 192 | *
|
---|
| 193 | * @param stream Stream to restore
|
---|
| 194 | * @param pos Position to restore
|
---|
| 195 | * @return Zero on success, non-zero (with errno set) on failure
|
---|
| 196 | */
|
---|
| 197 | int posix_fsetpos(FILE *stream, const posix_fpos_t *pos)
|
---|
| 198 | {
|
---|
| 199 | return fseek(stream, pos->offset, SEEK_SET);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | /** Saves the stream's position for later use by fsetpos().
|
---|
| 203 | *
|
---|
| 204 | * @param stream Stream to save
|
---|
| 205 | * @param pos Place to store the position
|
---|
| 206 | * @return Zero on success, non-zero (with errno set) on failure
|
---|
| 207 | */
|
---|
| 208 | int posix_fgetpos(FILE *restrict stream, posix_fpos_t *restrict pos)
|
---|
| 209 | {
|
---|
| 210 | off64_t ret = ftell(stream);
|
---|
| 211 | if (ret == -1) {
|
---|
| 212 | return errno;
|
---|
| 213 | }
|
---|
| 214 | pos->offset = ret;
|
---|
| 215 | return 0;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /**
|
---|
| 219 | *
|
---|
| 220 | * @param stream
|
---|
| 221 | * @param offset
|
---|
| 222 | * @param whence
|
---|
| 223 | * @return
|
---|
| 224 | */
|
---|
| 225 | int posix_fseek(FILE *stream, long offset, int whence)
|
---|
| 226 | {
|
---|
| 227 | return fseek(stream, (off64_t) offset, whence);
|
---|
[09b0b1fb] | 228 | }
|
---|
| 229 |
|
---|
[b08ef1fd] | 230 | /**
|
---|
| 231 | *
|
---|
| 232 | * @param stream
|
---|
| 233 | * @param offset
|
---|
| 234 | * @param whence
|
---|
| 235 | * @return
|
---|
| 236 | */
|
---|
| 237 | int posix_fseeko(FILE *stream, posix_off_t offset, int whence)
|
---|
| 238 | {
|
---|
[8b5fb5e] | 239 | return fseek(stream, (off64_t) offset, whence);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | /**
|
---|
| 243 | *
|
---|
| 244 | * @param stream
|
---|
| 245 | * @return
|
---|
| 246 | */
|
---|
| 247 | long posix_ftell(FILE *stream)
|
---|
| 248 | {
|
---|
| 249 | return (long) ftell(stream);
|
---|
[b08ef1fd] | 250 | }
|
---|
| 251 |
|
---|
| 252 | /**
|
---|
| 253 | *
|
---|
| 254 | * @param stream
|
---|
| 255 | * @return
|
---|
| 256 | */
|
---|
| 257 | posix_off_t posix_ftello(FILE *stream)
|
---|
| 258 | {
|
---|
[8b5fb5e] | 259 | return (posix_off_t) ftell(stream);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | int posix_dprintf(int fildes, const char *restrict format, ...)
|
---|
| 263 | {
|
---|
| 264 | va_list list;
|
---|
| 265 | va_start(list, format);
|
---|
| 266 | int result = posix_vdprintf(fildes, format, list);
|
---|
| 267 | va_end(list);
|
---|
| 268 | return result;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | static int _dprintf_str_write(const char *str, size_t size, void *fd)
|
---|
| 272 | {
|
---|
| 273 | ssize_t wr = write(*(int *) fd, str, size);
|
---|
| 274 | return str_nlength(str, wr);
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
|
---|
| 278 | {
|
---|
| 279 | size_t offset = 0;
|
---|
| 280 | size_t chars = 0;
|
---|
| 281 | size_t sz;
|
---|
| 282 | char buf[4];
|
---|
| 283 |
|
---|
| 284 | while (offset < size) {
|
---|
| 285 | sz = 0;
|
---|
| 286 | if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) {
|
---|
| 287 | break;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | if (write(*(int *) fd, buf, sz) != (ssize_t) sz) {
|
---|
| 291 | break;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | chars++;
|
---|
| 295 | offset += sizeof(wchar_t);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | return chars;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | int posix_vdprintf(int fildes, const char *restrict format, va_list ap)
|
---|
| 302 | {
|
---|
| 303 | printf_spec_t spec = {
|
---|
| 304 | .str_write = _dprintf_str_write,
|
---|
| 305 | .wstr_write = _dprintf_wstr_write,
|
---|
| 306 | .data = &fildes
|
---|
| 307 | };
|
---|
| 308 |
|
---|
| 309 | return printf_core(format, &spec, ap);
|
---|
[b08ef1fd] | 310 | }
|
---|
| 311 |
|
---|
[59f799b] | 312 | /**
|
---|
| 313 | *
|
---|
| 314 | * @param s
|
---|
| 315 | * @param format
|
---|
| 316 | * @param ...
|
---|
| 317 | * @return
|
---|
| 318 | */
|
---|
| 319 | int posix_sprintf(char *s, const char *format, ...)
|
---|
| 320 | {
|
---|
[8b5fb5e] | 321 | va_list list;
|
---|
| 322 | va_start(list, format);
|
---|
| 323 | int result = posix_vsprintf(s, format, list);
|
---|
| 324 | va_end(list);
|
---|
| 325 | return result;
|
---|
[59f799b] | 326 | }
|
---|
| 327 |
|
---|
[823a929] | 328 | /**
|
---|
| 329 | *
|
---|
| 330 | * @param s
|
---|
| 331 | * @param format
|
---|
[8b5fb5e] | 332 | * @param ap
|
---|
[823a929] | 333 | * @return
|
---|
| 334 | */
|
---|
| 335 | int posix_vsprintf(char *s, const char *format, va_list ap)
|
---|
| 336 | {
|
---|
[8b5fb5e] | 337 | return vsnprintf(s, STR_NO_LIMIT, format, ap);
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | /**
|
---|
| 341 | *
|
---|
| 342 | * @param stream
|
---|
| 343 | * @param format
|
---|
| 344 | * @param ...
|
---|
| 345 | * @return
|
---|
| 346 | */
|
---|
| 347 | int posix_fscanf(FILE *restrict stream, const char *restrict format, ...)
|
---|
| 348 | {
|
---|
| 349 | va_list list;
|
---|
| 350 | va_start(list, format);
|
---|
| 351 | int result = posix_vfscanf(stream, format, list);
|
---|
| 352 | va_end(list);
|
---|
| 353 | return result;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | int posix_vfscanf(FILE *restrict stream, const char *restrict format, va_list arg)
|
---|
| 357 | {
|
---|
| 358 | // TODO
|
---|
[823a929] | 359 | not_implemented();
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[8b5fb5e] | 362 | /**
|
---|
| 363 | *
|
---|
| 364 | * @param format
|
---|
| 365 | * @param ...
|
---|
| 366 | * @return
|
---|
| 367 | */
|
---|
| 368 | int posix_scanf(const char *restrict format, ...)
|
---|
| 369 | {
|
---|
| 370 | va_list list;
|
---|
| 371 | va_start(list, format);
|
---|
| 372 | int result = posix_vscanf(format, list);
|
---|
| 373 | va_end(list);
|
---|
| 374 | return result;
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | /**
|
---|
| 378 | *
|
---|
| 379 | * @param format
|
---|
| 380 | * @param arg
|
---|
| 381 | * @return
|
---|
| 382 | */
|
---|
| 383 | int posix_vscanf(const char *restrict format, va_list arg)
|
---|
| 384 | {
|
---|
| 385 | return posix_vfscanf(stdin, format, arg);
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[59f799b] | 388 | /**
|
---|
| 389 | *
|
---|
| 390 | * @param s
|
---|
| 391 | * @param format
|
---|
| 392 | * @param ...
|
---|
| 393 | * @return
|
---|
| 394 | */
|
---|
| 395 | int posix_sscanf(const char *s, const char *format, ...)
|
---|
[8b5fb5e] | 396 | {
|
---|
| 397 | va_list list;
|
---|
| 398 | va_start(list, format);
|
---|
| 399 | int result = posix_vsscanf(s, format, list);
|
---|
| 400 | va_end(list);
|
---|
| 401 | return result;
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | /**
|
---|
| 405 | *
|
---|
| 406 | * @param s
|
---|
| 407 | * @param format
|
---|
| 408 | * @param arg
|
---|
| 409 | * @return
|
---|
| 410 | */
|
---|
| 411 | int posix_vsscanf(
|
---|
| 412 | const char *restrict s, const char *restrict format, va_list arg)
|
---|
[59f799b] | 413 | {
|
---|
| 414 | // TODO
|
---|
| 415 | not_implemented();
|
---|
| 416 | }
|
---|
| 417 |
|
---|
[8b5fb5e] | 418 | void posix_flockfile(FILE *file)
|
---|
| 419 | {
|
---|
| 420 | /* dummy */
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | int posix_ftrylockfile(FILE *file)
|
---|
| 424 | {
|
---|
| 425 | /* dummy */
|
---|
| 426 | return 0;
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | void posix_funlockfile(FILE *file)
|
---|
| 430 | {
|
---|
| 431 | /* dummy */
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | int posix_getc_unlocked(FILE *stream)
|
---|
| 435 | {
|
---|
| 436 | return getc(stream);
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | int posix_getchar_unlocked(void)
|
---|
| 440 | {
|
---|
| 441 | return getchar();
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | int posix_putc_unlocked(int c, FILE *stream)
|
---|
| 445 | {
|
---|
| 446 | return putc(c, stream);
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | int posix_putchar_unlocked(int c)
|
---|
| 450 | {
|
---|
| 451 | return putchar(c);
|
---|
| 452 | }
|
---|
| 453 |
|
---|
[823a929] | 454 | /**
|
---|
| 455 | *
|
---|
| 456 | * @param path
|
---|
| 457 | * @return
|
---|
| 458 | */
|
---|
| 459 | int posix_remove(const char *path)
|
---|
| 460 | {
|
---|
[8b5fb5e] | 461 | // FIXME: unlink() and rmdir() seem to be equivalent at the moment,
|
---|
| 462 | // but that does not have to be true forever
|
---|
| 463 | return unlink(path);
|
---|
[823a929] | 464 | }
|
---|
| 465 |
|
---|
| 466 | /**
|
---|
| 467 | *
|
---|
| 468 | * @param s
|
---|
| 469 | * @return
|
---|
| 470 | */
|
---|
| 471 | char *posix_tmpnam(char *s)
|
---|
| 472 | {
|
---|
| 473 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 474 | not_implemented();
|
---|
| 475 | }
|
---|
| 476 |
|
---|
[09b0b1fb] | 477 | /** @}
|
---|
| 478 | */
|
---|