xref: /minix3/usr.bin/sort/tmp.c (revision 0fbbaa43e914d38ef3af549125d31574117d1ebf)
1*0fbbaa43SLionel Sambuc /*	$NetBSD: tmp.c,v 1.16 2009/11/06 18:34:22 joerg Exp $	*/
2*0fbbaa43SLionel Sambuc 
3*0fbbaa43SLionel Sambuc /*-
4*0fbbaa43SLionel Sambuc  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
5*0fbbaa43SLionel Sambuc  * All rights reserved.
6*0fbbaa43SLionel Sambuc  *
7*0fbbaa43SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8*0fbbaa43SLionel Sambuc  * by Ben Harris and Jaromir Dolecek.
9*0fbbaa43SLionel Sambuc  *
10*0fbbaa43SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*0fbbaa43SLionel Sambuc  * modification, are permitted provided that the following conditions
12*0fbbaa43SLionel Sambuc  * are met:
13*0fbbaa43SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*0fbbaa43SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*0fbbaa43SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*0fbbaa43SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*0fbbaa43SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*0fbbaa43SLionel Sambuc  *
19*0fbbaa43SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*0fbbaa43SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*0fbbaa43SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*0fbbaa43SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*0fbbaa43SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*0fbbaa43SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*0fbbaa43SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*0fbbaa43SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*0fbbaa43SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*0fbbaa43SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*0fbbaa43SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30*0fbbaa43SLionel Sambuc  */
31*0fbbaa43SLionel Sambuc 
32*0fbbaa43SLionel Sambuc /*-
33*0fbbaa43SLionel Sambuc  * Copyright (c) 1993
34*0fbbaa43SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
35*0fbbaa43SLionel Sambuc  *
36*0fbbaa43SLionel Sambuc  * This code is derived from software contributed to Berkeley by
37*0fbbaa43SLionel Sambuc  * Peter McIlroy.
38*0fbbaa43SLionel Sambuc  *
39*0fbbaa43SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
40*0fbbaa43SLionel Sambuc  * modification, are permitted provided that the following conditions
41*0fbbaa43SLionel Sambuc  * are met:
42*0fbbaa43SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
43*0fbbaa43SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
44*0fbbaa43SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
45*0fbbaa43SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
46*0fbbaa43SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
47*0fbbaa43SLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
48*0fbbaa43SLionel Sambuc  *    may be used to endorse or promote products derived from this software
49*0fbbaa43SLionel Sambuc  *    without specific prior written permission.
50*0fbbaa43SLionel Sambuc  *
51*0fbbaa43SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52*0fbbaa43SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53*0fbbaa43SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54*0fbbaa43SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55*0fbbaa43SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56*0fbbaa43SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57*0fbbaa43SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58*0fbbaa43SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59*0fbbaa43SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60*0fbbaa43SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61*0fbbaa43SLionel Sambuc  * SUCH DAMAGE.
62*0fbbaa43SLionel Sambuc  */
63*0fbbaa43SLionel Sambuc 
64*0fbbaa43SLionel Sambuc #include <sys/cdefs.h>
65*0fbbaa43SLionel Sambuc 
66*0fbbaa43SLionel Sambuc __RCSID("$NetBSD: tmp.c,v 1.16 2009/11/06 18:34:22 joerg Exp $");
67*0fbbaa43SLionel Sambuc 
68*0fbbaa43SLionel Sambuc #include <sys/param.h>
69*0fbbaa43SLionel Sambuc 
70*0fbbaa43SLionel Sambuc #include <err.h>
71*0fbbaa43SLionel Sambuc #include <errno.h>
72*0fbbaa43SLionel Sambuc #include <limits.h>
73*0fbbaa43SLionel Sambuc #include <signal.h>
74*0fbbaa43SLionel Sambuc #include <stdio.h>
75*0fbbaa43SLionel Sambuc #include <stdlib.h>
76*0fbbaa43SLionel Sambuc #include <string.h>
77*0fbbaa43SLionel Sambuc #include <unistd.h>
78*0fbbaa43SLionel Sambuc 
79*0fbbaa43SLionel Sambuc #include "sort.h"
80*0fbbaa43SLionel Sambuc #include "pathnames.h"
81*0fbbaa43SLionel Sambuc 
82*0fbbaa43SLionel Sambuc #define _NAME_TMP "sort.XXXXXXXX"
83*0fbbaa43SLionel Sambuc 
84*0fbbaa43SLionel Sambuc FILE *
ftmp(void)85*0fbbaa43SLionel Sambuc ftmp(void)
86*0fbbaa43SLionel Sambuc {
87*0fbbaa43SLionel Sambuc 	sigset_t set, oset;
88*0fbbaa43SLionel Sambuc 	FILE *fp;
89*0fbbaa43SLionel Sambuc 	int fd;
90*0fbbaa43SLionel Sambuc 	char path[MAXPATHLEN];
91*0fbbaa43SLionel Sambuc 
92*0fbbaa43SLionel Sambuc 	(void)snprintf(path, sizeof(path), "%s%s%s", tmpdir,
93*0fbbaa43SLionel Sambuc 		       (tmpdir[strlen(tmpdir)-1] != '/') ? "/" : "", _NAME_TMP);
94*0fbbaa43SLionel Sambuc 
95*0fbbaa43SLionel Sambuc 	sigfillset(&set);
96*0fbbaa43SLionel Sambuc 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
97*0fbbaa43SLionel Sambuc 	if ((fd = mkstemp(path)) < 0)
98*0fbbaa43SLionel Sambuc 		err(2, "ftmp: mkstemp(\"%s\")", path);
99*0fbbaa43SLionel Sambuc 	if (!(fp = fdopen(fd, "w+")))
100*0fbbaa43SLionel Sambuc 		err(2, "ftmp: fdopen(\"%s\")", path);
101*0fbbaa43SLionel Sambuc 	if (!DEBUG('t'))
102*0fbbaa43SLionel Sambuc 		(void)unlink(path);
103*0fbbaa43SLionel Sambuc 
104*0fbbaa43SLionel Sambuc 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
105*0fbbaa43SLionel Sambuc 	return (fp);
106*0fbbaa43SLionel Sambuc }
107