Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / dmake / tos / tempnam.c
blob8c0e3077d65a9aa2a5c43049e0d31c87aef80d00
1 /* RCS $Id: tempnam.c,v 1.1.1.1 2000-09-22 15:33:33 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.
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.
27 /*LINTLIBRARY*/
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
32 #define max(A,B) (((A)<(B))?(B):(A))
34 extern char *mktemp();
35 extern int access();
37 static char *cpdir();
38 static char *seed="AAA";
40 /* BSD stdio.h doesn't define P_tmpdir, so let's do it here */
41 #ifndef P_tmpdir
42 static char *P_tmpdir = "/tmp";
43 #endif
45 char *
46 tempnam(dir, prefix)
47 char *dir; /* use this directory please (if non-NULL) */
48 char *prefix; /* use this (if non-NULL) as filename prefix */
50 register char *p, *q, *tmpdir;
51 int tl=0, dl=0, pl;
53 pl = strlen(P_tmpdir);
55 if( (tmpdir = getenv("TMPDIR")) != NULL ) tl = strlen(tmpdir);
56 if( dir != NULL ) dl = strlen(dir);
58 if( (p = malloc((unsigned)(max(max(dl,tl),pl)+16))) == NULL )
59 return(NULL);
61 *p = '\0';
63 if( (tl == 0) || (access( cpdir(p, tmpdir), 3) != 0) )
64 if( (dl == 0) || (access( cpdir(p, dir), 3) != 0) )
65 if( access( cpdir(p, P_tmpdir), 3) != 0 )
66 if( access( cpdir(p, "/tmp"), 3) != 0 )
67 return(NULL);
69 (void) strcat(p, "/");
70 if(prefix)
72 *(p+strlen(p)+5) = '\0';
73 (void)strncat(p, prefix, 5);
76 (void)strcat(p, seed);
77 (void)strcat(p, "XXXXXX");
79 q = seed;
80 while(*q == 'Z') *q++ = 'A';
81 ++*q;
83 if(*mktemp(p) == '\0') return(NULL);
84 return(p);
89 static char *
90 cpdir(buf, str)
91 char *buf;
92 char *str;
94 char *p;
96 if(str != NULL)
98 (void) strcpy(buf, str);
99 p = buf - 1 + strlen(buf);
100 if(*p == '/') *p = '\0';
103 return(buf);