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