1 /* public domain, no copyright claimed */
2
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9
fmt_open(int flags,mode_t mode,const char * fmt,...)10 int fmt_open(int flags, mode_t mode, const char* fmt, ...)
11 {
12 int rv = -1;
13
14 unsigned cc;
15 va_list args;
16 va_start( args, fmt );
17 cc = vsnprintf(NULL, 0, fmt, args );
18 va_end( args);
19
20 char* dbuf;
21 if (cc > 0 && (dbuf = (char*) malloc(cc + 1)) ) {
22 va_start(args, fmt);
23 vsnprintf(dbuf, cc+1, fmt, args);
24 va_end(args);
25
26 rv = open(dbuf, flags, mode);
27 free(dbuf);
28 }
29
30 #ifndef _WIN32
31 /* don't let spawned children have access to this one */
32 if (-1 != rv)
33 fcntl(rv, FD_CLOEXEC);
34 #endif
35
36 return rv;
37 }
38