update dev300-m57
[ooovba.git] / dmake / unix / macosx / tempnam.c
blobff5dace5b07ee8ceab48c4f5d1f4b53c36f77bff
1 /* RCS $Id: tempnam.c,v 1.1 2001-02-23 03:44:30 pluby 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.
17 --
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 /*LINTLIBRARY*/
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
31 #if defined(max)
32 # undef max
33 #endif
34 #define max(A,B) (((A)<(B))?(B):(A))
36 extern char *mktemp();
37 extern int access();
38 int d_access();
40 char *
41 dtempnam(dir, prefix)
42 char *dir; /* use this directory please (if non-NULL) */
43 char *prefix; /* use this (if non-NULL) as filename prefix */
45 static int count = 0;
46 register char *p, *q, *tmpdir;
47 int tl=0, dl=0, pl;
48 char buf[30];
50 pl = strlen(P_tmpdir);
52 if( (tmpdir = getenv("TMPDIR")) != NULL ) tl = strlen(tmpdir);
53 else if( (tmpdir = getenv("TMP")) != NULL ) tl = strlen(tmpdir);
54 if( dir != NULL ) dl = strlen(dir);
56 if( (p = malloc((unsigned)(max(max(dl,tl),pl)+13))) == NULL )
57 return(NULL);
59 *p = '\0';
61 if( (tl == 0) || (d_access( strcpy(p, tmpdir), 0) != 0) )
62 if( (dl == 0) || (d_access( strcpy(p, dir), 0) != 0) )
63 if( d_access( strcpy(p, P_tmpdir), 0) != 0 )
64 if( !prefix )
65 prefix = "tp";
67 if(prefix)
69 *(p+strlen(p)+2) = '\0';
70 (void)strncat(p, prefix, 2);
73 sprintf( buf, "%08x", getpid() );
74 buf[6]='\0';
75 (void)strcat(p, buf );
76 sprintf( buf, "%04d", count++ );
77 q=p+strlen(p)-6;
78 *q++ = buf[0]; *q++ = buf[1];
79 *q++ = buf[2]; *q++ = buf[3];
81 if( (q = strrchr(p,'.')) != NULL ) *q = '\0';
83 return(p);
88 d_access( name, flag )
89 char *name;
90 int flag;
92 extern char *DirSepStr;
93 char *p;
94 int r;
96 if( name == NULL || !*name ) return(1); /* NULL dir means current dir */
97 r = access( name, flag );
98 p = name+strlen(name)-1;
99 if(*p != '/' && *p != '\\') strcat( p, DirSepStr );
101 return( r );