]> git.sur5r.net Git - openldap/blob - libraries/liblutil/tempnam.c
If we are using lutil_lock functions then define NEED_SIMPLE_LOCKING or
[openldap] / libraries / liblutil / tempnam.c
1 #include "portable.h"
2
3 #ifndef HAVE_TEMPNAM
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <ac/string.h>
8 #include <ac/unistd.h>
9
10 #include "lutil.h"
11
12 char *
13 tempnam( const char *dir, const char *pfx )
14 {
15     char        *s;
16
17     if ( dir == NULL ) {
18         dir = "/tmp";
19     }
20
21 /*
22  * allocate space for dir + '/' + pfx (up to 5 chars) + 6 trailing 'X's + 0 byte
23  */
24     if (( s = (char *)malloc( strlen( dir ) + 14 )) == NULL ) {
25         return( NULL );
26     }
27
28     strcpy( s, dir );
29     strcat( s, "/" );
30     if ( pfx != NULL ) {
31         strcat( s, pfx );
32     }
33     strcat( s, "XXXXXX" );
34     mktemp( s );
35
36     if ( *s == '\0' ) {
37         free( s );
38         s = NULL;
39     }
40
41     return( s );
42 }
43
44 #endif /* TEMPNAM */