[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 | */
|
---|
[4cf8ca6] | 33 | /** @file Standard buffered input/output.
|
---|
[09b0b1fb] | 34 | */
|
---|
| 35 |
|
---|
[491e1ee] | 36 | #define LIBPOSIX_INTERNAL
|
---|
[09b0b1fb] | 37 |
|
---|
[a6d908c1] | 38 | /* Has to be first. */
|
---|
| 39 | #include "stdbool.h"
|
---|
[4f4b4e7] | 40 |
|
---|
[9b1503e] | 41 | #include "internal/common.h"
|
---|
[09b0b1fb] | 42 | #include "stdio.h"
|
---|
[a6d908c1] | 43 |
|
---|
| 44 | #include "assert.h"
|
---|
| 45 | #include "errno.h"
|
---|
[ab547063] | 46 | #include "string.h"
|
---|
[1978a5f] | 47 | #include "sys/types.h"
|
---|
[4f4b4e7] | 48 |
|
---|
[a6d908c1] | 49 | #include "libc/io/printf_core.h"
|
---|
| 50 | #include "libc/str.h"
|
---|
[08053f7] | 51 | #include "libc/malloc.h"
|
---|
[fd4b636] | 52 | #include "libc/adt/list.h"
|
---|
| 53 | #include "libc/sys/stat.h"
|
---|
[a6d908c1] | 54 |
|
---|
| 55 |
|
---|
[1978a5f] | 56 | /* not the best of solutions, but freopen and ungetc will eventually
|
---|
[4f4b4e7] | 57 | * need to be implemented in libc anyway
|
---|
| 58 | */
|
---|
[ab547063] | 59 | #include "../c/generic/private/stdio.h"
|
---|
[09b0b1fb] | 60 |
|
---|
[8b5fb5e] | 61 | /** Clears the stream's error and end-of-file indicators.
|
---|
| 62 | *
|
---|
[1978a5f] | 63 | * @param stream Stream whose indicators shall be cleared.
|
---|
[8b5fb5e] | 64 | */
|
---|
| 65 | void posix_clearerr(FILE *stream)
|
---|
| 66 | {
|
---|
| 67 | stream->error = 0;
|
---|
| 68 | stream->eof = 0;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
[1978a5f] | 72 | * Generate a pathname for the controlling terminal.
|
---|
[8b5fb5e] | 73 | *
|
---|
[1978a5f] | 74 | * @param s Allocated buffer to which the pathname shall be put.
|
---|
| 75 | * @return Either s or static location filled with the requested pathname.
|
---|
[8b5fb5e] | 76 | */
|
---|
| 77 | char *posix_ctermid(char *s)
|
---|
| 78 | {
|
---|
| 79 | /* Currently always returns an error value (empty string). */
|
---|
| 80 | // TODO: return a real terminal path
|
---|
| 81 |
|
---|
| 82 | static char dummy_path[L_ctermid] = {'\0'};
|
---|
| 83 |
|
---|
| 84 | if (s == NULL) {
|
---|
| 85 | return dummy_path;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | s[0] = '\0';
|
---|
| 89 | return s;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[221afc9e] | 92 | /**
|
---|
| 93 | * Put a string on the stream.
|
---|
| 94 | *
|
---|
| 95 | * @param s String to be written.
|
---|
| 96 | * @param stream Output stream.
|
---|
| 97 | * @return Non-negative on success, EOF on failure.
|
---|
| 98 | */
|
---|
| 99 | int posix_fputs(const char *restrict s, FILE *restrict stream)
|
---|
| 100 | {
|
---|
| 101 | int rc = fputs(s, stream);
|
---|
| 102 | if (rc == 0) {
|
---|
| 103 | return EOF;
|
---|
| 104 | } else {
|
---|
| 105 | return 0;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[59f799b] | 109 | /**
|
---|
[1978a5f] | 110 | * Push byte back into input stream.
|
---|
[59f799b] | 111 | *
|
---|
[1978a5f] | 112 | * @param c Byte to be pushed back.
|
---|
| 113 | * @param stream Stream to where the byte shall be pushed.
|
---|
[08053f7] | 114 | * @return Provided byte on success or EOF if not possible.
|
---|
[59f799b] | 115 | */
|
---|
| 116 | int posix_ungetc(int c, FILE *stream)
|
---|
| 117 | {
|
---|
[1978a5f] | 118 | uint8_t b = (uint8_t) c;
|
---|
| 119 |
|
---|
| 120 | bool can_unget =
|
---|
[46ac986] | 121 | /* Provided character is legal. */
|
---|
[1978a5f] | 122 | c != EOF &&
|
---|
[46ac986] | 123 | /* Stream is consistent. */
|
---|
[1978a5f] | 124 | !stream->error &&
|
---|
[46ac986] | 125 | /* Stream is buffered. */
|
---|
[1978a5f] | 126 | stream->btype != _IONBF &&
|
---|
[46ac986] | 127 | /* Last operation on the stream was a read operation. */
|
---|
[1978a5f] | 128 | stream->buf_state == _bs_read &&
|
---|
[46ac986] | 129 | /* Stream buffer is already allocated (i.e. there was already carried
|
---|
| 130 | * out either write or read operation on the stream). This is probably
|
---|
| 131 | * redundant check but let's be safe. */
|
---|
[1978a5f] | 132 | stream->buf != NULL &&
|
---|
[46ac986] | 133 | /* There is still space in the stream to retreat. POSIX demands the
|
---|
| 134 | * possibility to unget at least 1 character. It should be always
|
---|
| 135 | * possible, assuming the last operation on the stream read at least 1
|
---|
| 136 | * character, because the buffer is refilled in the lazily manner. */
|
---|
[1978a5f] | 137 | stream->buf_tail > stream->buf;
|
---|
| 138 |
|
---|
| 139 | if (can_unget) {
|
---|
| 140 | --stream->buf_tail;
|
---|
| 141 | stream->buf_tail[0] = b;
|
---|
| 142 | stream->eof = false;
|
---|
| 143 | return (int) b;
|
---|
| 144 | } else {
|
---|
| 145 | return EOF;
|
---|
| 146 | }
|
---|
[59f799b] | 147 | }
|
---|
| 148 |
|
---|
[1978a5f] | 149 | /**
|
---|
[08053f7] | 150 | * Read a stream until the delimiter (or EOF) is encountered.
|
---|
[1978a5f] | 151 | *
|
---|
[08053f7] | 152 | * @param lineptr Pointer to the output buffer in which there will be stored
|
---|
| 153 | * nul-terminated string together with the delimiter (if encountered).
|
---|
| 154 | * Will be resized if necessary.
|
---|
| 155 | * @param n Pointer to the size of the output buffer. Will be increased if
|
---|
| 156 | * necessary.
|
---|
| 157 | * @param delimiter Delimiter on which to finish reading the stream.
|
---|
| 158 | * @param stream Input stream.
|
---|
| 159 | * @return Number of fetched characters (including delimiter if encountered)
|
---|
| 160 | * or -1 on error (set in errno).
|
---|
[1978a5f] | 161 | */
|
---|
[8b5fb5e] | 162 | ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
|
---|
| 163 | int delimiter, FILE *restrict stream)
|
---|
| 164 | {
|
---|
[08053f7] | 165 | /* Check arguments for sanity. */
|
---|
| 166 | if (!lineptr || !n) {
|
---|
| 167 | errno = EINVAL;
|
---|
| 168 | return -1;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | size_t alloc_step = 80; /* Buffer size gain during reallocation. */
|
---|
| 172 | char *pos = *lineptr; /* Next free byte of the output buffer. */
|
---|
| 173 | size_t cnt = 0; /* Number of fetched characters. */
|
---|
| 174 | int c = fgetc(stream); /* Current input character. Might be EOF. */
|
---|
| 175 |
|
---|
| 176 | do {
|
---|
| 177 | /* Mask EOF as NUL to terminate string. */
|
---|
| 178 | if (c == EOF) {
|
---|
| 179 | c = '\0';
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /* Ensure there is still space left in the buffer. */
|
---|
| 183 | if (pos == *lineptr + *n) {
|
---|
| 184 | *lineptr = realloc(*lineptr, *n + alloc_step);
|
---|
| 185 | if (*lineptr) {
|
---|
| 186 | pos = *lineptr + *n;
|
---|
| 187 | *n += alloc_step;
|
---|
| 188 | } else {
|
---|
| 189 | errno = ENOMEM;
|
---|
| 190 | return -1;
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /* Store the fetched character. */
|
---|
| 195 | *pos = c;
|
---|
| 196 |
|
---|
| 197 | /* Fetch the next character according to the current character. */
|
---|
| 198 | if (c != '\0') {
|
---|
| 199 | ++pos;
|
---|
| 200 | ++cnt;
|
---|
| 201 | if (c == delimiter) {
|
---|
| 202 | /* Delimiter was just stored. Provide EOF as the next
|
---|
| 203 | * character - it will be masked as NUL and output string
|
---|
| 204 | * will be properly terminated. */
|
---|
| 205 | c = EOF;
|
---|
| 206 | } else {
|
---|
| 207 | /* Neither delimiter nor EOF were encountered. Just fetch
|
---|
| 208 | * the next character from the stream. */
|
---|
| 209 | c = fgetc(stream);
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | } while (c != '\0');
|
---|
| 213 |
|
---|
| 214 | if (errno == EOK && cnt > 0) {
|
---|
| 215 | return cnt;
|
---|
| 216 | } else {
|
---|
| 217 | /* Either some error occured or the stream was already at EOF. */
|
---|
| 218 | return -1;
|
---|
| 219 | }
|
---|
[8b5fb5e] | 220 | }
|
---|
| 221 |
|
---|
[1978a5f] | 222 | /**
|
---|
[08053f7] | 223 | * Read a stream until the newline (or EOF) is encountered.
|
---|
[1978a5f] | 224 | *
|
---|
[08053f7] | 225 | * @param lineptr Pointer to the output buffer in which there will be stored
|
---|
| 226 | * nul-terminated string together with the delimiter (if encountered).
|
---|
| 227 | * Will be resized if necessary.
|
---|
| 228 | * @param n Pointer to the size of the output buffer. Will be increased if
|
---|
| 229 | * necessary.
|
---|
| 230 | * @param stream Input stream.
|
---|
| 231 | * @return Number of fetched characters (including newline if encountered)
|
---|
| 232 | * or -1 on error (set in errno).
|
---|
[1978a5f] | 233 | */
|
---|
[8b5fb5e] | 234 | ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
|
---|
| 235 | FILE *restrict stream)
|
---|
| 236 | {
|
---|
[08053f7] | 237 | return posix_getdelim(lineptr, n, '\n', stream);
|
---|
[8b5fb5e] | 238 | }
|
---|
| 239 |
|
---|
[4f4b4e7] | 240 | /**
|
---|
[1978a5f] | 241 | * Reopen a file stream.
|
---|
[4f4b4e7] | 242 | *
|
---|
[1978a5f] | 243 | * @param filename Pathname of a file to be reopened or NULL for changing
|
---|
| 244 | * the mode of the stream.
|
---|
| 245 | * @param mode Mode to be used for reopening the file or changing current
|
---|
| 246 | * mode of the stream.
|
---|
| 247 | * @param stream Current stream associated with the opened file.
|
---|
| 248 | * @return On success, either a stream of the reopened file or the provided
|
---|
| 249 | * stream with a changed mode. NULL otherwise.
|
---|
[4f4b4e7] | 250 | */
|
---|
[46ac986] | 251 | FILE *posix_freopen(const char *restrict filename,
|
---|
| 252 | const char *restrict mode, FILE *restrict stream)
|
---|
[09b0b1fb] | 253 | {
|
---|
[ab547063] | 254 | assert(mode != NULL);
|
---|
| 255 | assert(stream != NULL);
|
---|
[fd4b636] | 256 |
|
---|
| 257 | /* Retieve the node. */
|
---|
| 258 | struct stat st;
|
---|
| 259 | int rc;
|
---|
| 260 |
|
---|
[ab547063] | 261 | if (filename == NULL) {
|
---|
[fd4b636] | 262 | rc = fstat(stream->fd, &st);
|
---|
| 263 | } else {
|
---|
| 264 | rc = stat(filename, &st);
|
---|
[ab547063] | 265 | }
|
---|
[fd4b636] | 266 |
|
---|
| 267 | if (rc != EOK) {
|
---|
| 268 | fclose(stream);
|
---|
| 269 | errno = -rc;
|
---|
[ab547063] | 270 | return NULL;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[fd4b636] | 273 | fdi_node_t node = {
|
---|
| 274 | .fs_handle = st.fs_handle,
|
---|
| 275 | .devmap_handle = st.devmap_handle,
|
---|
| 276 | .index = st.index
|
---|
| 277 | };
|
---|
| 278 |
|
---|
| 279 | /* Open a new stream. */
|
---|
| 280 | FILE* new = fopen_node(&node, mode);
|
---|
| 281 | if (new == NULL) {
|
---|
| 282 | fclose(stream);
|
---|
| 283 | /* fopen_node() sets errno. */
|
---|
[ab547063] | 284 | return NULL;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[fd4b636] | 287 | /* Close the original stream without freeing it (ignoring errors). */
|
---|
| 288 | if (stream->buf != NULL) {
|
---|
| 289 | fflush(stream);
|
---|
| 290 | }
|
---|
| 291 | if (stream->sess != NULL) {
|
---|
| 292 | async_hangup(stream->sess);
|
---|
| 293 | }
|
---|
| 294 | if (stream->fd >= 0) {
|
---|
| 295 | close(stream->fd);
|
---|
| 296 | }
|
---|
| 297 | list_remove(&stream->link);
|
---|
| 298 |
|
---|
| 299 | /* Move the new stream to the original location. */
|
---|
| 300 | memcpy(stream, new, sizeof (FILE));
|
---|
| 301 | free(new);
|
---|
[ab547063] | 302 |
|
---|
[fd4b636] | 303 | /* Update references in the file list. */
|
---|
[ab547063] | 304 | stream->link.next->prev = &stream->link;
|
---|
| 305 | stream->link.prev->next = &stream->link;
|
---|
| 306 |
|
---|
| 307 | return stream;
|
---|
[09b0b1fb] | 308 | }
|
---|
| 309 |
|
---|
[1978a5f] | 310 | /**
|
---|
| 311 | *
|
---|
| 312 | * @param buf
|
---|
| 313 | * @param size
|
---|
| 314 | * @param mode
|
---|
| 315 | * @return
|
---|
| 316 | */
|
---|
[8b5fb5e] | 317 | FILE *posix_fmemopen(void *restrict buf, size_t size,
|
---|
| 318 | const char *restrict mode)
|
---|
| 319 | {
|
---|
| 320 | // TODO
|
---|
| 321 | not_implemented();
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[1978a5f] | 324 | /**
|
---|
| 325 | *
|
---|
| 326 | * @param bufp
|
---|
| 327 | * @param sizep
|
---|
| 328 | * @return
|
---|
| 329 | */
|
---|
[8b5fb5e] | 330 | FILE *posix_open_memstream(char **bufp, size_t *sizep)
|
---|
| 331 | {
|
---|
| 332 | // TODO
|
---|
| 333 | not_implemented();
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[4f4b4e7] | 336 | /**
|
---|
[1978a5f] | 337 | * Write error messages to standard error.
|
---|
[4f4b4e7] | 338 | *
|
---|
[1978a5f] | 339 | * @param s Error message.
|
---|
[4f4b4e7] | 340 | */
|
---|
[09b0b1fb] | 341 | void posix_perror(const char *s)
|
---|
| 342 | {
|
---|
[8b5fb5e] | 343 | if (s == NULL || s[0] == '\0') {
|
---|
| 344 | fprintf(stderr, "%s\n", posix_strerror(errno));
|
---|
| 345 | } else {
|
---|
| 346 | fprintf(stderr, "%s: %s\n", s, posix_strerror(errno));
|
---|
| 347 | }
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | struct _posix_fpos {
|
---|
| 351 | off64_t offset;
|
---|
| 352 | };
|
---|
| 353 |
|
---|
| 354 | /** Restores stream a to position previously saved with fgetpos().
|
---|
| 355 | *
|
---|
| 356 | * @param stream Stream to restore
|
---|
| 357 | * @param pos Position to restore
|
---|
| 358 | * @return Zero on success, non-zero (with errno set) on failure
|
---|
| 359 | */
|
---|
| 360 | int posix_fsetpos(FILE *stream, const posix_fpos_t *pos)
|
---|
| 361 | {
|
---|
| 362 | return fseek(stream, pos->offset, SEEK_SET);
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | /** Saves the stream's position for later use by fsetpos().
|
---|
| 366 | *
|
---|
| 367 | * @param stream Stream to save
|
---|
| 368 | * @param pos Place to store the position
|
---|
| 369 | * @return Zero on success, non-zero (with errno set) on failure
|
---|
| 370 | */
|
---|
| 371 | int posix_fgetpos(FILE *restrict stream, posix_fpos_t *restrict pos)
|
---|
| 372 | {
|
---|
| 373 | off64_t ret = ftell(stream);
|
---|
[cfbb5d18] | 374 | if (ret != -1) {
|
---|
| 375 | pos->offset = ret;
|
---|
| 376 | return 0;
|
---|
| 377 | } else {
|
---|
| 378 | return -1;
|
---|
[8b5fb5e] | 379 | }
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | /**
|
---|
[1978a5f] | 383 | * Reposition a file-position indicator in a stream.
|
---|
[8b5fb5e] | 384 | *
|
---|
[1978a5f] | 385 | * @param stream Stream to seek in.
|
---|
| 386 | * @param offset Direction and amount of bytes to seek.
|
---|
| 387 | * @param whence From where to seek.
|
---|
| 388 | * @return Zero on success, -1 otherwise.
|
---|
[8b5fb5e] | 389 | */
|
---|
| 390 | int posix_fseek(FILE *stream, long offset, int whence)
|
---|
| 391 | {
|
---|
| 392 | return fseek(stream, (off64_t) offset, whence);
|
---|
[09b0b1fb] | 393 | }
|
---|
| 394 |
|
---|
[b08ef1fd] | 395 | /**
|
---|
[1978a5f] | 396 | * Reposition a file-position indicator in a stream.
|
---|
[b08ef1fd] | 397 | *
|
---|
[1978a5f] | 398 | * @param stream Stream to seek in.
|
---|
| 399 | * @param offset Direction and amount of bytes to seek.
|
---|
| 400 | * @param whence From where to seek.
|
---|
| 401 | * @return Zero on success, -1 otherwise.
|
---|
[b08ef1fd] | 402 | */
|
---|
| 403 | int posix_fseeko(FILE *stream, posix_off_t offset, int whence)
|
---|
| 404 | {
|
---|
[8b5fb5e] | 405 | return fseek(stream, (off64_t) offset, whence);
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | /**
|
---|
[1978a5f] | 409 | * Discover current file offset in a stream.
|
---|
[8b5fb5e] | 410 | *
|
---|
[1978a5f] | 411 | * @param stream Stream for which the offset shall be retrieved.
|
---|
| 412 | * @return Current offset or -1 if not possible.
|
---|
[8b5fb5e] | 413 | */
|
---|
| 414 | long posix_ftell(FILE *stream)
|
---|
| 415 | {
|
---|
| 416 | return (long) ftell(stream);
|
---|
[b08ef1fd] | 417 | }
|
---|
| 418 |
|
---|
| 419 | /**
|
---|
[1978a5f] | 420 | * Discover current file offset in a stream.
|
---|
[b08ef1fd] | 421 | *
|
---|
[1978a5f] | 422 | * @param stream Stream for which the offset shall be retrieved.
|
---|
| 423 | * @return Current offset or -1 if not possible.
|
---|
[b08ef1fd] | 424 | */
|
---|
| 425 | posix_off_t posix_ftello(FILE *stream)
|
---|
| 426 | {
|
---|
[8b5fb5e] | 427 | return (posix_off_t) ftell(stream);
|
---|
| 428 | }
|
---|
| 429 |
|
---|
[221afc9e] | 430 | /**
|
---|
| 431 | * Discard prefetched data or write unwritten data.
|
---|
| 432 | *
|
---|
| 433 | * @param stream Stream that shall be flushed.
|
---|
| 434 | * @return Zero on success, EOF on failure.
|
---|
| 435 | */
|
---|
| 436 | int posix_fflush(FILE *stream)
|
---|
| 437 | {
|
---|
| 438 | int rc = fflush(stream);
|
---|
| 439 | if (rc < 0) {
|
---|
| 440 | errno = -rc;
|
---|
| 441 | return EOF;
|
---|
| 442 | } else {
|
---|
| 443 | return 0;
|
---|
| 444 | }
|
---|
| 445 | }
|
---|
| 446 |
|
---|
[1978a5f] | 447 | /**
|
---|
| 448 | * Print formatted output to the opened file.
|
---|
| 449 | *
|
---|
| 450 | * @param fildes File descriptor of the opened file.
|
---|
| 451 | * @param format Format description.
|
---|
| 452 | * @return Either the number of printed characters or negative value on error.
|
---|
| 453 | */
|
---|
[8b5fb5e] | 454 | int posix_dprintf(int fildes, const char *restrict format, ...)
|
---|
| 455 | {
|
---|
| 456 | va_list list;
|
---|
| 457 | va_start(list, format);
|
---|
| 458 | int result = posix_vdprintf(fildes, format, list);
|
---|
| 459 | va_end(list);
|
---|
| 460 | return result;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
[1978a5f] | 463 | /**
|
---|
| 464 | * Write ordinary string to the opened file.
|
---|
| 465 | *
|
---|
| 466 | * @param str String to be written.
|
---|
| 467 | * @param size Size of the string (in bytes)..
|
---|
| 468 | * @param fd File descriptor of the opened file.
|
---|
| 469 | * @return The number of written characters.
|
---|
| 470 | */
|
---|
[8b5fb5e] | 471 | static int _dprintf_str_write(const char *str, size_t size, void *fd)
|
---|
| 472 | {
|
---|
| 473 | ssize_t wr = write(*(int *) fd, str, size);
|
---|
| 474 | return str_nlength(str, wr);
|
---|
| 475 | }
|
---|
| 476 |
|
---|
[1978a5f] | 477 | /**
|
---|
| 478 | * Write wide string to the opened file.
|
---|
| 479 | *
|
---|
| 480 | * @param str String to be written.
|
---|
| 481 | * @param size Size of the string (in bytes).
|
---|
| 482 | * @param fd File descriptor of the opened file.
|
---|
| 483 | * @return The number of written characters.
|
---|
| 484 | */
|
---|
[8b5fb5e] | 485 | static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
|
---|
| 486 | {
|
---|
| 487 | size_t offset = 0;
|
---|
| 488 | size_t chars = 0;
|
---|
| 489 | size_t sz;
|
---|
| 490 | char buf[4];
|
---|
| 491 |
|
---|
| 492 | while (offset < size) {
|
---|
| 493 | sz = 0;
|
---|
| 494 | if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) {
|
---|
| 495 | break;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | if (write(*(int *) fd, buf, sz) != (ssize_t) sz) {
|
---|
| 499 | break;
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | chars++;
|
---|
| 503 | offset += sizeof(wchar_t);
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | return chars;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
[1978a5f] | 509 | /**
|
---|
| 510 | * Print formatted output to the opened file.
|
---|
| 511 | *
|
---|
| 512 | * @param fildes File descriptor of the opened file.
|
---|
| 513 | * @param format Format description.
|
---|
| 514 | * @param ap Print arguments.
|
---|
| 515 | * @return Either the number of printed characters or negative value on error.
|
---|
| 516 | */
|
---|
[8b5fb5e] | 517 | int posix_vdprintf(int fildes, const char *restrict format, va_list ap)
|
---|
| 518 | {
|
---|
| 519 | printf_spec_t spec = {
|
---|
| 520 | .str_write = _dprintf_str_write,
|
---|
| 521 | .wstr_write = _dprintf_wstr_write,
|
---|
| 522 | .data = &fildes
|
---|
| 523 | };
|
---|
| 524 |
|
---|
| 525 | return printf_core(format, &spec, ap);
|
---|
[b08ef1fd] | 526 | }
|
---|
| 527 |
|
---|
[59f799b] | 528 | /**
|
---|
[1978a5f] | 529 | * Print formatted output to the string.
|
---|
[59f799b] | 530 | *
|
---|
[1978a5f] | 531 | * @param s Output string.
|
---|
| 532 | * @param format Format description.
|
---|
| 533 | * @return Either the number of printed characters (excluding null byte) or
|
---|
| 534 | * negative value on error.
|
---|
[59f799b] | 535 | */
|
---|
[08053f7] | 536 | int posix_sprintf(char *s, const char *restrict format, ...)
|
---|
[59f799b] | 537 | {
|
---|
[8b5fb5e] | 538 | va_list list;
|
---|
| 539 | va_start(list, format);
|
---|
| 540 | int result = posix_vsprintf(s, format, list);
|
---|
| 541 | va_end(list);
|
---|
| 542 | return result;
|
---|
[59f799b] | 543 | }
|
---|
| 544 |
|
---|
[823a929] | 545 | /**
|
---|
[1978a5f] | 546 | * Print formatted output to the string.
|
---|
[823a929] | 547 | *
|
---|
[1978a5f] | 548 | * @param s Output string.
|
---|
| 549 | * @param format Format description.
|
---|
| 550 | * @param ap Print arguments.
|
---|
| 551 | * @return Either the number of printed characters (excluding null byte) or
|
---|
| 552 | * negative value on error.
|
---|
[823a929] | 553 | */
|
---|
[08053f7] | 554 | int posix_vsprintf(char *s, const char *restrict format, va_list ap)
|
---|
[823a929] | 555 | {
|
---|
[8b5fb5e] | 556 | return vsnprintf(s, STR_NO_LIMIT, format, ap);
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | /**
|
---|
[1978a5f] | 560 | * Convert formatted input from the stream.
|
---|
[8b5fb5e] | 561 | *
|
---|
[1978a5f] | 562 | * @param stream Input stream.
|
---|
| 563 | * @param format Format description.
|
---|
| 564 | * @return The number of converted output items or EOF on failure.
|
---|
[8b5fb5e] | 565 | */
|
---|
| 566 | int posix_fscanf(FILE *restrict stream, const char *restrict format, ...)
|
---|
| 567 | {
|
---|
| 568 | va_list list;
|
---|
| 569 | va_start(list, format);
|
---|
| 570 | int result = posix_vfscanf(stream, format, list);
|
---|
| 571 | va_end(list);
|
---|
| 572 | return result;
|
---|
| 573 | }
|
---|
| 574 |
|
---|
| 575 | /**
|
---|
[1978a5f] | 576 | * Convert formatted input from the standard input.
|
---|
[8b5fb5e] | 577 | *
|
---|
[1978a5f] | 578 | * @param format Format description.
|
---|
| 579 | * @return The number of converted output items or EOF on failure.
|
---|
[8b5fb5e] | 580 | */
|
---|
| 581 | int posix_scanf(const char *restrict format, ...)
|
---|
| 582 | {
|
---|
| 583 | va_list list;
|
---|
| 584 | va_start(list, format);
|
---|
| 585 | int result = posix_vscanf(format, list);
|
---|
| 586 | va_end(list);
|
---|
| 587 | return result;
|
---|
| 588 | }
|
---|
| 589 |
|
---|
| 590 | /**
|
---|
[1978a5f] | 591 | * Convert formatted input from the standard input.
|
---|
[8b5fb5e] | 592 | *
|
---|
[1978a5f] | 593 | * @param format Format description.
|
---|
| 594 | * @param arg Output items.
|
---|
| 595 | * @return The number of converted output items or EOF on failure.
|
---|
[8b5fb5e] | 596 | */
|
---|
| 597 | int posix_vscanf(const char *restrict format, va_list arg)
|
---|
| 598 | {
|
---|
| 599 | return posix_vfscanf(stdin, format, arg);
|
---|
| 600 | }
|
---|
| 601 |
|
---|
[59f799b] | 602 | /**
|
---|
[1978a5f] | 603 | * Convert formatted input from the string.
|
---|
[59f799b] | 604 | *
|
---|
[1978a5f] | 605 | * @param s Input string.
|
---|
| 606 | * @param format Format description.
|
---|
| 607 | * @return The number of converted output items or EOF on failure.
|
---|
[59f799b] | 608 | */
|
---|
[08053f7] | 609 | int posix_sscanf(const char *restrict s, const char *restrict format, ...)
|
---|
[8b5fb5e] | 610 | {
|
---|
| 611 | va_list list;
|
---|
| 612 | va_start(list, format);
|
---|
| 613 | int result = posix_vsscanf(s, format, list);
|
---|
| 614 | va_end(list);
|
---|
| 615 | return result;
|
---|
| 616 | }
|
---|
| 617 |
|
---|
[1978a5f] | 618 | /**
|
---|
| 619 | * Acquire file stream for the thread.
|
---|
| 620 | *
|
---|
| 621 | * @param file File stream to lock.
|
---|
| 622 | */
|
---|
[8b5fb5e] | 623 | void posix_flockfile(FILE *file)
|
---|
| 624 | {
|
---|
| 625 | /* dummy */
|
---|
| 626 | }
|
---|
| 627 |
|
---|
[1978a5f] | 628 | /**
|
---|
| 629 | * Acquire file stream for the thread (non-blocking).
|
---|
| 630 | *
|
---|
| 631 | * @param file File stream to lock.
|
---|
| 632 | * @return Zero for success and non-zero if the lock cannot be acquired.
|
---|
| 633 | */
|
---|
[8b5fb5e] | 634 | int posix_ftrylockfile(FILE *file)
|
---|
| 635 | {
|
---|
| 636 | /* dummy */
|
---|
| 637 | return 0;
|
---|
| 638 | }
|
---|
| 639 |
|
---|
[1978a5f] | 640 | /**
|
---|
| 641 | * Relinquish the ownership of the locked file stream.
|
---|
| 642 | *
|
---|
| 643 | * @param file File stream to unlock.
|
---|
| 644 | */
|
---|
[8b5fb5e] | 645 | void posix_funlockfile(FILE *file)
|
---|
| 646 | {
|
---|
| 647 | /* dummy */
|
---|
| 648 | }
|
---|
| 649 |
|
---|
[1978a5f] | 650 | /**
|
---|
| 651 | * Get a byte from a stream (thread-unsafe).
|
---|
| 652 | *
|
---|
| 653 | * @param stream Input file stream.
|
---|
| 654 | * @return Either read byte or EOF.
|
---|
| 655 | */
|
---|
[8b5fb5e] | 656 | int posix_getc_unlocked(FILE *stream)
|
---|
| 657 | {
|
---|
| 658 | return getc(stream);
|
---|
| 659 | }
|
---|
| 660 |
|
---|
[1978a5f] | 661 | /**
|
---|
| 662 | * Get a byte from the standard input stream (thread-unsafe).
|
---|
| 663 | *
|
---|
| 664 | * @return Either read byte or EOF.
|
---|
| 665 | */
|
---|
[8b5fb5e] | 666 | int posix_getchar_unlocked(void)
|
---|
| 667 | {
|
---|
| 668 | return getchar();
|
---|
| 669 | }
|
---|
| 670 |
|
---|
[1978a5f] | 671 | /**
|
---|
| 672 | * Put a byte on a stream (thread-unsafe).
|
---|
| 673 | *
|
---|
| 674 | * @param c Byte to output.
|
---|
| 675 | * @param stream Output file stream.
|
---|
| 676 | * @return Either written byte or EOF.
|
---|
| 677 | */
|
---|
[8b5fb5e] | 678 | int posix_putc_unlocked(int c, FILE *stream)
|
---|
| 679 | {
|
---|
| 680 | return putc(c, stream);
|
---|
| 681 | }
|
---|
| 682 |
|
---|
[1978a5f] | 683 | /**
|
---|
| 684 | * Put a byte on the standard output stream (thread-unsafe).
|
---|
| 685 | *
|
---|
| 686 | * @param c Byte to output.
|
---|
| 687 | * @return Either written byte or EOF.
|
---|
| 688 | */
|
---|
[8b5fb5e] | 689 | int posix_putchar_unlocked(int c)
|
---|
| 690 | {
|
---|
| 691 | return putchar(c);
|
---|
| 692 | }
|
---|
| 693 |
|
---|
[823a929] | 694 | /**
|
---|
[58115ae] | 695 | * Remove a file or directory.
|
---|
[823a929] | 696 | *
|
---|
[1978a5f] | 697 | * @param path Pathname of the file that shall be removed.
|
---|
[58115ae] | 698 | * @return Zero on success, -1 (with errno set) otherwise.
|
---|
[823a929] | 699 | */
|
---|
| 700 | int posix_remove(const char *path)
|
---|
| 701 | {
|
---|
[58115ae] | 702 | struct stat st;
|
---|
| 703 | int rc = stat(path, &st);
|
---|
| 704 |
|
---|
| 705 | if (rc != EOK) {
|
---|
| 706 | errno = -rc;
|
---|
| 707 | return -1;
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | if (st.is_directory) {
|
---|
| 711 | rc = rmdir(path);
|
---|
| 712 | } else {
|
---|
| 713 | rc = unlink(path);
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | if (rc != EOK) {
|
---|
| 717 | errno = -rc;
|
---|
| 718 | return -1;
|
---|
| 719 | }
|
---|
| 720 | return 0;
|
---|
[823a929] | 721 | }
|
---|
| 722 |
|
---|
| 723 | /**
|
---|
| 724 | *
|
---|
| 725 | * @param s
|
---|
| 726 | * @return
|
---|
| 727 | */
|
---|
| 728 | char *posix_tmpnam(char *s)
|
---|
| 729 | {
|
---|
| 730 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 731 | not_implemented();
|
---|
| 732 | }
|
---|
| 733 |
|
---|
[09b0b1fb] | 734 | /** @}
|
---|
| 735 | */
|
---|