1fb3ef04dSDag-Erling Smørgrav /*
2fb3ef04dSDag-Erling Smørgrav * Copyright (c) 2012 - 2013 Tony Finch <dot@dotat.at>
3fb3ef04dSDag-Erling Smørgrav *
4fb3ef04dSDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without
5fb3ef04dSDag-Erling Smørgrav * modification, are permitted provided that the following conditions
6fb3ef04dSDag-Erling Smørgrav * are met:
7fb3ef04dSDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright
8fb3ef04dSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer.
9fb3ef04dSDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright
10fb3ef04dSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the
11fb3ef04dSDag-Erling Smørgrav * documentation and/or other materials provided with the distribution.
12fb3ef04dSDag-Erling Smørgrav *
13fb3ef04dSDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14fb3ef04dSDag-Erling Smørgrav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15fb3ef04dSDag-Erling Smørgrav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16fb3ef04dSDag-Erling Smørgrav * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17fb3ef04dSDag-Erling Smørgrav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18fb3ef04dSDag-Erling Smørgrav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19fb3ef04dSDag-Erling Smørgrav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20fb3ef04dSDag-Erling Smørgrav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21fb3ef04dSDag-Erling Smørgrav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22fb3ef04dSDag-Erling Smørgrav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23fb3ef04dSDag-Erling Smørgrav * SUCH DAMAGE.
24fb3ef04dSDag-Erling Smørgrav */
25fb3ef04dSDag-Erling Smørgrav
26*343b776fSDag-Erling Smørgrav #include <sys/param.h>
27fb3ef04dSDag-Erling Smørgrav #include <sys/stat.h>
28*343b776fSDag-Erling Smørgrav #include <sys/tree.h>
29fb3ef04dSDag-Erling Smørgrav
30*343b776fSDag-Erling Smørgrav #include <assert.h>
31fb3ef04dSDag-Erling Smørgrav #include <ctype.h>
32fb3ef04dSDag-Erling Smørgrav #include <err.h>
33fb3ef04dSDag-Erling Smørgrav #include <stdarg.h>
34fb3ef04dSDag-Erling Smørgrav #include <stdbool.h>
35fb3ef04dSDag-Erling Smørgrav #include <stdio.h>
36fb3ef04dSDag-Erling Smørgrav #include <stdlib.h>
37fb3ef04dSDag-Erling Smørgrav #include <string.h>
38fb3ef04dSDag-Erling Smørgrav #include <unistd.h>
39fb3ef04dSDag-Erling Smørgrav
40fb3ef04dSDag-Erling Smørgrav /* portability stubs */
41fb3ef04dSDag-Erling Smørgrav
42fb3ef04dSDag-Erling Smørgrav #define fbinmode(fp) (fp)
43fb3ef04dSDag-Erling Smørgrav
44fb3ef04dSDag-Erling Smørgrav #define replace(old,new) rename(old,new)
45fb3ef04dSDag-Erling Smørgrav
46fb3ef04dSDag-Erling Smørgrav static FILE *
mktempmode(char * tmp,int mode)47fb3ef04dSDag-Erling Smørgrav mktempmode(char *tmp, int mode)
48fb3ef04dSDag-Erling Smørgrav {
49fb3ef04dSDag-Erling Smørgrav int fd = mkstemp(tmp);
50fb3ef04dSDag-Erling Smørgrav if (fd < 0) return (NULL);
51fb3ef04dSDag-Erling Smørgrav fchmod(fd, mode & (S_IRWXU|S_IRWXG|S_IRWXO));
52fb3ef04dSDag-Erling Smørgrav return (fdopen(fd, "wb"));
53fb3ef04dSDag-Erling Smørgrav }
54