2 * libcompat - system compatibility library
4 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
5 * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 #include <sys/types.h>
32 vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list args)
34 static FILE *file = NULL;
35 static pid_t file_pid;
40 if (maxsize != 0 && buf == NULL)
43 /* Avoid race conditions from children after a fork(2). */
44 if (file_pid > 0 && file_pid != getpid()) {
55 if (fseek(file, 0, 0))
57 if (ftruncate(fileno(file), 0))
61 total = vfprintf(file, fmt, args);
66 if (total >= (int)maxsize)
72 if (fseek(file, 0, SEEK_SET))
75 nr = fread(buf, 1, want, file);