update dev300-m58
[ooovba.git] / dmake / winnt / borland / tempnam.c
blob4a1766d78d8b9af521148d9be4fdbeba260edd11
1 /* RCS $Id: tempnam.c,v 1.1.1.1 2000-09-22 15:33:37 hr 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>
30 #include <dos.h>
32 #if defined(max)
33 # undef max
34 #endif
35 #define max(A,B) (((A)<(B))?(B):(A))
37 extern char *mktemp();
38 extern int access();
39 int d_access();
41 /* Turbo C stdio.h doesn't define P_tmpdir, so let's do it here */
42 /* Under DOS leave the default tmpdir pointing here! */
43 #ifndef P_tmpdir
44 static char *P_tmpdir = "";
45 #endif
47 char *
48 tempnam(dir, prefix)
49 char *dir; /* use this directory please (if non-NULL) */
50 char *prefix; /* use this (if non-NULL) as filename prefix */
52 static int count = 0;
53 register char *p, *q, *tmpdir;
54 int tl=0, dl=0, pl;
55 char buf[30];
57 #if defined(__WIN32__)
58 unsigned int _psp = rand();
59 #endif
61 pl = strlen(P_tmpdir);
63 if( (tmpdir = getenv("TMPDIR")) != NULL ) tl = strlen(tmpdir);
64 else if( (tmpdir = getenv("TMP")) != NULL ) tl = strlen(tmpdir);
65 if( dir != NULL ) dl = strlen(dir);
67 if( (p = malloc((unsigned)(max(max(dl,tl),pl)+13))) == NULL )
68 return(NULL);
70 *p = '\0';
72 if( (tl == 0) || (d_access( strcpy(p, tmpdir), 0) != 0) )
73 if( (dl == 0) || (d_access( strcpy(p, dir), 0) != 0) )
74 if( d_access( strcpy(p, P_tmpdir), 0) != 0 )
75 if( !prefix )
76 prefix = "tp";
78 if(prefix)
80 *(p+strlen(p)+2) = '\0';
81 (void)strncat(p, prefix, 2);
84 sprintf( buf, "%08x", _psp );
85 buf[6]='\0';
86 (void)strcat(p, buf );
87 sprintf( buf, "%04d", count++ );
88 q=p+strlen(p)-6;
89 *q++ = buf[0]; *q++ = buf[1];
90 *q++ = buf[2]; *q = buf[3];
92 if( (q = strrchr(p,'.')) != NULL ) *q = '\0';
94 return(p);
99 d_access( name, flag )
100 char *name;
101 int flag;
103 extern char *DirSepStr;
104 char *p;
105 int r;
107 if( name == NULL || !*name ) return(1); /* NULL dir means current dir */
108 r = access( name, flag );
109 p = name+strlen(name)-1;
111 if(*p != '/' && *p != '\\') strcat( p, DirSepStr );
113 return( r );