1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.6 |
static const char RCSid[] = "$Id: mktemp.c,v 2.5 2003/06/07 12:50:20 schorsch Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Replacement mktemp(3) function for systems without |
6 |
greg |
2.3 |
*/ |
7 |
|
|
|
8 |
greg |
2.4 |
#include "copyright.h" |
9 |
greg |
2.1 |
|
10 |
schorsch |
2.5 |
#ifndef NULL |
11 |
greg |
2.2 |
#define NULL 0 |
12 |
schorsch |
2.5 |
#endif |
13 |
greg |
2.1 |
|
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 |
greg |
2.2 |
if (access(template, 0) == -1) |
40 |
greg |
2.1 |
return(template); /* found unique name */ |
41 |
|
|
return(NULL); /* failure! */ |
42 |
|
|
} |