Update contrib.
2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
16 * <<tmpnam>>, <<tempnam>>---name for a temporary file
27 * char *tmpnam(char *<[s]>);
28 * char *tempnam(char *<[dir]>, char *<[pfx]>);
29 * char *_tmpnam_r(void *<[reent]>, char *<[s]>);
30 * char *_tempnam_r(void *<[reent]>, char *<[dir]>, char *<[pfx]>);
35 * char *tempnam(<[dir]>, <[pfx]>)
38 * char *_tmpnam_r(<[reent]>, <[s]>)
41 * char *_tempnam_r(<[reent]>, <[dir]>, <[pfx]>)
45 * Use either of these functions to generate a name for a temporary file.
46 * The generated name is guaranteed to avoid collision with other files
47 * (for up to <<TMP_MAX>> calls of either function).
48 * <<tmpnam>> generates file names with the value of <<P_tmpdir>>
49 * (defined in `<<stdio.h>>') as the leading directory component of the path.
50 * You can use the <<tmpnam>> argument <[s]> to specify a suitable area
51 * of memory for the generated filename; otherwise, you can call
52 * <<tmpnam(NULL)>> to use an internal static buffer.
53 * <<tempnam>> allows you more control over the generated filename: you
54 * can use the argument <[dir]> to specify the path to a directory for
55 * temporary files, and you can use the argument <[pfx]> to specify a
56 * prefix for the base filename.
57 * If <[dir]> is <<NULL>>, <<tempnam>> will attempt to use the value of
58 * environment variable <<TMPDIR>> instead; if there is no such value,
59 * <<tempnam>> uses the value of <<P_tmpdir>> (defined in `<<stdio.h>>').
60 * If you don't need any particular prefix to the basename of temporary
61 * files, you can pass <<NULL>> as the <[pfx]> argument to <<tempnam>>.
62 * <<_tmpnam_r>> and <<_tempnam_r>> are reentrant versions of <<tmpnam>>
63 * and <<tempnam>> respectively. The extra argument <[reent]> is a
64 * pointer to a reentrancy structure.
66 * The generated filenames are suitable for temporary files, but do not
67 * in themselves make files temporary. Files with these names must still
68 * be explicitly removed when you no longer want them.
69 * If you supply your own data area <[s]> for <<tmpnam>>, you must ensure
70 * that it has room for at least <<L_tmpnam>> elements of type <<char>>.
72 * Both <<tmpnam>> and <<tempnam>> return a pointer to the newly
75 * ANSI C requires <<tmpnam>>, but does not specify the use of
76 * <<P_tmpdir>>. The System V Interface Definition (Issue 2) requires
77 * both <<tmpnam>> and <<tempnam>>.
78 * Supporting OS subroutines required: <<close>>, <<fstat>>, <<getpid>>,
79 * <<isatty>>, <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
80 * The global pointer <<environ>> is also required.
97 /* Try to open the file specified, if it can be opened then try
100 #define MAXFILENAME 255
102 static void worker (struct _reent *ptr,char *result,int part3,int *part4)
104 /* Generate the filename and make sure that there isn't one called
111 _sprintf_r (ptr, result, P_tmpdir "t%x.%x", part3, *part4);
112 t = stat(result, &st);
114 break; /* file doesn't exist, so it's a plausible name */
119 /** A reentrant version of tmpnam().
121 EXPORT_C char * _tmpnam_r (struct _reent *p, char *s)
131 worker (p, result, pid, &p->_inc);
136 /** A reentrant version of wtmpnam().
138 EXPORT_C wchar_t * _wtmpnam_r (struct _reent *p, wchar_t *s)
142 char temp1[MAXFILENAME];
152 worker (p, result, pid, &p->_inc);
155 //store the wide result
156 target = s ? s : p->_wtmpnam;
157 if (mbstowcs(target, result, 37))
167 Generate a unique temporary filename.
168 @return A pointer to the string containing the proposed name for a temporary file.
169 If NULL was specified as the buffer this points to an internal buffer
170 that will be overwritten the next time this function is called,
171 otherwise it returns the buffer parameter.
172 If an error occurs this function returns NULL.
173 @param s Pointer to an array of bytes, where the proposed tempname will be stored.
175 EXPORT_C char * tmpnam (char *s)
177 return _tmpnam_r (_REENT, s);
180 EXPORT_C wchar_t * wtmpnam (wchar_t *s)
182 return _wtmpnam_r (_REENT, s);