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