1*2b8a0536Sjoerg /* $NetBSD: tmp.c,v 1.16 2009/11/06 18:34:22 joerg Exp $ */
2f84513a7Sjdolecek
3f84513a7Sjdolecek /*-
4f84513a7Sjdolecek * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
5f84513a7Sjdolecek * All rights reserved.
6f84513a7Sjdolecek *
7f84513a7Sjdolecek * This code is derived from software contributed to The NetBSD Foundation
8f84513a7Sjdolecek * by Ben Harris and Jaromir Dolecek.
9f84513a7Sjdolecek *
10f84513a7Sjdolecek * Redistribution and use in source and binary forms, with or without
11f84513a7Sjdolecek * modification, are permitted provided that the following conditions
12f84513a7Sjdolecek * are met:
13f84513a7Sjdolecek * 1. Redistributions of source code must retain the above copyright
14f84513a7Sjdolecek * notice, this list of conditions and the following disclaimer.
15f84513a7Sjdolecek * 2. Redistributions in binary form must reproduce the above copyright
16f84513a7Sjdolecek * notice, this list of conditions and the following disclaimer in the
17f84513a7Sjdolecek * documentation and/or other materials provided with the distribution.
18f84513a7Sjdolecek *
19f84513a7Sjdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f84513a7Sjdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f84513a7Sjdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f84513a7Sjdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f84513a7Sjdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f84513a7Sjdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f84513a7Sjdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f84513a7Sjdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f84513a7Sjdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f84513a7Sjdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f84513a7Sjdolecek * POSSIBILITY OF SUCH DAMAGE.
30f84513a7Sjdolecek */
315347005eSjdolecek
325347005eSjdolecek /*-
335347005eSjdolecek * Copyright (c) 1993
345347005eSjdolecek * The Regents of the University of California. All rights reserved.
355347005eSjdolecek *
365347005eSjdolecek * This code is derived from software contributed to Berkeley by
375347005eSjdolecek * Peter McIlroy.
385347005eSjdolecek *
395347005eSjdolecek * Redistribution and use in source and binary forms, with or without
405347005eSjdolecek * modification, are permitted provided that the following conditions
415347005eSjdolecek * are met:
425347005eSjdolecek * 1. Redistributions of source code must retain the above copyright
435347005eSjdolecek * notice, this list of conditions and the following disclaimer.
445347005eSjdolecek * 2. Redistributions in binary form must reproduce the above copyright
455347005eSjdolecek * notice, this list of conditions and the following disclaimer in the
465347005eSjdolecek * documentation and/or other materials provided with the distribution.
4789aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
485347005eSjdolecek * may be used to endorse or promote products derived from this software
495347005eSjdolecek * without specific prior written permission.
505347005eSjdolecek *
515347005eSjdolecek * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
525347005eSjdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
535347005eSjdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
545347005eSjdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
555347005eSjdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
565347005eSjdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
575347005eSjdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
585347005eSjdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
595347005eSjdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
605347005eSjdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
615347005eSjdolecek * SUCH DAMAGE.
625347005eSjdolecek */
635347005eSjdolecek
64f2799c52Shubertf #include <sys/cdefs.h>
655347005eSjdolecek
66*2b8a0536Sjoerg __RCSID("$NetBSD: tmp.c,v 1.16 2009/11/06 18:34:22 joerg Exp $");
675347005eSjdolecek
685347005eSjdolecek #include <sys/param.h>
695347005eSjdolecek
705347005eSjdolecek #include <err.h>
715347005eSjdolecek #include <errno.h>
725347005eSjdolecek #include <limits.h>
735347005eSjdolecek #include <signal.h>
745347005eSjdolecek #include <stdio.h>
755347005eSjdolecek #include <stdlib.h>
765347005eSjdolecek #include <string.h>
775347005eSjdolecek #include <unistd.h>
785347005eSjdolecek
795347005eSjdolecek #include "sort.h"
805347005eSjdolecek #include "pathnames.h"
815347005eSjdolecek
825347005eSjdolecek #define _NAME_TMP "sort.XXXXXXXX"
835347005eSjdolecek
845347005eSjdolecek FILE *
ftmp(void)852a0ab276Sdsl ftmp(void)
865347005eSjdolecek {
875347005eSjdolecek sigset_t set, oset;
885347005eSjdolecek FILE *fp;
895347005eSjdolecek int fd;
90b1eda372Sjdolecek char path[MAXPATHLEN];
915347005eSjdolecek
92b1eda372Sjdolecek (void)snprintf(path, sizeof(path), "%s%s%s", tmpdir,
935347005eSjdolecek (tmpdir[strlen(tmpdir)-1] != '/') ? "/" : "", _NAME_TMP);
945347005eSjdolecek
955347005eSjdolecek sigfillset(&set);
965347005eSjdolecek (void)sigprocmask(SIG_BLOCK, &set, &oset);
975347005eSjdolecek if ((fd = mkstemp(path)) < 0)
98d9068053Sjdolecek err(2, "ftmp: mkstemp(\"%s\")", path);
995347005eSjdolecek if (!(fp = fdopen(fd, "w+")))
100d9068053Sjdolecek err(2, "ftmp: fdopen(\"%s\")", path);
1016458ae9cSdsl if (!DEBUG('t'))
1025347005eSjdolecek (void)unlink(path);
1035347005eSjdolecek
1045347005eSjdolecek (void)sigprocmask(SIG_SETMASK, &oset, NULL);
1055347005eSjdolecek return (fp);
1065347005eSjdolecek }
107