libite 2.6.1
fopenf.c
Go to the documentation of this file.
1/* Formatted fopen()
2 *
3 * Copyright (c) 2021 Joachim Wiberg <troglobit@gmail.com>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
24
25#include <errno.h>
26#include <stdio.h>
27#include <stdarg.h>
28#include <stdlib.h>
29
40FILE *vfopenf(const char *mode, const char *fmt, va_list ap)
41{
42 va_list apc;
43 char *file;
44 int len;
45
46 va_copy(apc, ap);
47 len = vsnprintf(NULL, 0, fmt, apc);
48 va_end(apc);
49
50 file = alloca(len + 1);
51 if (!file) {
52 errno = ENOMEM;
53 return NULL;
54 }
55
56 va_copy(apc, ap);
57 vsnprintf(file, len + 1, fmt, apc);
58 va_end(apc);
59
60 return fopen(file, mode);
61}
62
75FILE *fopenf(const char *mode, const char *fmt, ...)
76{
77 va_list ap;
78 FILE *fp;
79
80 va_start(ap, fmt);
81 fp = vfopenf(mode, fmt, ap);
82 va_end(ap);
83 if (!fp)
84 return NULL;
85
86 return fp;
87}
88
FILE * fopenf(const char *mode, const char *fmt,...)
Definition fopenf.c:75
FILE * vfopenf(const char *mode, const char *fmt, va_list ap)
Definition fopenf.c:40