1248849Sfanf/* 2279639Shselasky * Copyright (c) 2012 - 2013 Tony Finch <dot@dotat.at> 3248849Sfanf * 4248849Sfanf * Redistribution and use in source and binary forms, with or without 5248849Sfanf * modification, are permitted provided that the following conditions 6248849Sfanf * are met: 7248849Sfanf * 1. Redistributions of source code must retain the above copyright 8248849Sfanf * notice, this list of conditions and the following disclaimer. 9248849Sfanf * 2. Redistributions in binary form must reproduce the above copyright 10248849Sfanf * notice, this list of conditions and the following disclaimer in the 11248849Sfanf * documentation and/or other materials provided with the distribution. 12248849Sfanf * 13248849Sfanf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14248849Sfanf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15248849Sfanf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16248849Sfanf * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17248849Sfanf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18248849Sfanf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19248849Sfanf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20248849Sfanf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21248849Sfanf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22248849Sfanf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23248849Sfanf * SUCH DAMAGE. 24248849Sfanf * 25248849Sfanf * $FreeBSD$ 26248849Sfanf */ 27248849Sfanf 28248849Sfanf#include <sys/stat.h> 29248849Sfanf 30248849Sfanf#include <ctype.h> 31248849Sfanf#include <err.h> 32248849Sfanf#include <stdarg.h> 33248849Sfanf#include <stdbool.h> 34248849Sfanf#include <stdio.h> 35248849Sfanf#include <stdlib.h> 36248849Sfanf#include <string.h> 37248849Sfanf#include <unistd.h> 38248849Sfanf 39248849Sfanf/* portabiity stubs */ 40248849Sfanf 41248849Sfanf#define fbinmode(fp) (fp) 42248849Sfanf 43279639Shselasky#define replace(old,new) rename(old,new) 44279639Shselasky 45248849Sfanfstatic FILE * 46248849Sfanfmktempmode(char *tmp, int mode) 47248849Sfanf{ 48248849Sfanf int fd = mkstemp(tmp); 49248849Sfanf if (fd < 0) return (NULL); 50248849Sfanf fchmod(fd, mode & (S_IRWXU|S_IRWXG|S_IRWXO)); 51248849Sfanf return (fdopen(fd, "wb")); 52248849Sfanf} 53