[09b0b1fb] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
[4f4b4e7] | 3 | * Copyright (c) 2011 Petr Koupy
|
---|
[4e6a610] | 4 | * Copyright (c) 2018 Jiri Svoboda
|
---|
[09b0b1fb] | 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 | /** @addtogroup libposix
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
[4cf8ca6] | 34 | /** @file Standard buffered input/output.
|
---|
[09b0b1fb] | 35 | */
|
---|
| 36 |
|
---|
[9b1503e] | 37 | #include "internal/common.h"
|
---|
[9b8be79] | 38 | #include <stdio.h>
|
---|
[a6d908c1] | 39 |
|
---|
[e8d3c6f5] | 40 | #include <assert.h>
|
---|
[0d0b319] | 41 | #include <errno.h>
|
---|
[4e6a610] | 42 | #include <stdbool.h>
|
---|
| 43 | #include <tmpfile.h>
|
---|
[0d0b319] | 44 |
|
---|
[9b8be79] | 45 | #include <fcntl.h>
|
---|
| 46 | #include <stdlib.h>
|
---|
| 47 | #include <string.h>
|
---|
| 48 | #include <sys/stat.h>
|
---|
| 49 | #include <sys/types.h>
|
---|
| 50 | #include <unistd.h>
|
---|
[4f4b4e7] | 51 |
|
---|
[9b8be79] | 52 | #include <io/printf_core.h>
|
---|
| 53 | #include <str.h>
|
---|
| 54 | #include <malloc.h>
|
---|
| 55 | #include <adt/list.h>
|
---|
[a6d908c1] | 56 |
|
---|
[8b5fb5e] | 57 | /**
|
---|
[1978a5f] | 58 | * Generate a pathname for the controlling terminal.
|
---|
[8b5fb5e] | 59 | *
|
---|
[1978a5f] | 60 | * @param s Allocated buffer to which the pathname shall be put.
|
---|
| 61 | * @return Either s or static location filled with the requested pathname.
|
---|
[8b5fb5e] | 62 | */
|
---|
[7f9df7b9] | 63 | char *ctermid(char *s)
|
---|
[8b5fb5e] | 64 | {
|
---|
| 65 | /* Currently always returns an error value (empty string). */
|
---|
| 66 | // TODO: return a real terminal path
|
---|
| 67 |
|
---|
[1433ecda] | 68 | static char dummy_path[L_ctermid] = { '\0' };
|
---|
[8b5fb5e] | 69 |
|
---|
| 70 | if (s == NULL) {
|
---|
| 71 | return dummy_path;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | s[0] = '\0';
|
---|
| 75 | return s;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[1978a5f] | 78 | /**
|
---|
[08053f7] | 79 | * Read a stream until the delimiter (or EOF) is encountered.
|
---|
[1978a5f] | 80 | *
|
---|
[08053f7] | 81 | * @param lineptr Pointer to the output buffer in which there will be stored
|
---|
| 82 | * nul-terminated string together with the delimiter (if encountered).
|
---|
| 83 | * Will be resized if necessary.
|
---|
| 84 | * @param n Pointer to the size of the output buffer. Will be increased if
|
---|
| 85 | * necessary.
|
---|
| 86 | * @param delimiter Delimiter on which to finish reading the stream.
|
---|
| 87 | * @param stream Input stream.
|
---|
| 88 | * @return Number of fetched characters (including delimiter if encountered)
|
---|
| 89 | * or -1 on error (set in errno).
|
---|
[1978a5f] | 90 | */
|
---|
[7f9df7b9] | 91 | ssize_t getdelim(char **restrict lineptr, size_t *restrict n,
|
---|
[8b5fb5e] | 92 | int delimiter, FILE *restrict stream)
|
---|
| 93 | {
|
---|
[08053f7] | 94 | /* Check arguments for sanity. */
|
---|
| 95 | if (!lineptr || !n) {
|
---|
| 96 | errno = EINVAL;
|
---|
| 97 | return -1;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | size_t alloc_step = 80; /* Buffer size gain during reallocation. */
|
---|
| 101 | char *pos = *lineptr; /* Next free byte of the output buffer. */
|
---|
| 102 | size_t cnt = 0; /* Number of fetched characters. */
|
---|
| 103 | int c = fgetc(stream); /* Current input character. Might be EOF. */
|
---|
| 104 |
|
---|
| 105 | do {
|
---|
| 106 | /* Mask EOF as NUL to terminate string. */
|
---|
| 107 | if (c == EOF) {
|
---|
| 108 | c = '\0';
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /* Ensure there is still space left in the buffer. */
|
---|
| 112 | if (pos == *lineptr + *n) {
|
---|
| 113 | *lineptr = realloc(*lineptr, *n + alloc_step);
|
---|
| 114 | if (*lineptr) {
|
---|
| 115 | pos = *lineptr + *n;
|
---|
| 116 | *n += alloc_step;
|
---|
| 117 | } else {
|
---|
| 118 | errno = ENOMEM;
|
---|
| 119 | return -1;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /* Store the fetched character. */
|
---|
| 124 | *pos = c;
|
---|
| 125 |
|
---|
| 126 | /* Fetch the next character according to the current character. */
|
---|
| 127 | if (c != '\0') {
|
---|
| 128 | ++pos;
|
---|
| 129 | ++cnt;
|
---|
| 130 | if (c == delimiter) {
|
---|
[7c3fb9b] | 131 | /*
|
---|
| 132 | * Delimiter was just stored. Provide EOF as the next
|
---|
[08053f7] | 133 | * character - it will be masked as NUL and output string
|
---|
[7c3fb9b] | 134 | * will be properly terminated.
|
---|
| 135 | */
|
---|
[08053f7] | 136 | c = EOF;
|
---|
| 137 | } else {
|
---|
[7c3fb9b] | 138 | /*
|
---|
| 139 | * Neither delimiter nor EOF were encountered. Just fetch
|
---|
| 140 | * the next character from the stream.
|
---|
| 141 | */
|
---|
[08053f7] | 142 | c = fgetc(stream);
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | } while (c != '\0');
|
---|
| 146 |
|
---|
| 147 | if (errno == EOK && cnt > 0) {
|
---|
| 148 | return cnt;
|
---|
| 149 | } else {
|
---|
| 150 | /* Either some error occured or the stream was already at EOF. */
|
---|
| 151 | return -1;
|
---|
| 152 | }
|
---|
[8b5fb5e] | 153 | }
|
---|
| 154 |
|
---|
[1978a5f] | 155 | /**
|
---|
[08053f7] | 156 | * Read a stream until the newline (or EOF) is encountered.
|
---|
[1b20da0] | 157 | *
|
---|
[08053f7] | 158 | * @param lineptr Pointer to the output buffer in which there will be stored
|
---|
| 159 | * nul-terminated string together with the delimiter (if encountered).
|
---|
| 160 | * Will be resized if necessary.
|
---|
| 161 | * @param n Pointer to the size of the output buffer. Will be increased if
|
---|
| 162 | * necessary.
|
---|
| 163 | * @param stream Input stream.
|
---|
| 164 | * @return Number of fetched characters (including newline if encountered)
|
---|
| 165 | * or -1 on error (set in errno).
|
---|
[1978a5f] | 166 | */
|
---|
[7f9df7b9] | 167 | ssize_t getline(char **restrict lineptr, size_t *restrict n,
|
---|
[8b5fb5e] | 168 | FILE *restrict stream)
|
---|
| 169 | {
|
---|
[7f9df7b9] | 170 | return getdelim(lineptr, n, '\n', stream);
|
---|
[09b0b1fb] | 171 | }
|
---|
| 172 |
|
---|
[8b5fb5e] | 173 | /**
|
---|
[1978a5f] | 174 | * Reposition a file-position indicator in a stream.
|
---|
[1b20da0] | 175 | *
|
---|
[1978a5f] | 176 | * @param stream Stream to seek in.
|
---|
| 177 | * @param offset Direction and amount of bytes to seek.
|
---|
| 178 | * @param whence From where to seek.
|
---|
| 179 | * @return Zero on success, -1 otherwise.
|
---|
[8b5fb5e] | 180 | */
|
---|
[7f9df7b9] | 181 | int fseeko(FILE *stream, off_t offset, int whence)
|
---|
[b08ef1fd] | 182 | {
|
---|
[e0f47f5] | 183 | return fseek64(stream, offset, whence);
|
---|
[8b5fb5e] | 184 | }
|
---|
| 185 |
|
---|
| 186 | /**
|
---|
[1978a5f] | 187 | * Discover current file offset in a stream.
|
---|
[1b20da0] | 188 | *
|
---|
[1978a5f] | 189 | * @param stream Stream for which the offset shall be retrieved.
|
---|
| 190 | * @return Current offset or -1 if not possible.
|
---|
[8b5fb5e] | 191 | */
|
---|
[7f9df7b9] | 192 | off_t ftello(FILE *stream)
|
---|
[b08ef1fd] | 193 | {
|
---|
[e0f47f5] | 194 | return ftell64(stream);
|
---|
[8b5fb5e] | 195 | }
|
---|
| 196 |
|
---|
[1978a5f] | 197 | /**
|
---|
| 198 | * Print formatted output to the opened file.
|
---|
| 199 | *
|
---|
| 200 | * @param fildes File descriptor of the opened file.
|
---|
| 201 | * @param format Format description.
|
---|
| 202 | * @return Either the number of printed characters or negative value on error.
|
---|
| 203 | */
|
---|
[7f9df7b9] | 204 | int dprintf(int fildes, const char *restrict format, ...)
|
---|
[8b5fb5e] | 205 | {
|
---|
| 206 | va_list list;
|
---|
| 207 | va_start(list, format);
|
---|
[7f9df7b9] | 208 | int result = vdprintf(fildes, format, list);
|
---|
[8b5fb5e] | 209 | va_end(list);
|
---|
| 210 | return result;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[1978a5f] | 213 | /**
|
---|
| 214 | * Write ordinary string to the opened file.
|
---|
| 215 | *
|
---|
| 216 | * @param str String to be written.
|
---|
| 217 | * @param size Size of the string (in bytes)..
|
---|
| 218 | * @param fd File descriptor of the opened file.
|
---|
| 219 | * @return The number of written characters.
|
---|
| 220 | */
|
---|
[8b5fb5e] | 221 | static int _dprintf_str_write(const char *str, size_t size, void *fd)
|
---|
| 222 | {
|
---|
[58898d1d] | 223 | const int fildes = *(int *) fd;
|
---|
[8e3498b] | 224 | size_t wr;
|
---|
[0d0b319] | 225 | if (failed(vfs_write(fildes, &posix_pos[fildes], str, size, &wr)))
|
---|
| 226 | return -1;
|
---|
[8b5fb5e] | 227 | return str_nlength(str, wr);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[1978a5f] | 230 | /**
|
---|
| 231 | * Write wide string to the opened file.
|
---|
[1b20da0] | 232 | *
|
---|
[1978a5f] | 233 | * @param str String to be written.
|
---|
| 234 | * @param size Size of the string (in bytes).
|
---|
| 235 | * @param fd File descriptor of the opened file.
|
---|
| 236 | * @return The number of written characters.
|
---|
| 237 | */
|
---|
[8b5fb5e] | 238 | static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
|
---|
| 239 | {
|
---|
| 240 | size_t offset = 0;
|
---|
| 241 | size_t chars = 0;
|
---|
| 242 | size_t sz;
|
---|
| 243 | char buf[4];
|
---|
[a35b458] | 244 |
|
---|
[8b5fb5e] | 245 | while (offset < size) {
|
---|
| 246 | sz = 0;
|
---|
| 247 | if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) {
|
---|
| 248 | break;
|
---|
| 249 | }
|
---|
[a35b458] | 250 |
|
---|
[58898d1d] | 251 | const int fildes = *(int *) fd;
|
---|
[8e3498b] | 252 | size_t nwr;
|
---|
| 253 | if (vfs_write(fildes, &posix_pos[fildes], buf, sz, &nwr) != EOK)
|
---|
[8b5fb5e] | 254 | break;
|
---|
[a35b458] | 255 |
|
---|
[8b5fb5e] | 256 | chars++;
|
---|
| 257 | offset += sizeof(wchar_t);
|
---|
| 258 | }
|
---|
[a35b458] | 259 |
|
---|
[8b5fb5e] | 260 | return chars;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[1978a5f] | 263 | /**
|
---|
| 264 | * Print formatted output to the opened file.
|
---|
[1b20da0] | 265 | *
|
---|
[1978a5f] | 266 | * @param fildes File descriptor of the opened file.
|
---|
| 267 | * @param format Format description.
|
---|
| 268 | * @param ap Print arguments.
|
---|
| 269 | * @return Either the number of printed characters or negative value on error.
|
---|
| 270 | */
|
---|
[7f9df7b9] | 271 | int vdprintf(int fildes, const char *restrict format, va_list ap)
|
---|
[8b5fb5e] | 272 | {
|
---|
| 273 | printf_spec_t spec = {
|
---|
| 274 | .str_write = _dprintf_str_write,
|
---|
| 275 | .wstr_write = _dprintf_wstr_write,
|
---|
| 276 | .data = &fildes
|
---|
| 277 | };
|
---|
[a35b458] | 278 |
|
---|
[8b5fb5e] | 279 | return printf_core(format, &spec, ap);
|
---|
[b08ef1fd] | 280 | }
|
---|
| 281 |
|
---|
[1978a5f] | 282 | /**
|
---|
| 283 | * Acquire file stream for the thread.
|
---|
| 284 | *
|
---|
| 285 | * @param file File stream to lock.
|
---|
| 286 | */
|
---|
[7f9df7b9] | 287 | void flockfile(FILE *file)
|
---|
[8b5fb5e] | 288 | {
|
---|
| 289 | /* dummy */
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[1978a5f] | 292 | /**
|
---|
| 293 | * Acquire file stream for the thread (non-blocking).
|
---|
| 294 | *
|
---|
| 295 | * @param file File stream to lock.
|
---|
| 296 | * @return Zero for success and non-zero if the lock cannot be acquired.
|
---|
| 297 | */
|
---|
[7f9df7b9] | 298 | int ftrylockfile(FILE *file)
|
---|
[8b5fb5e] | 299 | {
|
---|
| 300 | /* dummy */
|
---|
| 301 | return 0;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[1978a5f] | 304 | /**
|
---|
| 305 | * Relinquish the ownership of the locked file stream.
|
---|
| 306 | *
|
---|
| 307 | * @param file File stream to unlock.
|
---|
| 308 | */
|
---|
[7f9df7b9] | 309 | void funlockfile(FILE *file)
|
---|
[8b5fb5e] | 310 | {
|
---|
| 311 | /* dummy */
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[1978a5f] | 314 | /**
|
---|
| 315 | * Get a byte from a stream (thread-unsafe).
|
---|
| 316 | *
|
---|
| 317 | * @param stream Input file stream.
|
---|
| 318 | * @return Either read byte or EOF.
|
---|
| 319 | */
|
---|
[7f9df7b9] | 320 | int getc_unlocked(FILE *stream)
|
---|
[8b5fb5e] | 321 | {
|
---|
| 322 | return getc(stream);
|
---|
| 323 | }
|
---|
| 324 |
|
---|
[1978a5f] | 325 | /**
|
---|
| 326 | * Get a byte from the standard input stream (thread-unsafe).
|
---|
| 327 | *
|
---|
| 328 | * @return Either read byte or EOF.
|
---|
| 329 | */
|
---|
[7f9df7b9] | 330 | int getchar_unlocked(void)
|
---|
[8b5fb5e] | 331 | {
|
---|
| 332 | return getchar();
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[1978a5f] | 335 | /**
|
---|
| 336 | * Put a byte on a stream (thread-unsafe).
|
---|
| 337 | *
|
---|
| 338 | * @param c Byte to output.
|
---|
| 339 | * @param stream Output file stream.
|
---|
| 340 | * @return Either written byte or EOF.
|
---|
| 341 | */
|
---|
[7f9df7b9] | 342 | int putc_unlocked(int c, FILE *stream)
|
---|
[8b5fb5e] | 343 | {
|
---|
| 344 | return putc(c, stream);
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[1978a5f] | 347 | /**
|
---|
| 348 | * Put a byte on the standard output stream (thread-unsafe).
|
---|
[1b20da0] | 349 | *
|
---|
[1978a5f] | 350 | * @param c Byte to output.
|
---|
| 351 | * @return Either written byte or EOF.
|
---|
| 352 | */
|
---|
[7f9df7b9] | 353 | int putchar_unlocked(int c)
|
---|
[8b5fb5e] | 354 | {
|
---|
| 355 | return putchar(c);
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[4e6a610] | 358 | /** Determine if directory is an 'appropriate' temporary directory.
|
---|
[11544f4] | 359 | *
|
---|
[4e6a610] | 360 | * @param dir Directory path
|
---|
| 361 | * @return @c true iff directory is appropriate.
|
---|
[823a929] | 362 | */
|
---|
[4e6a610] | 363 | static bool is_appropriate_tmpdir(const char *dir)
|
---|
[823a929] | 364 | {
|
---|
[4e6a610] | 365 | struct stat sbuf;
|
---|
[a35b458] | 366 |
|
---|
[4e6a610] | 367 | /* Must not be NULL */
|
---|
| 368 | if (dir == NULL)
|
---|
| 369 | return false;
|
---|
[a35b458] | 370 |
|
---|
[4e6a610] | 371 | /* Must not be empty */
|
---|
| 372 | if (dir[0] == '\0')
|
---|
| 373 | return false;
|
---|
[a35b458] | 374 |
|
---|
[4e6a610] | 375 | if (stat(dir, &sbuf) != 0)
|
---|
| 376 | return false;
|
---|
[a35b458] | 377 |
|
---|
[4e6a610] | 378 | /* Must be a directory */
|
---|
| 379 | if ((sbuf.st_mode & S_IFMT) != S_IFDIR)
|
---|
| 380 | return false;
|
---|
| 381 |
|
---|
| 382 | /* Must be writable */
|
---|
| 383 | if (access(dir, W_OK) != 0)
|
---|
| 384 | return false;
|
---|
| 385 |
|
---|
| 386 | return true;
|
---|
[11544f4] | 387 | }
|
---|
| 388 |
|
---|
[4e6a610] | 389 | /** Construct unique file name.
|
---|
| 390 | *
|
---|
| 391 | * Never use this function.
|
---|
[11544f4] | 392 | *
|
---|
| 393 | * @param dir Path to directory, where the file should be created.
|
---|
| 394 | * @param pfx Optional prefix up to 5 characters long.
|
---|
| 395 | * @return Newly allocated unique path for temporary file. NULL on failure.
|
---|
| 396 | */
|
---|
[7f9df7b9] | 397 | char *tempnam(const char *dir, const char *pfx)
|
---|
[11544f4] | 398 | {
|
---|
[4e6a610] | 399 | const char *dpref;
|
---|
| 400 | char *d;
|
---|
| 401 | char *buf;
|
---|
| 402 | int rc;
|
---|
| 403 |
|
---|
| 404 | d = getenv("TMPDIR");
|
---|
| 405 | if (is_appropriate_tmpdir(d))
|
---|
| 406 | dpref = d;
|
---|
| 407 | else if (is_appropriate_tmpdir(dir))
|
---|
| 408 | dpref = dir;
|
---|
| 409 | else if (is_appropriate_tmpdir(P_tmpdir))
|
---|
| 410 | dpref = P_tmpdir;
|
---|
| 411 | else
|
---|
| 412 | dpref = "/";
|
---|
| 413 |
|
---|
| 414 | if (dpref[strlen(dpref) - 1] != '/')
|
---|
| 415 | rc = asprintf(&buf, "%s/%sXXXXXX", dpref, pfx);
|
---|
| 416 | else
|
---|
| 417 | rc = asprintf(&buf, "%s%sXXXXXX", dpref, pfx);
|
---|
| 418 |
|
---|
| 419 | if (rc < 0)
|
---|
[11544f4] | 420 | return NULL;
|
---|
[a35b458] | 421 |
|
---|
[4e6a610] | 422 | rc = __tmpfile_templ(buf, false);
|
---|
| 423 | if (rc != 0) {
|
---|
| 424 | free(buf);
|
---|
[11544f4] | 425 | return NULL;
|
---|
| 426 | }
|
---|
[a35b458] | 427 |
|
---|
[4e6a610] | 428 | return buf;
|
---|
[823a929] | 429 | }
|
---|
| 430 |
|
---|
[09b0b1fb] | 431 | /** @}
|
---|
| 432 | */
|
---|