Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / tmpnam.c
blobdc04cf3f6cd470bfa2a1679e2bc0aca7c64ca910
1 /*
2 * tmpname.c
3 * Original Author: G. Haley
4 */
5 /*
6 FUNCTION
7 <<tmpnam>>, <<tempnam>>---name for a temporary file
9 INDEX
10 tmpnam
11 INDEX
12 tempnam
13 INDEX
14 _tmpnam_r
15 INDEX
16 _tempnam_r
18 SYNOPSIS
19 #include <stdio.h>
20 char *tmpnam(char *<[s]>);
21 char *tempnam(char *<[dir]>, char *<[pfx]>);
22 char *_tmpnam_r(struct _reent *<[reent]>, char *<[s]>);
23 char *_tempnam_r(struct _reent *<[reent]>, char *<[dir]>, char *<[pfx]>);
25 DESCRIPTION
26 Use either of these functions to generate a name for a temporary file.
27 The generated name is guaranteed to avoid collision with other files
28 (for up to <<TMP_MAX>> calls of either function).
30 <<tmpnam>> generates file names with the value of <<P_tmpdir>>
31 (defined in `<<stdio.h>>') as the leading directory component of the path.
33 You can use the <<tmpnam>> argument <[s]> to specify a suitable area
34 of memory for the generated filename; otherwise, you can call
35 <<tmpnam(NULL)>> to use an internal static buffer.
37 <<tempnam>> allows you more control over the generated filename: you
38 can use the argument <[dir]> to specify the path to a directory for
39 temporary files, and you can use the argument <[pfx]> to specify a
40 prefix for the base filename.
42 If <[dir]> is <<NULL>>, <<tempnam>> will attempt to use the value of
43 environment variable <<TMPDIR>> instead; if there is no such value,
44 <<tempnam>> uses the value of <<P_tmpdir>> (defined in `<<stdio.h>>').
46 If you don't need any particular prefix to the basename of temporary
47 files, you can pass <<NULL>> as the <[pfx]> argument to <<tempnam>>.
49 <<_tmpnam_r>> and <<_tempnam_r>> are reentrant versions of <<tmpnam>>
50 and <<tempnam>> respectively. The extra argument <[reent]> is a
51 pointer to a reentrancy structure.
53 WARNINGS
54 The generated filenames are suitable for temporary files, but do not
55 in themselves make files temporary. Files with these names must still
56 be explicitly removed when you no longer want them.
58 If you supply your own data area <[s]> for <<tmpnam>>, you must ensure
59 that it has room for at least <<L_tmpnam>> elements of type <<char>>.
61 RETURNS
62 Both <<tmpnam>> and <<tempnam>> return a pointer to the newly
63 generated filename.
65 PORTABILITY
66 ANSI C requires <<tmpnam>>, but does not specify the use of
67 <<P_tmpdir>>. The System V Interface Definition (Issue 2) requires
68 both <<tmpnam>> and <<tempnam>>.
70 Supporting OS subroutines required: <<close>>, <<fstat>>, <<getpid>>,
71 <<isatty>>, <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
73 The global pointer <<environ>> is also required.
76 #include <_ansi.h>
77 #include <reent.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <fcntl.h>
82 #include <reent.h>
83 #include <errno.h>
85 #ifdef _REENT_THREAD_LOCAL
86 _Thread_local int _tls_inc;
87 _Thread_local char _tls_emergency[_REENT_EMERGENCY_SIZE];
88 #endif
90 /* Try to open the file specified, if it can't be opened then try
91 another one. Return nonzero if successful, otherwise zero. */
93 static int
94 worker (struct _reent *ptr,
95 char *result,
96 const char *part1,
97 const char *part2,
98 int part3,
99 int *part4)
101 /* Generate the filename and make sure that there isn't one called
102 it already. */
104 while (1)
106 int t;
107 _sprintf_r (ptr, result, "%s/%s%x.%x", part1, part2, part3, *part4);
108 (*part4)++;
109 t = _open_r (ptr, result, O_RDONLY, 0);
110 if (t == -1)
112 if (_REENT_ERRNO(ptr) == ENOSYS)
114 result[0] = '\0';
115 return 0;
117 break;
119 _close_r (ptr, t);
121 return 1;
124 char *
125 _tmpnam_r (struct _reent *p,
126 char *s)
128 char *result;
129 int pid;
131 if (s == NULL)
133 /* ANSI states we must use an internal static buffer if s is NULL */
134 _REENT_CHECK_EMERGENCY(p);
135 result = _REENT_EMERGENCY(p);
137 else
139 result = s;
141 pid = _getpid_r (p);
143 if (worker (p, result, P_tmpdir, "t", pid, &_REENT_INC(p)))
145 _REENT_INC(p)++;
146 return result;
149 return NULL;
152 char *
153 _tempnam_r (struct _reent *p,
154 const char *dir,
155 const char *pfx)
157 char *filename;
158 int length;
159 const char *prefix = (pfx) ? pfx : "";
160 if (dir == NULL && (dir = getenv ("TMPDIR")) == NULL)
161 dir = P_tmpdir;
163 /* two 8 digit numbers + . / */
164 length = strlen (dir) + strlen (prefix) + (4 * sizeof (int)) + 2 + 1;
166 filename = _malloc_r (p, length);
167 if (filename)
169 if (! worker (p, filename, dir, prefix,
170 _getpid_r (p) ^ (int) (_POINTER_INT) p, &_REENT_INC(p)))
171 return NULL;
173 return filename;
176 #ifndef _REENT_ONLY
178 char *
179 tempnam (const char *dir,
180 const char *pfx)
182 return _tempnam_r (_REENT, dir, pfx);
185 char *
186 tmpnam (char *s)
188 return _tmpnam_r (_REENT, s);
191 #endif