Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / dmake / unix / tempnam.c
blob4b143ed8d0d7fa3ee05e4bff7bbb51922fddc97f
1 /* RCS $Id: tempnam.c,v 1.2 2002-07-11 08:53:35 mh Exp $
2 --
3 -- SYNOPSIS
4 -- tempnam
5 --
6 -- DESCRIPTION
7 -- temp file name generation routines.
8 --
9 -- AUTHOR
10 -- Dennis Vadura, dvadura@dmake.wticorp.com
12 -- WWW
13 -- http://dmake.wticorp.com/
15 -- COPYRIGHT
16 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
18 -- This program is NOT free software; you can redistribute it and/or
19 -- modify it under the terms of the Software License Agreement Provided
20 -- in the file <distribution-root>/readme/license.txt.
22 -- LOG
23 -- Use cvs log to obtain detailed change logs.
26 #ifdef __APPLE__
28 /*LINTLIBRARY*/
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
33 #if defined(max)
34 # undef max
35 #endif
36 #define max(A,B) (((A)<(B))?(B):(A))
38 extern char *mktemp();
39 extern int access();
40 int d_access();
42 char *
43 dtempnam(dir, prefix)
44 char *dir; /* use this directory please (if non-NULL) */
45 char *prefix; /* use this (if non-NULL) as filename prefix */
47 static int count = 0;
48 register char *p, *q, *tmpdir;
49 int tl=0, dl=0, pl;
50 char buf[30];
52 pl = strlen(P_tmpdir);
54 if( (tmpdir = getenv("TMPDIR")) != NULL ) tl = strlen(tmpdir);
55 else if( (tmpdir = getenv("TMP")) != NULL ) tl = strlen(tmpdir);
56 if( dir != NULL ) dl = strlen(dir);
58 if( (p = malloc((unsigned)(max(max(dl,tl),pl)+13))) == NULL )
59 return(NULL);
61 *p = '\0';
63 if( (tl == 0) || (d_access( strcpy(p, tmpdir), 0) != 0) )
64 if( (dl == 0) || (d_access( strcpy(p, dir), 0) != 0) )
65 if( d_access( strcpy(p, P_tmpdir), 0) != 0 )
66 if( !prefix )
67 prefix = "tp";
69 if(prefix)
71 *(p+strlen(p)+2) = '\0';
72 (void)strncat(p, prefix, 2);
75 sprintf( buf, "%08x", getpid() );
76 buf[6]='\0';
77 (void)strcat(p, buf );
78 sprintf( buf, "%04d", count++ );
79 q=p+strlen(p)-6;
80 *q++ = buf[0]; *q++ = buf[1];
81 *q++ = buf[2]; *q++ = buf[3];
83 if( (q = strrchr(p,'.')) != NULL ) *q = '\0';
85 return(p);
90 d_access( name, flag )
91 char *name;
92 int flag;
94 extern char *DirSepStr;
95 char *p;
96 int r;
98 if( name == NULL || !*name ) return(1); /* NULL dir means current dir */
99 r = access( name, flag );
100 p = name+strlen(name)-1;
101 if(*p != '/' && *p != '\\') strcat( p, DirSepStr );
103 return( r );
106 #endif