sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
* FUNCTION
|
sl@0
|
16 |
* <<tmpnam>>, <<tempnam>>---name for a temporary file
|
sl@0
|
17 |
* INDEX
|
sl@0
|
18 |
* tmpnam
|
sl@0
|
19 |
* INDEX
|
sl@0
|
20 |
* tempnam
|
sl@0
|
21 |
* INDEX
|
sl@0
|
22 |
* _tmpnam_r
|
sl@0
|
23 |
* INDEX
|
sl@0
|
24 |
* _tempnam_r
|
sl@0
|
25 |
* ANSI_SYNOPSIS
|
sl@0
|
26 |
* #include <stdio.h>
|
sl@0
|
27 |
* char *tmpnam(char *<[s]>);
|
sl@0
|
28 |
* char *tempnam(char *<[dir]>, char *<[pfx]>);
|
sl@0
|
29 |
* char *_tmpnam_r(void *<[reent]>, char *<[s]>);
|
sl@0
|
30 |
* char *_tempnam_r(void *<[reent]>, char *<[dir]>, char *<[pfx]>);
|
sl@0
|
31 |
* TRAD_SYNOPSIS
|
sl@0
|
32 |
* #include <stdio.h>
|
sl@0
|
33 |
* char *tmpnam(<[s]>)
|
sl@0
|
34 |
* char *<[s]>;
|
sl@0
|
35 |
* char *tempnam(<[dir]>, <[pfx]>)
|
sl@0
|
36 |
* char *<[dir]>;
|
sl@0
|
37 |
* char *<[pfx]>;
|
sl@0
|
38 |
* char *_tmpnam_r(<[reent]>, <[s]>)
|
sl@0
|
39 |
* char *<[reent]>;
|
sl@0
|
40 |
* char *<[s]>;
|
sl@0
|
41 |
* char *_tempnam_r(<[reent]>, <[dir]>, <[pfx]>)
|
sl@0
|
42 |
* char *<[reent]>;
|
sl@0
|
43 |
* char *<[dir]>;
|
sl@0
|
44 |
* char *<[pfx]>;
|
sl@0
|
45 |
* Use either of these functions to generate a name for a temporary file.
|
sl@0
|
46 |
* The generated name is guaranteed to avoid collision with other files
|
sl@0
|
47 |
* (for up to <<TMP_MAX>> calls of either function).
|
sl@0
|
48 |
* <<tmpnam>> generates file names with the value of <<P_tmpdir>>
|
sl@0
|
49 |
* (defined in `<<stdio.h>>') as the leading directory component of the path.
|
sl@0
|
50 |
* You can use the <<tmpnam>> argument <[s]> to specify a suitable area
|
sl@0
|
51 |
* of memory for the generated filename; otherwise, you can call
|
sl@0
|
52 |
* <<tmpnam(NULL)>> to use an internal static buffer.
|
sl@0
|
53 |
* <<tempnam>> allows you more control over the generated filename: you
|
sl@0
|
54 |
* can use the argument <[dir]> to specify the path to a directory for
|
sl@0
|
55 |
* temporary files, and you can use the argument <[pfx]> to specify a
|
sl@0
|
56 |
* prefix for the base filename.
|
sl@0
|
57 |
* If <[dir]> is <<NULL>>, <<tempnam>> will attempt to use the value of
|
sl@0
|
58 |
* environment variable <<TMPDIR>> instead; if there is no such value,
|
sl@0
|
59 |
* <<tempnam>> uses the value of <<P_tmpdir>> (defined in `<<stdio.h>>').
|
sl@0
|
60 |
* If you don't need any particular prefix to the basename of temporary
|
sl@0
|
61 |
* files, you can pass <<NULL>> as the <[pfx]> argument to <<tempnam>>.
|
sl@0
|
62 |
* <<_tmpnam_r>> and <<_tempnam_r>> are reentrant versions of <<tmpnam>>
|
sl@0
|
63 |
* and <<tempnam>> respectively. The extra argument <[reent]> is a
|
sl@0
|
64 |
* pointer to a reentrancy structure.
|
sl@0
|
65 |
* WARNINGS
|
sl@0
|
66 |
* The generated filenames are suitable for temporary files, but do not
|
sl@0
|
67 |
* in themselves make files temporary. Files with these names must still
|
sl@0
|
68 |
* be explicitly removed when you no longer want them.
|
sl@0
|
69 |
* If you supply your own data area <[s]> for <<tmpnam>>, you must ensure
|
sl@0
|
70 |
* that it has room for at least <<L_tmpnam>> elements of type <<char>>.
|
sl@0
|
71 |
* RETURNS
|
sl@0
|
72 |
* Both <<tmpnam>> and <<tempnam>> return a pointer to the newly
|
sl@0
|
73 |
* generated filename.
|
sl@0
|
74 |
* PORTABILITY
|
sl@0
|
75 |
* ANSI C requires <<tmpnam>>, but does not specify the use of
|
sl@0
|
76 |
* <<P_tmpdir>>. The System V Interface Definition (Issue 2) requires
|
sl@0
|
77 |
* both <<tmpnam>> and <<tempnam>>.
|
sl@0
|
78 |
* Supporting OS subroutines required: <<close>>, <<fstat>>, <<getpid>>,
|
sl@0
|
79 |
* <<isatty>>, <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
|
sl@0
|
80 |
* The global pointer <<environ>> is also required.
|
sl@0
|
81 |
*
|
sl@0
|
82 |
*
|
sl@0
|
83 |
*/
|
sl@0
|
84 |
|
sl@0
|
85 |
|
sl@0
|
86 |
|
sl@0
|
87 |
#include <_ansi.h>
|
sl@0
|
88 |
#include <stdio_r.h>
|
sl@0
|
89 |
#include <stdlib.h>
|
sl@0
|
90 |
#include <string.h>
|
sl@0
|
91 |
#include <fcntl.h>
|
sl@0
|
92 |
#include <sys/stat.h>
|
sl@0
|
93 |
#include <unistd.h>
|
sl@0
|
94 |
#include <errno.h>
|
sl@0
|
95 |
|
sl@0
|
96 |
|
sl@0
|
97 |
/* Try to open the file specified, if it can be opened then try
|
sl@0
|
98 |
another one. */
|
sl@0
|
99 |
|
sl@0
|
100 |
#define MAXFILENAME 255
|
sl@0
|
101 |
|
sl@0
|
102 |
static void worker (struct _reent *ptr,char *result,int part3,int *part4)
|
sl@0
|
103 |
{
|
sl@0
|
104 |
/* Generate the filename and make sure that there isn't one called
|
sl@0
|
105 |
it already. */
|
sl@0
|
106 |
|
sl@0
|
107 |
for (;;)
|
sl@0
|
108 |
{
|
sl@0
|
109 |
int t;
|
sl@0
|
110 |
struct stat st;
|
sl@0
|
111 |
_sprintf_r (ptr, result, P_tmpdir "t%x.%x", part3, *part4);
|
sl@0
|
112 |
t = stat(result, &st);
|
sl@0
|
113 |
if (t == -1)
|
sl@0
|
114 |
break; /* file doesn't exist, so it's a plausible name */
|
sl@0
|
115 |
(*part4)++;
|
sl@0
|
116 |
}
|
sl@0
|
117 |
}
|
sl@0
|
118 |
|
sl@0
|
119 |
/** A reentrant version of tmpnam().
|
sl@0
|
120 |
*/
|
sl@0
|
121 |
EXPORT_C char * _tmpnam_r (struct _reent *p, char *s)
|
sl@0
|
122 |
{
|
sl@0
|
123 |
char *result;
|
sl@0
|
124 |
int pid;
|
sl@0
|
125 |
|
sl@0
|
126 |
if (s == NULL)
|
sl@0
|
127 |
result = p->_tmpnam;
|
sl@0
|
128 |
else
|
sl@0
|
129 |
result = s;
|
sl@0
|
130 |
pid = getpid();
|
sl@0
|
131 |
worker (p, result, pid, &p->_inc);
|
sl@0
|
132 |
p->_inc++;
|
sl@0
|
133 |
return result;
|
sl@0
|
134 |
}
|
sl@0
|
135 |
|
sl@0
|
136 |
/** A reentrant version of wtmpnam().
|
sl@0
|
137 |
*/
|
sl@0
|
138 |
EXPORT_C wchar_t * _wtmpnam_r (struct _reent *p, wchar_t *s)
|
sl@0
|
139 |
{
|
sl@0
|
140 |
char *result;
|
sl@0
|
141 |
int pid;
|
sl@0
|
142 |
char temp1[MAXFILENAME];
|
sl@0
|
143 |
wchar_t *target;
|
sl@0
|
144 |
|
sl@0
|
145 |
|
sl@0
|
146 |
if (s == NULL)
|
sl@0
|
147 |
result = p->_tmpnam;
|
sl@0
|
148 |
else
|
sl@0
|
149 |
result = temp1;
|
sl@0
|
150 |
|
sl@0
|
151 |
pid = getpid();
|
sl@0
|
152 |
worker (p, result, pid, &p->_inc);
|
sl@0
|
153 |
p->_inc++;
|
sl@0
|
154 |
|
sl@0
|
155 |
//store the wide result
|
sl@0
|
156 |
target = s ? s : p->_wtmpnam;
|
sl@0
|
157 |
if (mbstowcs(target, result, 37))
|
sl@0
|
158 |
return target;
|
sl@0
|
159 |
|
sl@0
|
160 |
p->_errno=EILSEQ;
|
sl@0
|
161 |
return 0;
|
sl@0
|
162 |
}
|
sl@0
|
163 |
|
sl@0
|
164 |
#ifndef _REENT_ONLY
|
sl@0
|
165 |
|
sl@0
|
166 |
/**
|
sl@0
|
167 |
Generate a unique temporary filename.
|
sl@0
|
168 |
@return A pointer to the string containing the proposed name for a temporary file.
|
sl@0
|
169 |
If NULL was specified as the buffer this points to an internal buffer
|
sl@0
|
170 |
that will be overwritten the next time this function is called,
|
sl@0
|
171 |
otherwise it returns the buffer parameter.
|
sl@0
|
172 |
If an error occurs this function returns NULL.
|
sl@0
|
173 |
@param s Pointer to an array of bytes, where the proposed tempname will be stored.
|
sl@0
|
174 |
*/
|
sl@0
|
175 |
EXPORT_C char * tmpnam (char *s)
|
sl@0
|
176 |
{
|
sl@0
|
177 |
return _tmpnam_r (_REENT, s);
|
sl@0
|
178 |
}
|
sl@0
|
179 |
|
sl@0
|
180 |
EXPORT_C wchar_t * wtmpnam (wchar_t *s)
|
sl@0
|
181 |
{
|
sl@0
|
182 |
return _wtmpnam_r (_REENT, s);
|
sl@0
|
183 |
}
|
sl@0
|
184 |
|
sl@0
|
185 |
|
sl@0
|
186 |
#endif
|