Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / dmake / msdos / borland / tempnam.c
blobc4d599de5971449cd4b10b0b174a73382b5872cb
1 /* RCS $Id: tempnam.c,v 1.1.1.1 2000-09-22 15:33:28 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.
25 /*LINTLIBRARY*/
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <dos.h>
31 #if defined(max)
32 # undef max
33 #endif
34 #define max(A,B) (((A)<(B))?(B):(A))
36 extern char *mktemp();
37 extern int access();
38 int d_access();
40 /* Turbo C stdio.h doesn't define P_tmpdir, so let's do it here */
41 /* Under DOS leave the default tmpdir pointing here! */
42 #ifndef P_tmpdir
43 static char *P_tmpdir = "";
44 #endif
46 char *
47 tempnam(dir, prefix)
48 char *dir; /* use this directory please (if non-NULL) */
49 char *prefix; /* use this (if non-NULL) as filename prefix */
51 static int count = 0;
52 register char *p, *q, *tmpdir;
53 int tl=0, dl=0, pl;
54 char buf[30];
56 pl = strlen(P_tmpdir);
58 if( (tmpdir = getenv("TMPDIR")) != NULL ) tl = strlen(tmpdir);
59 else if( (tmpdir = getenv("TMP")) != NULL ) tl = strlen(tmpdir);
60 if( dir != NULL ) dl = strlen(dir);
62 if( (p = malloc((unsigned)(max(max(dl,tl),pl)+13))) == NULL )
63 return(NULL);
65 *p = '\0';
67 if( (tl == 0) || (d_access( strcpy(p, tmpdir), 0) != 0) )
68 if( (dl == 0) || (d_access( strcpy(p, dir), 0) != 0) )
69 if( d_access( strcpy(p, P_tmpdir), 0) != 0 )
70 if( !prefix )
71 prefix = "tp";
73 if(prefix)
75 *(p+strlen(p)+2) = '\0';
76 (void)strncat(p, prefix, 2);
79 sprintf( buf, "%08x", _psp );
80 buf[6]='\0';
81 (void)strcat(p, buf );
82 sprintf( buf, "%04d", count++ );
83 q=p+strlen(p)-6;
84 *q++ = buf[0]; *q++ = buf[1];
85 *q++ = buf[2]; *q = buf[3];
87 if( (q = strrchr(p,'.')) != NULL ) *q = '\0';
89 return(p);
94 d_access( name, flag )
95 char *name;
96 int flag;
98 extern char *DirSepStr;
99 char *p;
100 int r;
102 if( name == NULL || !*name ) return(1); /* NULL dir means current dir */
103 r = access( name, flag );
104 p = name+strlen(name)-1;
106 if(*p != '/' && *p != '\\') strcat( p, DirSepStr );
108 return( r );