[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;
|
---|
[ce04ea44] | 345 | ssize_t wr = vfs_write(fildes, &posix_pos[fildes], str, size);
|
---|
[6afc9d7] | 346 | if (wr < 0)
|
---|
[ce04ea44] | 347 | return wr;
|
---|
[8b5fb5e] | 348 | return str_nlength(str, wr);
|
---|
| 349 | }
|
---|
| 350 |
|
---|
[1978a5f] | 351 | /**
|
---|
| 352 | * Write wide string to the opened file.
|
---|
| 353 | *
|
---|
| 354 | * @param str String to be written.
|
---|
| 355 | * @param size Size of the string (in bytes).
|
---|
| 356 | * @param fd File descriptor of the opened file.
|
---|
| 357 | * @return The number of written characters.
|
---|
| 358 | */
|
---|
[8b5fb5e] | 359 | static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
|
---|
| 360 | {
|
---|
| 361 | size_t offset = 0;
|
---|
| 362 | size_t chars = 0;
|
---|
| 363 | size_t sz;
|
---|
| 364 | char buf[4];
|
---|
| 365 |
|
---|
| 366 | while (offset < size) {
|
---|
| 367 | sz = 0;
|
---|
| 368 | if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) {
|
---|
| 369 | break;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[58898d1d] | 372 | const int fildes = *(int *) fd;
|
---|
[ce04ea44] | 373 | if (vfs_write(fildes, &posix_pos[fildes], buf, sz) < 0)
|
---|
[8b5fb5e] | 374 | break;
|
---|
| 375 |
|
---|
| 376 | chars++;
|
---|
| 377 | offset += sizeof(wchar_t);
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | return chars;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
[1978a5f] | 383 | /**
|
---|
| 384 | * Print formatted output to the opened file.
|
---|
| 385 | *
|
---|
| 386 | * @param fildes File descriptor of the opened file.
|
---|
| 387 | * @param format Format description.
|
---|
| 388 | * @param ap Print arguments.
|
---|
| 389 | * @return Either the number of printed characters or negative value on error.
|
---|
| 390 | */
|
---|
[8b5fb5e] | 391 | int posix_vdprintf(int fildes, const char *restrict format, va_list ap)
|
---|
| 392 | {
|
---|
| 393 | printf_spec_t spec = {
|
---|
| 394 | .str_write = _dprintf_str_write,
|
---|
| 395 | .wstr_write = _dprintf_wstr_write,
|
---|
| 396 | .data = &fildes
|
---|
| 397 | };
|
---|
| 398 |
|
---|
| 399 | return printf_core(format, &spec, ap);
|
---|
[b08ef1fd] | 400 | }
|
---|
| 401 |
|
---|
[59f799b] | 402 | /**
|
---|
[1978a5f] | 403 | * Print formatted output to the string.
|
---|
[59f799b] | 404 | *
|
---|
[1978a5f] | 405 | * @param s Output string.
|
---|
| 406 | * @param format Format description.
|
---|
| 407 | * @return Either the number of printed characters (excluding null byte) or
|
---|
| 408 | * negative value on error.
|
---|
[59f799b] | 409 | */
|
---|
[08053f7] | 410 | int posix_sprintf(char *s, const char *restrict format, ...)
|
---|
[59f799b] | 411 | {
|
---|
[8b5fb5e] | 412 | va_list list;
|
---|
| 413 | va_start(list, format);
|
---|
| 414 | int result = posix_vsprintf(s, format, list);
|
---|
| 415 | va_end(list);
|
---|
| 416 | return result;
|
---|
[59f799b] | 417 | }
|
---|
| 418 |
|
---|
[823a929] | 419 | /**
|
---|
[1978a5f] | 420 | * Print formatted output to the string.
|
---|
[823a929] | 421 | *
|
---|
[1978a5f] | 422 | * @param s Output string.
|
---|
| 423 | * @param format Format description.
|
---|
| 424 | * @param ap Print arguments.
|
---|
| 425 | * @return Either the number of printed characters (excluding null byte) or
|
---|
| 426 | * negative value on error.
|
---|
[823a929] | 427 | */
|
---|
[08053f7] | 428 | int posix_vsprintf(char *s, const char *restrict format, va_list ap)
|
---|
[823a929] | 429 | {
|
---|
[8b5fb5e] | 430 | return vsnprintf(s, STR_NO_LIMIT, format, ap);
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | /**
|
---|
[1978a5f] | 434 | * Convert formatted input from the stream.
|
---|
[8b5fb5e] | 435 | *
|
---|
[1978a5f] | 436 | * @param stream Input stream.
|
---|
| 437 | * @param format Format description.
|
---|
| 438 | * @return The number of converted output items or EOF on failure.
|
---|
[8b5fb5e] | 439 | */
|
---|
| 440 | int posix_fscanf(FILE *restrict stream, const char *restrict format, ...)
|
---|
| 441 | {
|
---|
| 442 | va_list list;
|
---|
| 443 | va_start(list, format);
|
---|
| 444 | int result = posix_vfscanf(stream, format, list);
|
---|
| 445 | va_end(list);
|
---|
| 446 | return result;
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | /**
|
---|
[1978a5f] | 450 | * Convert formatted input from the standard input.
|
---|
[8b5fb5e] | 451 | *
|
---|
[1978a5f] | 452 | * @param format Format description.
|
---|
| 453 | * @return The number of converted output items or EOF on failure.
|
---|
[8b5fb5e] | 454 | */
|
---|
| 455 | int posix_scanf(const char *restrict format, ...)
|
---|
| 456 | {
|
---|
| 457 | va_list list;
|
---|
| 458 | va_start(list, format);
|
---|
| 459 | int result = posix_vscanf(format, list);
|
---|
| 460 | va_end(list);
|
---|
| 461 | return result;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | /**
|
---|
[1978a5f] | 465 | * Convert formatted input from the standard input.
|
---|
[8b5fb5e] | 466 | *
|
---|
[1978a5f] | 467 | * @param format Format description.
|
---|
| 468 | * @param arg Output items.
|
---|
| 469 | * @return The number of converted output items or EOF on failure.
|
---|
[8b5fb5e] | 470 | */
|
---|
| 471 | int posix_vscanf(const char *restrict format, va_list arg)
|
---|
| 472 | {
|
---|
| 473 | return posix_vfscanf(stdin, format, arg);
|
---|
| 474 | }
|
---|
| 475 |
|
---|
[59f799b] | 476 | /**
|
---|
[1978a5f] | 477 | * Convert formatted input from the string.
|
---|
[59f799b] | 478 | *
|
---|
[1978a5f] | 479 | * @param s Input string.
|
---|
| 480 | * @param format Format description.
|
---|
| 481 | * @return The number of converted output items or EOF on failure.
|
---|
[59f799b] | 482 | */
|
---|
[08053f7] | 483 | int posix_sscanf(const char *restrict s, const char *restrict format, ...)
|
---|
[8b5fb5e] | 484 | {
|
---|
| 485 | va_list list;
|
---|
| 486 | va_start(list, format);
|
---|
| 487 | int result = posix_vsscanf(s, format, list);
|
---|
| 488 | va_end(list);
|
---|
| 489 | return result;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
[1978a5f] | 492 | /**
|
---|
| 493 | * Acquire file stream for the thread.
|
---|
| 494 | *
|
---|
| 495 | * @param file File stream to lock.
|
---|
| 496 | */
|
---|
[8b5fb5e] | 497 | void posix_flockfile(FILE *file)
|
---|
| 498 | {
|
---|
| 499 | /* dummy */
|
---|
| 500 | }
|
---|
| 501 |
|
---|
[1978a5f] | 502 | /**
|
---|
| 503 | * Acquire file stream for the thread (non-blocking).
|
---|
| 504 | *
|
---|
| 505 | * @param file File stream to lock.
|
---|
| 506 | * @return Zero for success and non-zero if the lock cannot be acquired.
|
---|
| 507 | */
|
---|
[8b5fb5e] | 508 | int posix_ftrylockfile(FILE *file)
|
---|
| 509 | {
|
---|
| 510 | /* dummy */
|
---|
| 511 | return 0;
|
---|
| 512 | }
|
---|
| 513 |
|
---|
[1978a5f] | 514 | /**
|
---|
| 515 | * Relinquish the ownership of the locked file stream.
|
---|
| 516 | *
|
---|
| 517 | * @param file File stream to unlock.
|
---|
| 518 | */
|
---|
[8b5fb5e] | 519 | void posix_funlockfile(FILE *file)
|
---|
| 520 | {
|
---|
| 521 | /* dummy */
|
---|
| 522 | }
|
---|
| 523 |
|
---|
[1978a5f] | 524 | /**
|
---|
| 525 | * Get a byte from a stream (thread-unsafe).
|
---|
| 526 | *
|
---|
| 527 | * @param stream Input file stream.
|
---|
| 528 | * @return Either read byte or EOF.
|
---|
| 529 | */
|
---|
[8b5fb5e] | 530 | int posix_getc_unlocked(FILE *stream)
|
---|
| 531 | {
|
---|
| 532 | return getc(stream);
|
---|
| 533 | }
|
---|
| 534 |
|
---|
[1978a5f] | 535 | /**
|
---|
| 536 | * Get a byte from the standard input stream (thread-unsafe).
|
---|
| 537 | *
|
---|
| 538 | * @return Either read byte or EOF.
|
---|
| 539 | */
|
---|
[8b5fb5e] | 540 | int posix_getchar_unlocked(void)
|
---|
| 541 | {
|
---|
| 542 | return getchar();
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[1978a5f] | 545 | /**
|
---|
| 546 | * Put a byte on a stream (thread-unsafe).
|
---|
| 547 | *
|
---|
| 548 | * @param c Byte to output.
|
---|
| 549 | * @param stream Output file stream.
|
---|
| 550 | * @return Either written byte or EOF.
|
---|
| 551 | */
|
---|
[8b5fb5e] | 552 | int posix_putc_unlocked(int c, FILE *stream)
|
---|
| 553 | {
|
---|
| 554 | return putc(c, stream);
|
---|
| 555 | }
|
---|
| 556 |
|
---|
[1978a5f] | 557 | /**
|
---|
| 558 | * Put a byte on the standard output stream (thread-unsafe).
|
---|
| 559 | *
|
---|
| 560 | * @param c Byte to output.
|
---|
| 561 | * @return Either written byte or EOF.
|
---|
| 562 | */
|
---|
[8b5fb5e] | 563 | int posix_putchar_unlocked(int c)
|
---|
| 564 | {
|
---|
| 565 | return putchar(c);
|
---|
| 566 | }
|
---|
| 567 |
|
---|
[823a929] | 568 | /**
|
---|
[58115ae] | 569 | * Remove a file or directory.
|
---|
[823a929] | 570 | *
|
---|
[1978a5f] | 571 | * @param path Pathname of the file that shall be removed.
|
---|
[58115ae] | 572 | * @return Zero on success, -1 (with errno set) otherwise.
|
---|
[823a929] | 573 | */
|
---|
| 574 | int posix_remove(const char *path)
|
---|
| 575 | {
|
---|
[79ea5af] | 576 | if (rcerrno(vfs_unlink_path, path) != EOK)
|
---|
| 577 | return -1;
|
---|
| 578 | else
|
---|
| 579 | return 0;
|
---|
[823a929] | 580 | }
|
---|
| 581 |
|
---|
[75406dc] | 582 | /**
|
---|
| 583 | * Rename a file or directory.
|
---|
| 584 | *
|
---|
[2a53f71] | 585 | * @param old Old pathname.
|
---|
| 586 | * @param new New pathname.
|
---|
[75406dc] | 587 | * @return Zero on success, -1 (with errno set) otherwise.
|
---|
| 588 | */
|
---|
| 589 | int posix_rename(const char *old, const char *new)
|
---|
| 590 | {
|
---|
[163fc09] | 591 | int rc = rcerrno(vfs_rename_path, old, new);
|
---|
| 592 | if (rc != EOK)
|
---|
| 593 | return -1;
|
---|
| 594 | else
|
---|
| 595 | return 0;
|
---|
[75406dc] | 596 | }
|
---|
| 597 |
|
---|
[823a929] | 598 | /**
|
---|
[11544f4] | 599 | * Get a unique temporary file name (obsolete).
|
---|
| 600 | *
|
---|
| 601 | * @param s Buffer for the file name. Must be at least L_tmpnam bytes long.
|
---|
| 602 | * @return The value of s on success, NULL on failure.
|
---|
[823a929] | 603 | */
|
---|
| 604 | char *posix_tmpnam(char *s)
|
---|
| 605 | {
|
---|
[11544f4] | 606 | assert(L_tmpnam >= posix_strlen("/tmp/tnXXXXXX"));
|
---|
| 607 |
|
---|
| 608 | static char buffer[L_tmpnam + 1];
|
---|
| 609 | if (s == NULL) {
|
---|
| 610 | s = buffer;
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | posix_strcpy(s, "/tmp/tnXXXXXX");
|
---|
| 614 | posix_mktemp(s);
|
---|
| 615 |
|
---|
| 616 | if (*s == '\0') {
|
---|
| 617 | /* Errno set by mktemp(). */
|
---|
| 618 | return NULL;
|
---|
| 619 | }
|
---|
| 620 |
|
---|
| 621 | return s;
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | /**
|
---|
| 625 | * Get an unique temporary file name with additional constraints (obsolete).
|
---|
| 626 | *
|
---|
| 627 | * @param dir Path to directory, where the file should be created.
|
---|
| 628 | * @param pfx Optional prefix up to 5 characters long.
|
---|
| 629 | * @return Newly allocated unique path for temporary file. NULL on failure.
|
---|
| 630 | */
|
---|
| 631 | char *posix_tempnam(const char *dir, const char *pfx)
|
---|
| 632 | {
|
---|
| 633 | /* Sequence number of the filename. */
|
---|
| 634 | static int seq = 0;
|
---|
| 635 |
|
---|
| 636 | size_t dir_len = posix_strlen(dir);
|
---|
| 637 | if (dir[dir_len - 1] == '/') {
|
---|
| 638 | dir_len--;
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | size_t pfx_len = posix_strlen(pfx);
|
---|
| 642 | if (pfx_len > 5) {
|
---|
| 643 | pfx_len = 5;
|
---|
| 644 | }
|
---|
| 645 |
|
---|
| 646 | char *result = malloc(dir_len + /* slash*/ 1 +
|
---|
| 647 | pfx_len + /* three-digit seq */ 3 + /* .tmp */ 4 + /* nul */ 1);
|
---|
| 648 |
|
---|
| 649 | if (result == NULL) {
|
---|
| 650 | errno = ENOMEM;
|
---|
| 651 | return NULL;
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | char *res_ptr = result;
|
---|
| 655 | posix_strncpy(res_ptr, dir, dir_len);
|
---|
| 656 | res_ptr += dir_len;
|
---|
| 657 | posix_strncpy(res_ptr, pfx, pfx_len);
|
---|
| 658 | res_ptr += pfx_len;
|
---|
| 659 |
|
---|
| 660 | for (; seq < 1000; ++seq) {
|
---|
| 661 | snprintf(res_ptr, 8, "%03d.tmp", seq);
|
---|
| 662 |
|
---|
| 663 | int orig_errno = errno;
|
---|
| 664 | errno = 0;
|
---|
| 665 | /* Check if the file exists. */
|
---|
| 666 | if (posix_access(result, F_OK) == -1) {
|
---|
| 667 | if (errno == ENOENT) {
|
---|
| 668 | errno = orig_errno;
|
---|
| 669 | break;
|
---|
| 670 | } else {
|
---|
| 671 | /* errno set by access() */
|
---|
| 672 | return NULL;
|
---|
| 673 | }
|
---|
| 674 | }
|
---|
| 675 | }
|
---|
| 676 |
|
---|
| 677 | if (seq == 1000) {
|
---|
| 678 | free(result);
|
---|
| 679 | errno = EINVAL;
|
---|
| 680 | return NULL;
|
---|
| 681 | }
|
---|
| 682 |
|
---|
| 683 | return result;
|
---|
| 684 | }
|
---|
| 685 |
|
---|
| 686 | /**
|
---|
| 687 | * Create and open an unique temporary file.
|
---|
| 688 | * The file is automatically removed when the stream is closed.
|
---|
| 689 | *
|
---|
| 690 | * @param dir Path to directory, where the file should be created.
|
---|
| 691 | * @param pfx Optional prefix up to 5 characters long.
|
---|
| 692 | * @return Newly allocated unique path for temporary file. NULL on failure.
|
---|
| 693 | */
|
---|
| 694 | FILE *posix_tmpfile(void)
|
---|
| 695 | {
|
---|
| 696 | char filename[] = "/tmp/tfXXXXXX";
|
---|
| 697 | int fd = posix_mkstemp(filename);
|
---|
| 698 | if (fd == -1) {
|
---|
| 699 | /* errno set by mkstemp(). */
|
---|
| 700 | return NULL;
|
---|
| 701 | }
|
---|
| 702 |
|
---|
| 703 | /* Unlink the created file, so that it's removed on close(). */
|
---|
| 704 | posix_unlink(filename);
|
---|
| 705 | return fdopen(fd, "w+");
|
---|
[823a929] | 706 | }
|
---|
| 707 |
|
---|
[09b0b1fb] | 708 | /** @}
|
---|
| 709 | */
|
---|