Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / dmake / os2 / ibm / tempnam.c
blob5bf5c21b35440f8c822067f0d3940efc0fe4c20a
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.
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 "config.h"
32 #if defined(max)
33 # undef max
34 #endif
35 #define max(A,B) (((A)<(B))?(B):(A))
37 extern int access();
38 int d_access();
40 /* MSC stdio.h defines P_tmpdir, so let's undo it here */
41 /* Under DOS leave the default tmpdir pointing here! */
42 #ifdef P_tmpdir
43 #undef P_tmpdir
44 #endif
45 static char *P_tmpdir = "";
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 pl = strlen(P_tmpdir);
59 if( (tmpdir = getenv("TMPDIR")) != NULL )
60 tl = strlen(tmpdir);
61 else if( (tmpdir = getenv("TMP")) != NULL )
62 tl = strlen(tmpdir);
63 if( dir != NULL ) dl = strlen(dir);
65 if( (p = malloc((unsigned)(max(max(dl,tl),pl)+13))) == NULL )
66 return(NULL);
68 *p = '\0';
70 if( (tl == 0) || (d_access( strcpy(p, tmpdir), 0) != 0) )
71 if( (dl == 0) || (d_access( strcpy(p, dir), 0) != 0) )
72 if( d_access( strcpy(p, P_tmpdir), 0) != 0 )
73 if( !prefix )
74 prefix = "tp";
76 if(prefix)
78 *(p+strlen(p)+2) = '\0';
79 (void)strncat(p, prefix, 2);
82 sprintf( buf, "%08x", getpid() );
83 buf[6]='\0';
84 (void)strcat(p, buf );
85 sprintf( buf, "%04d", count++ );
86 q=p+strlen(p)-6;
87 *q++ = buf[0]; *q++ = buf[1];
88 *q++ = buf[2]; *q++ = buf[3];
90 if( (q = strrchr(p,'.')) != NULL ) *q = '\0';
92 return strlwr(p);
97 d_access( name, flag )
98 char *name;
99 int flag;
101 char *p;
102 int r;
104 if( name == NULL || !*name ) return(1); /* NULL dir means current dir */
105 p = name+strlen(name)-1;
106 if(*p == ':' ) strcat( p++, "\\" );
107 r = access( name, flag );
108 if(*p != '/' && *p != '\\') strcat( p, "\\" );
110 return( r );