update dev300-m58
[ooovba.git] / dmake / qssl / tempnam.c
blobde9c4692b1bffaa2204f610d5d059e821051ba4c
1 /* RCS $Id: tempnam.c,v 1.1.1.1 2000-09-22 15:33:30 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>
31 #define max(A,B) (((A)<(B))?(B):(A))
33 extern int access();
35 static char *cpdir();
36 static char seed[4]="AAA";
38 /* BSD stdio.h doesn't define P_tmpdir, so let's do it here */
39 #ifndef P_tmpdir
40 static char *P_tmpdir = "/tmp";
41 #endif
43 char *
44 tempnam(dir, prefix)
45 char *dir; /* use this directory please (if non-NULL) */
46 char *prefix; /* use this (if non-NULL) as filename prefix */
48 register char *p, *q, *tmpdir;
49 int tl=0, dl=0, pl;
51 pl = strlen(P_tmpdir);
53 if( (tmpdir = getenv("TMPDIR")) != NULL ) tl = strlen(tmpdir);
54 if( dir != NULL ) dl = strlen(dir);
56 if( (p = malloc((unsigned)(max(max(dl,tl),pl)+16))) == NULL )
57 return(NULL);
59 *p = '\0';
61 if( (tl == 0) || (access( cpdir(p, tmpdir), 3) != 0) )
62 if( (dl == 0) || (access( cpdir(p, dir), 3) != 0) )
63 if( access( cpdir(p, P_tmpdir), 3) != 0 )
64 if( access( cpdir(p, "/tmp"), 3) != 0 )
65 return(NULL);
67 (void) strcat(p, "/");
68 if(prefix)
70 *(p+strlen(p)+5) = '\0';
71 (void)strncat(p, prefix, 5);
74 (void)strcat(p, seed);
75 (void)strcat(p, "XXXXXX");
77 q = seed;
78 while(*q == 'Z') *q++ = 'A';
79 ++*q;
81 if(*tmpnam(p) == '\0') return(NULL);
82 return(p);
87 static char *
88 cpdir(buf, str)
89 char *buf;
90 char *str;
92 char *p;
94 if(str != NULL)
96 (void) strcpy(buf, str);
97 p = buf - 1 + strlen(buf);
98 if(*p == '/') *p = '\0';
101 return(buf);