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