source: mainline/uspace/lib/posix/source/stdio.c@ e80d8f8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e80d8f8 was 32b3a12, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

libposix: even less includes from libc

  • Property mode set to 100644
File size: 19.2 KB
RevLine 
[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
[09b0b1fb]37
[a6d908c1]38/* Has to be first. */
[a3da2b2]39#include "posix/stdbool.h"
[4f4b4e7]40
[9b1503e]41#include "internal/common.h"
[a3da2b2]42#include "posix/stdio.h"
[a6d908c1]43
[a3da2b2]44#include "posix/assert.h"
45#include "posix/errno.h"
46#include "posix/stdlib.h"
47#include "posix/string.h"
48#include "posix/sys/types.h"
49#include "posix/unistd.h"
[4f4b4e7]50
[32b3a12]51#include "libc/stdio.h"
[a6d908c1]52#include "libc/io/printf_core.h"
53#include "libc/str.h"
[08053f7]54#include "libc/malloc.h"
[fd4b636]55#include "libc/adt/list.h"
56#include "libc/sys/stat.h"
[a6d908c1]57
58
[1978a5f]59/* not the best of solutions, but freopen and ungetc will eventually
[4f4b4e7]60 * need to be implemented in libc anyway
61 */
[a3da2b2]62#include "../../c/generic/private/stdio.h"
[09b0b1fb]63
[8b5fb5e]64/** Clears the stream's error and end-of-file indicators.
65 *
[1978a5f]66 * @param stream Stream whose indicators shall be cleared.
[8b5fb5e]67 */
68void posix_clearerr(FILE *stream)
69{
70 stream->error = 0;
71 stream->eof = 0;
72}
73
74/**
[1978a5f]75 * Generate a pathname for the controlling terminal.
[8b5fb5e]76 *
[1978a5f]77 * @param s Allocated buffer to which the pathname shall be put.
78 * @return Either s or static location filled with the requested pathname.
[8b5fb5e]79 */
80char *posix_ctermid(char *s)
81{
82 /* Currently always returns an error value (empty string). */
83 // TODO: return a real terminal path
84
85 static char dummy_path[L_ctermid] = {'\0'};
86
87 if (s == NULL) {
88 return dummy_path;
89 }
90
91 s[0] = '\0';
92 return s;
93}
94
[221afc9e]95/**
96 * Put a string on the stream.
97 *
98 * @param s String to be written.
99 * @param stream Output stream.
100 * @return Non-negative on success, EOF on failure.
101 */
102int posix_fputs(const char *restrict s, FILE *restrict stream)
103{
104 int rc = fputs(s, stream);
105 if (rc == 0) {
106 return EOF;
107 } else {
108 return 0;
109 }
110}
111
[59f799b]112/**
[1978a5f]113 * Push byte back into input stream.
[59f799b]114 *
[1978a5f]115 * @param c Byte to be pushed back.
116 * @param stream Stream to where the byte shall be pushed.
[08053f7]117 * @return Provided byte on success or EOF if not possible.
[59f799b]118 */
119int posix_ungetc(int c, FILE *stream)
120{
[1978a5f]121 uint8_t b = (uint8_t) c;
122
123 bool can_unget =
[46ac986]124 /* Provided character is legal. */
[1978a5f]125 c != EOF &&
[46ac986]126 /* Stream is consistent. */
[1978a5f]127 !stream->error &&
[46ac986]128 /* Stream is buffered. */
[1978a5f]129 stream->btype != _IONBF &&
[46ac986]130 /* Last operation on the stream was a read operation. */
[1978a5f]131 stream->buf_state == _bs_read &&
[46ac986]132 /* Stream buffer is already allocated (i.e. there was already carried
133 * out either write or read operation on the stream). This is probably
134 * redundant check but let's be safe. */
[1978a5f]135 stream->buf != NULL &&
[46ac986]136 /* There is still space in the stream to retreat. POSIX demands the
137 * possibility to unget at least 1 character. It should be always
138 * possible, assuming the last operation on the stream read at least 1
139 * character, because the buffer is refilled in the lazily manner. */
[1978a5f]140 stream->buf_tail > stream->buf;
141
142 if (can_unget) {
143 --stream->buf_tail;
144 stream->buf_tail[0] = b;
145 stream->eof = false;
146 return (int) b;
147 } else {
148 return EOF;
149 }
[59f799b]150}
151
[1978a5f]152/**
[08053f7]153 * Read a stream until the delimiter (or EOF) is encountered.
[1978a5f]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 delimiter Delimiter on which to finish reading the stream.
161 * @param stream Input stream.
162 * @return Number of fetched characters (including delimiter if encountered)
163 * or -1 on error (set in errno).
[1978a5f]164 */
[8b5fb5e]165ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
166 int delimiter, FILE *restrict stream)
167{
[08053f7]168 /* Check arguments for sanity. */
169 if (!lineptr || !n) {
170 errno = EINVAL;
171 return -1;
172 }
173
174 size_t alloc_step = 80; /* Buffer size gain during reallocation. */
175 char *pos = *lineptr; /* Next free byte of the output buffer. */
176 size_t cnt = 0; /* Number of fetched characters. */
177 int c = fgetc(stream); /* Current input character. Might be EOF. */
178
179 do {
180 /* Mask EOF as NUL to terminate string. */
181 if (c == EOF) {
182 c = '\0';
183 }
184
185 /* Ensure there is still space left in the buffer. */
186 if (pos == *lineptr + *n) {
187 *lineptr = realloc(*lineptr, *n + alloc_step);
188 if (*lineptr) {
189 pos = *lineptr + *n;
190 *n += alloc_step;
191 } else {
192 errno = ENOMEM;
193 return -1;
194 }
195 }
196
197 /* Store the fetched character. */
198 *pos = c;
199
200 /* Fetch the next character according to the current character. */
201 if (c != '\0') {
202 ++pos;
203 ++cnt;
204 if (c == delimiter) {
205 /* Delimiter was just stored. Provide EOF as the next
206 * character - it will be masked as NUL and output string
207 * will be properly terminated. */
208 c = EOF;
209 } else {
210 /* Neither delimiter nor EOF were encountered. Just fetch
211 * the next character from the stream. */
212 c = fgetc(stream);
213 }
214 }
215 } while (c != '\0');
216
217 if (errno == EOK && cnt > 0) {
218 return cnt;
219 } else {
220 /* Either some error occured or the stream was already at EOF. */
221 return -1;
222 }
[8b5fb5e]223}
224
[1978a5f]225/**
[08053f7]226 * Read a stream until the newline (or EOF) is encountered.
[1978a5f]227 *
[08053f7]228 * @param lineptr Pointer to the output buffer in which there will be stored
229 * nul-terminated string together with the delimiter (if encountered).
230 * Will be resized if necessary.
231 * @param n Pointer to the size of the output buffer. Will be increased if
232 * necessary.
233 * @param stream Input stream.
234 * @return Number of fetched characters (including newline if encountered)
235 * or -1 on error (set in errno).
[1978a5f]236 */
[8b5fb5e]237ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
238 FILE *restrict stream)
239{
[08053f7]240 return posix_getdelim(lineptr, n, '\n', stream);
[8b5fb5e]241}
242
[4f4b4e7]243/**
[1978a5f]244 * Reopen a file stream.
[4f4b4e7]245 *
[1978a5f]246 * @param filename Pathname of a file to be reopened or NULL for changing
247 * the mode of the stream.
248 * @param mode Mode to be used for reopening the file or changing current
249 * mode of the stream.
250 * @param stream Current stream associated with the opened file.
251 * @return On success, either a stream of the reopened file or the provided
252 * stream with a changed mode. NULL otherwise.
[4f4b4e7]253 */
[46ac986]254FILE *posix_freopen(const char *restrict filename,
255 const char *restrict mode, FILE *restrict stream)
[09b0b1fb]256{
[ab547063]257 assert(mode != NULL);
258 assert(stream != NULL);
[fd4b636]259
[ab547063]260 if (filename == NULL) {
[f00af83]261 /* POSIX allows this to be imlementation-defined. HelenOS currently
262 * does not support changing the mode. */
263 // FIXME: handle mode change once it is supported
264 return stream;
[ab547063]265 }
[fd4b636]266
267 /* Open a new stream. */
[f00af83]268 FILE* new = fopen(filename, mode);
[fd4b636]269 if (new == NULL) {
270 fclose(stream);
[f00af83]271 /* errno was set by fopen() */
[ab547063]272 return NULL;
273 }
274
[fd4b636]275 /* Close the original stream without freeing it (ignoring errors). */
276 if (stream->buf != NULL) {
277 fflush(stream);
278 }
279 if (stream->sess != NULL) {
280 async_hangup(stream->sess);
281 }
282 if (stream->fd >= 0) {
283 close(stream->fd);
284 }
285 list_remove(&stream->link);
286
287 /* Move the new stream to the original location. */
288 memcpy(stream, new, sizeof (FILE));
289 free(new);
[ab547063]290
[fd4b636]291 /* Update references in the file list. */
[ab547063]292 stream->link.next->prev = &stream->link;
293 stream->link.prev->next = &stream->link;
294
295 return stream;
[09b0b1fb]296}
297
[4f4b4e7]298/**
[1978a5f]299 * Write error messages to standard error.
[4f4b4e7]300 *
[1978a5f]301 * @param s Error message.
[4f4b4e7]302 */
[09b0b1fb]303void posix_perror(const char *s)
304{
[8b5fb5e]305 if (s == NULL || s[0] == '\0') {
306 fprintf(stderr, "%s\n", posix_strerror(errno));
307 } else {
308 fprintf(stderr, "%s: %s\n", s, posix_strerror(errno));
309 }
310}
311
312struct _posix_fpos {
313 off64_t offset;
314};
315
316/** Restores stream a to position previously saved with fgetpos().
317 *
318 * @param stream Stream to restore
319 * @param pos Position to restore
320 * @return Zero on success, non-zero (with errno set) on failure
321 */
322int posix_fsetpos(FILE *stream, const posix_fpos_t *pos)
323{
324 return fseek(stream, pos->offset, SEEK_SET);
325}
326
327/** Saves the stream's position for later use by fsetpos().
328 *
329 * @param stream Stream to save
330 * @param pos Place to store the position
331 * @return Zero on success, non-zero (with errno set) on failure
332 */
333int posix_fgetpos(FILE *restrict stream, posix_fpos_t *restrict pos)
334{
335 off64_t ret = ftell(stream);
[cfbb5d18]336 if (ret != -1) {
337 pos->offset = ret;
338 return 0;
339 } else {
340 return -1;
[8b5fb5e]341 }
342}
343
344/**
[1978a5f]345 * Reposition a file-position indicator in a stream.
[8b5fb5e]346 *
[1978a5f]347 * @param stream Stream to seek in.
348 * @param offset Direction and amount of bytes to seek.
349 * @param whence From where to seek.
350 * @return Zero on success, -1 otherwise.
[8b5fb5e]351 */
352int posix_fseek(FILE *stream, long offset, int whence)
353{
354 return fseek(stream, (off64_t) offset, whence);
[09b0b1fb]355}
356
[b08ef1fd]357/**
[1978a5f]358 * Reposition a file-position indicator in a stream.
[b08ef1fd]359 *
[1978a5f]360 * @param stream Stream to seek in.
361 * @param offset Direction and amount of bytes to seek.
362 * @param whence From where to seek.
363 * @return Zero on success, -1 otherwise.
[b08ef1fd]364 */
365int posix_fseeko(FILE *stream, posix_off_t offset, int whence)
366{
[8b5fb5e]367 return fseek(stream, (off64_t) offset, whence);
368}
369
370/**
[1978a5f]371 * Discover current file offset in a stream.
[8b5fb5e]372 *
[1978a5f]373 * @param stream Stream for which the offset shall be retrieved.
374 * @return Current offset or -1 if not possible.
[8b5fb5e]375 */
376long posix_ftell(FILE *stream)
377{
378 return (long) ftell(stream);
[b08ef1fd]379}
380
381/**
[1978a5f]382 * Discover current file offset in a stream.
[b08ef1fd]383 *
[1978a5f]384 * @param stream Stream for which the offset shall be retrieved.
385 * @return Current offset or -1 if not possible.
[b08ef1fd]386 */
387posix_off_t posix_ftello(FILE *stream)
388{
[8b5fb5e]389 return (posix_off_t) ftell(stream);
390}
391
[221afc9e]392/**
393 * Discard prefetched data or write unwritten data.
394 *
395 * @param stream Stream that shall be flushed.
396 * @return Zero on success, EOF on failure.
397 */
398int posix_fflush(FILE *stream)
399{
400 int rc = fflush(stream);
401 if (rc < 0) {
402 errno = -rc;
403 return EOF;
404 } else {
405 return 0;
406 }
407}
408
[1978a5f]409/**
410 * Print formatted output to the opened file.
411 *
412 * @param fildes File descriptor of the opened file.
413 * @param format Format description.
414 * @return Either the number of printed characters or negative value on error.
415 */
[8b5fb5e]416int posix_dprintf(int fildes, const char *restrict format, ...)
417{
418 va_list list;
419 va_start(list, format);
420 int result = posix_vdprintf(fildes, format, list);
421 va_end(list);
422 return result;
423}
424
[1978a5f]425/**
426 * Write ordinary string to the opened file.
427 *
428 * @param str String to be written.
429 * @param size Size of the string (in bytes)..
430 * @param fd File descriptor of the opened file.
431 * @return The number of written characters.
432 */
[8b5fb5e]433static int _dprintf_str_write(const char *str, size_t size, void *fd)
434{
435 ssize_t wr = write(*(int *) fd, str, size);
436 return str_nlength(str, wr);
437}
438
[1978a5f]439/**
440 * Write wide string to the opened file.
441 *
442 * @param str String to be written.
443 * @param size Size of the string (in bytes).
444 * @param fd File descriptor of the opened file.
445 * @return The number of written characters.
446 */
[8b5fb5e]447static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
448{
449 size_t offset = 0;
450 size_t chars = 0;
451 size_t sz;
452 char buf[4];
453
454 while (offset < size) {
455 sz = 0;
456 if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) {
457 break;
458 }
459
460 if (write(*(int *) fd, buf, sz) != (ssize_t) sz) {
461 break;
462 }
463
464 chars++;
465 offset += sizeof(wchar_t);
466 }
467
468 return chars;
469}
470
[1978a5f]471/**
472 * Print formatted output to the opened file.
473 *
474 * @param fildes File descriptor of the opened file.
475 * @param format Format description.
476 * @param ap Print arguments.
477 * @return Either the number of printed characters or negative value on error.
478 */
[8b5fb5e]479int posix_vdprintf(int fildes, const char *restrict format, va_list ap)
480{
481 printf_spec_t spec = {
482 .str_write = _dprintf_str_write,
483 .wstr_write = _dprintf_wstr_write,
484 .data = &fildes
485 };
486
487 return printf_core(format, &spec, ap);
[b08ef1fd]488}
489
[59f799b]490/**
[1978a5f]491 * Print formatted output to the string.
[59f799b]492 *
[1978a5f]493 * @param s Output string.
494 * @param format Format description.
495 * @return Either the number of printed characters (excluding null byte) or
496 * negative value on error.
[59f799b]497 */
[08053f7]498int posix_sprintf(char *s, const char *restrict format, ...)
[59f799b]499{
[8b5fb5e]500 va_list list;
501 va_start(list, format);
502 int result = posix_vsprintf(s, format, list);
503 va_end(list);
504 return result;
[59f799b]505}
506
[823a929]507/**
[1978a5f]508 * Print formatted output to the string.
[823a929]509 *
[1978a5f]510 * @param s Output string.
511 * @param format Format description.
512 * @param ap Print arguments.
513 * @return Either the number of printed characters (excluding null byte) or
514 * negative value on error.
[823a929]515 */
[08053f7]516int posix_vsprintf(char *s, const char *restrict format, va_list ap)
[823a929]517{
[8b5fb5e]518 return vsnprintf(s, STR_NO_LIMIT, format, ap);
519}
520
521/**
[1978a5f]522 * Convert formatted input from the stream.
[8b5fb5e]523 *
[1978a5f]524 * @param stream Input stream.
525 * @param format Format description.
526 * @return The number of converted output items or EOF on failure.
[8b5fb5e]527 */
528int posix_fscanf(FILE *restrict stream, const char *restrict format, ...)
529{
530 va_list list;
531 va_start(list, format);
532 int result = posix_vfscanf(stream, format, list);
533 va_end(list);
534 return result;
535}
536
537/**
[1978a5f]538 * Convert formatted input from the standard input.
[8b5fb5e]539 *
[1978a5f]540 * @param format Format description.
541 * @return The number of converted output items or EOF on failure.
[8b5fb5e]542 */
543int posix_scanf(const char *restrict format, ...)
544{
545 va_list list;
546 va_start(list, format);
547 int result = posix_vscanf(format, list);
548 va_end(list);
549 return result;
550}
551
552/**
[1978a5f]553 * Convert formatted input from the standard input.
[8b5fb5e]554 *
[1978a5f]555 * @param format Format description.
556 * @param arg Output items.
557 * @return The number of converted output items or EOF on failure.
[8b5fb5e]558 */
559int posix_vscanf(const char *restrict format, va_list arg)
560{
561 return posix_vfscanf(stdin, format, arg);
562}
563
[59f799b]564/**
[1978a5f]565 * Convert formatted input from the string.
[59f799b]566 *
[1978a5f]567 * @param s Input string.
568 * @param format Format description.
569 * @return The number of converted output items or EOF on failure.
[59f799b]570 */
[08053f7]571int posix_sscanf(const char *restrict s, const char *restrict format, ...)
[8b5fb5e]572{
573 va_list list;
574 va_start(list, format);
575 int result = posix_vsscanf(s, format, list);
576 va_end(list);
577 return result;
578}
579
[1978a5f]580/**
581 * Acquire file stream for the thread.
582 *
583 * @param file File stream to lock.
584 */
[8b5fb5e]585void posix_flockfile(FILE *file)
586{
587 /* dummy */
588}
589
[1978a5f]590/**
591 * Acquire file stream for the thread (non-blocking).
592 *
593 * @param file File stream to lock.
594 * @return Zero for success and non-zero if the lock cannot be acquired.
595 */
[8b5fb5e]596int posix_ftrylockfile(FILE *file)
597{
598 /* dummy */
599 return 0;
600}
601
[1978a5f]602/**
603 * Relinquish the ownership of the locked file stream.
604 *
605 * @param file File stream to unlock.
606 */
[8b5fb5e]607void posix_funlockfile(FILE *file)
608{
609 /* dummy */
610}
611
[1978a5f]612/**
613 * Get a byte from a stream (thread-unsafe).
614 *
615 * @param stream Input file stream.
616 * @return Either read byte or EOF.
617 */
[8b5fb5e]618int posix_getc_unlocked(FILE *stream)
619{
620 return getc(stream);
621}
622
[1978a5f]623/**
624 * Get a byte from the standard input stream (thread-unsafe).
625 *
626 * @return Either read byte or EOF.
627 */
[8b5fb5e]628int posix_getchar_unlocked(void)
629{
630 return getchar();
631}
632
[1978a5f]633/**
634 * Put a byte on a stream (thread-unsafe).
635 *
636 * @param c Byte to output.
637 * @param stream Output file stream.
638 * @return Either written byte or EOF.
639 */
[8b5fb5e]640int posix_putc_unlocked(int c, FILE *stream)
641{
642 return putc(c, stream);
643}
644
[1978a5f]645/**
646 * Put a byte on the standard output stream (thread-unsafe).
647 *
648 * @param c Byte to output.
649 * @return Either written byte or EOF.
650 */
[8b5fb5e]651int posix_putchar_unlocked(int c)
652{
653 return putchar(c);
654}
655
[823a929]656/**
[58115ae]657 * Remove a file or directory.
[823a929]658 *
[1978a5f]659 * @param path Pathname of the file that shall be removed.
[58115ae]660 * @return Zero on success, -1 (with errno set) otherwise.
[823a929]661 */
662int posix_remove(const char *path)
663{
[58115ae]664 struct stat st;
665 int rc = stat(path, &st);
666
667 if (rc != EOK) {
668 errno = -rc;
669 return -1;
670 }
671
672 if (st.is_directory) {
673 rc = rmdir(path);
674 } else {
675 rc = unlink(path);
676 }
677
678 if (rc != EOK) {
679 errno = -rc;
680 return -1;
681 }
682 return 0;
[823a929]683}
684
[75406dc]685/**
686 * Rename a file or directory.
687 *
[2a53f71]688 * @param old Old pathname.
689 * @param new New pathname.
[75406dc]690 * @return Zero on success, -1 (with errno set) otherwise.
691 */
692int posix_rename(const char *old, const char *new)
693{
694 return errnify(rename, old, new);
695}
696
[823a929]697/**
[11544f4]698 * Get a unique temporary file name (obsolete).
699 *
700 * @param s Buffer for the file name. Must be at least L_tmpnam bytes long.
701 * @return The value of s on success, NULL on failure.
[823a929]702 */
703char *posix_tmpnam(char *s)
704{
[11544f4]705 assert(L_tmpnam >= posix_strlen("/tmp/tnXXXXXX"));
706
707 static char buffer[L_tmpnam + 1];
708 if (s == NULL) {
709 s = buffer;
710 }
711
712 posix_strcpy(s, "/tmp/tnXXXXXX");
713 posix_mktemp(s);
714
715 if (*s == '\0') {
716 /* Errno set by mktemp(). */
717 return NULL;
718 }
719
720 return s;
721}
722
723/**
724 * Get an unique temporary file name with additional constraints (obsolete).
725 *
726 * @param dir Path to directory, where the file should be created.
727 * @param pfx Optional prefix up to 5 characters long.
728 * @return Newly allocated unique path for temporary file. NULL on failure.
729 */
730char *posix_tempnam(const char *dir, const char *pfx)
731{
732 /* Sequence number of the filename. */
733 static int seq = 0;
734
735 size_t dir_len = posix_strlen(dir);
736 if (dir[dir_len - 1] == '/') {
737 dir_len--;
738 }
739
740 size_t pfx_len = posix_strlen(pfx);
741 if (pfx_len > 5) {
742 pfx_len = 5;
743 }
744
745 char *result = malloc(dir_len + /* slash*/ 1 +
746 pfx_len + /* three-digit seq */ 3 + /* .tmp */ 4 + /* nul */ 1);
747
748 if (result == NULL) {
749 errno = ENOMEM;
750 return NULL;
751 }
752
753 char *res_ptr = result;
754 posix_strncpy(res_ptr, dir, dir_len);
755 res_ptr += dir_len;
756 posix_strncpy(res_ptr, pfx, pfx_len);
757 res_ptr += pfx_len;
758
759 for (; seq < 1000; ++seq) {
760 snprintf(res_ptr, 8, "%03d.tmp", seq);
761
762 int orig_errno = errno;
763 errno = 0;
764 /* Check if the file exists. */
765 if (posix_access(result, F_OK) == -1) {
766 if (errno == ENOENT) {
767 errno = orig_errno;
768 break;
769 } else {
770 /* errno set by access() */
771 return NULL;
772 }
773 }
774 }
775
776 if (seq == 1000) {
777 free(result);
778 errno = EINVAL;
779 return NULL;
780 }
781
782 return result;
783}
784
785/**
786 * Create and open an unique temporary file.
787 * The file is automatically removed when the stream is closed.
788 *
789 * @param dir Path to directory, where the file should be created.
790 * @param pfx Optional prefix up to 5 characters long.
791 * @return Newly allocated unique path for temporary file. NULL on failure.
792 */
793FILE *posix_tmpfile(void)
794{
795 char filename[] = "/tmp/tfXXXXXX";
796 int fd = posix_mkstemp(filename);
797 if (fd == -1) {
798 /* errno set by mkstemp(). */
799 return NULL;
800 }
801
802 /* Unlink the created file, so that it's removed on close(). */
803 posix_unlink(filename);
804 return fdopen(fd, "w+");
[823a929]805}
806
[09b0b1fb]807/** @}
808 */
Note: See TracBrowser for help on using the repository browser.