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