1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: mktemp.c,v 2.4 2003/02/25 02:47:21 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Replacement mktemp(3) function for systems without |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
10 |
#ifndef NULL |
11 |
#define NULL 0 |
12 |
#endif |
13 |
|
14 |
|
15 |
char * |
16 |
mktemp(template) /* make a unique filename from template */ |
17 |
char *template; |
18 |
{ |
19 |
register char *tb, *te, *p; |
20 |
int pid; |
21 |
/* find string of 6 (opt) X's */ |
22 |
for (te = template; *te; te++) |
23 |
; |
24 |
while (te > template && te[-1] != 'X') |
25 |
te--; |
26 |
if (te == template) |
27 |
return(template); /* no X's! */ |
28 |
for (tb = te; tb > template && tb[-1] == 'X'; tb--) |
29 |
; |
30 |
if (te-tb > 6) /* only need 6 chars */ |
31 |
tb = te-6; |
32 |
pid = getpid(); /* 5 (opt) chars of pid */ |
33 |
for (p = te-2; p >= tb; p--) { |
34 |
*p = pid%10 + '0'; |
35 |
pid /= 10; |
36 |
} |
37 |
p = te-1; /* final character */ |
38 |
for (*p = 'a'; *p <= 'z'; (*p)++) |
39 |
if (access(template, 0) == -1) |
40 |
return(template); /* found unique name */ |
41 |
return(NULL); /* failure! */ |
42 |
} |