]> git.ipfire.org Git - thirdparty/glibc.git/blame - support/temp_file.c
Assume that accept4 is always available and works
[thirdparty/glibc.git] / support / temp_file.c
CommitLineData
c23de0aa 1/* Temporary file handling for tests.
bfff8b1b 2 Copyright (C) 1998-2017 Free Software Foundation, Inc.
c23de0aa
FW
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19/* This is required to get an mkstemp which can create large files on
20 some 32-bit platforms. */
21#define _FILE_OFFSET_BITS 64
22
23#include <support/temp_file.h>
24#include <support/temp_file-internal.h>
25#include <support/support.h>
26
27#include <paths.h>
28#include <search.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33/* List of temporary files. */
34static struct temp_name_list
35{
36 struct qelem q;
37 char *name;
38} *temp_name_list;
39
40/* Location of the temporary files. Set by the test skeleton via
41 support_set_test_dir. The string is not be freed. */
42static const char *test_dir = _PATH_TMP;
43
44void
45add_temp_file (const char *name)
46{
47 struct temp_name_list *newp
48 = (struct temp_name_list *) xcalloc (sizeof (*newp), 1);
49 char *newname = strdup (name);
50 if (newname != NULL)
51 {
52 newp->name = newname;
53 if (temp_name_list == NULL)
54 temp_name_list = (struct temp_name_list *) &newp->q;
55 else
56 insque (newp, temp_name_list);
57 }
58 else
59 free (newp);
60}
61
62int
63create_temp_file (const char *base, char **filename)
64{
65 char *fname;
66 int fd;
67
68 fname = (char *) xmalloc (strlen (test_dir) + 1 + strlen (base)
69 + sizeof ("XXXXXX"));
70 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
71
72 fd = mkstemp (fname);
73 if (fd == -1)
74 {
75 printf ("cannot open temporary file '%s': %m\n", fname);
76 free (fname);
77 return -1;
78 }
79
80 add_temp_file (fname);
81 if (filename != NULL)
82 *filename = fname;
83 else
84 free (fname);
85
86 return fd;
87}
88
89/* Helper functions called by the test skeleton follow. */
90
91void
92support_set_test_dir (const char *path)
93{
94 test_dir = path;
95}
96
97void
98support_delete_temp_files (void)
99{
100 while (temp_name_list != NULL)
101 {
2bda2d82
FW
102 /* For some tests, the temporary file removal runs multiple
103 times (in the parent processes and the subprocess), so do not
104 report a failed removal attempt. */
105 (void) remove (temp_name_list->name);
c23de0aa
FW
106 free (temp_name_list->name);
107
108 struct temp_name_list *next
109 = (struct temp_name_list *) temp_name_list->q.q_forw;
110 free (temp_name_list);
111 temp_name_list = next;
112 }
113}
114
115void
116support_print_temp_files (FILE *f)
117{
118 if (temp_name_list != NULL)
119 {
120 struct temp_name_list *n;
121 fprintf (f, "temp_files=(\n");
122 for (n = temp_name_list;
123 n != NULL;
124 n = (struct temp_name_list *) n->q.q_forw)
125 fprintf (f, " '%s'\n", n->name);
126 fprintf (f, ")\n");
127 }
128}