update dev300-m58
[ooovba.git] / dmake / msdos / microsft / tempnam.c
blob41bad028bda7d627a7dc3632c8fe7f257c6ab0ea
1 /* RCS $Id: tempnam.c,v 1.1.1.1 2000-09-22 15:33:29 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 /* MSC stdio.h defines P_tmpdir, so let's undo it here */
42 /* Under DOS leave the default tmpdir pointing here! */
43 #ifdef P_tmpdir
44 #undef P_tmpdir
45 #endif
46 static char *P_tmpdir = "";
48 char *
49 tempnam(dir, prefix)
50 char *dir; /* use this directory please (if non-NULL) */
51 char *prefix; /* use this (if non-NULL) as filename prefix */
53 static int count = 0;
54 register char *p, *q, *tmpdir;
55 int tl=0, dl=0, pl;
56 char buf[30];
58 pl = strlen(P_tmpdir);
60 if( (tmpdir = getenv("TMPDIR")) != NULL ) tl = strlen(tmpdir);
61 else if( (tmpdir = getenv("TMP")) != NULL ) tl = strlen(tmpdir);
62 if( dir != NULL ) dl = strlen(dir);
64 if( (p = malloc((unsigned)(max(max(dl,tl),pl)+13))) == NULL )
65 return(NULL);
67 *p = '\0';
69 if( (tl == 0) || (d_access( strcpy(p, tmpdir), 0) != 0) )
70 if( (dl == 0) || (d_access( strcpy(p, dir), 0) != 0) )
71 if( d_access( strcpy(p, P_tmpdir), 0) != 0 )
72 if( !prefix )
73 prefix = "tp";
75 if(prefix)
77 *(p+strlen(p)+2) = '\0';
78 (void)strncat(p, prefix, 2);
81 sprintf( buf, "%08x", _psp );
82 buf[6]='\0';
83 (void)strcat(p, buf );
84 sprintf( buf, "%04d", count++ );
85 q=p+strlen(p)-6;
86 *q++ = buf[0]; *q++ = buf[1];
87 *q++ = buf[2]; *q++ = buf[3];
89 if( (q = strrchr(p,'.')) != NULL ) *q = '\0';
91 return(p);
96 d_access( name, flag )
97 char *name;
98 int flag;
100 extern char *DirSepStr;
101 char *p;
102 int r;
104 if( name == NULL || !*name ) return(1); /* NULL dir means current dir */
105 r = access( name, flag );
106 p = name+strlen(name)-1;
107 if(*p != '/' && *p != '\\') strcat( p, DirSepStr );
109 return( r );