Use __floor not floor in sinf.
[glibc.git] / support / temp_file.c
blob547263a3e4ba11d82de3a99ef752c7db9ad9b367
1 /* Temporary file handling for tests.
2 Copyright (C) 1998-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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
23 #include <support/temp_file.h>
24 #include <support/temp_file-internal.h>
25 #include <support/support.h>
27 #include <paths.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 /* List of temporary files. */
34 static struct temp_name_list
36 struct temp_name_list *next;
37 char *name;
38 pid_t owner;
39 } *temp_name_list;
41 /* Location of the temporary files. Set by the test skeleton via
42 support_set_test_dir. The string is not be freed. */
43 static const char *test_dir = _PATH_TMP;
45 void
46 add_temp_file (const char *name)
48 struct temp_name_list *newp
49 = (struct temp_name_list *) xcalloc (sizeof (*newp), 1);
50 char *newname = strdup (name);
51 if (newname != NULL)
53 newp->name = newname;
54 newp->next = temp_name_list;
55 newp->owner = getpid ();
56 temp_name_list = newp;
58 else
59 free (newp);
62 int
63 create_temp_file (const char *base, char **filename)
65 char *fname;
66 int fd;
68 fname = (char *) xmalloc (strlen (test_dir) + 1 + strlen (base)
69 + sizeof ("XXXXXX"));
70 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
72 fd = mkstemp (fname);
73 if (fd == -1)
75 printf ("cannot open temporary file '%s': %m\n", fname);
76 free (fname);
77 return -1;
80 add_temp_file (fname);
81 if (filename != NULL)
82 *filename = fname;
83 else
84 free (fname);
86 return fd;
89 char *
90 support_create_temp_directory (const char *base)
92 char *path = xasprintf ("%s/%sXXXXXX", test_dir, base);
93 if (mkdtemp (path) == NULL)
95 printf ("error: mkdtemp (\"%s\"): %m", path);
96 exit (1);
98 add_temp_file (path);
99 return path;
102 /* Helper functions called by the test skeleton follow. */
104 void
105 support_set_test_dir (const char *path)
107 test_dir = path;
110 void
111 support_delete_temp_files (void)
113 pid_t pid = getpid ();
114 while (temp_name_list != NULL)
116 /* Only perform the removal if the path was registed in the same
117 process, as identified by the PID. (This assumes that the
118 parent process which registered the temporary file sticks
119 around, to prevent PID reuse.) */
120 if (temp_name_list->owner == pid)
122 if (remove (temp_name_list->name) != 0)
123 printf ("warning: could not remove temporary file: %s: %m\n",
124 temp_name_list->name);
126 free (temp_name_list->name);
128 struct temp_name_list *next = temp_name_list->next;
129 free (temp_name_list);
130 temp_name_list = next;
134 void
135 support_print_temp_files (FILE *f)
137 if (temp_name_list != NULL)
139 struct temp_name_list *n;
140 fprintf (f, "temp_files=(\n");
141 for (n = temp_name_list; n != NULL; n = n->next)
142 fprintf (f, " '%s'\n", n->name);
143 fprintf (f, ")\n");