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