/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* AT&T Research
*
* generate a temp file / name
*
* [<dir>/][<pfx>]<bas>.<suf>
*
* length(<pfx>)<=5
* length(<bas>)==3
* length(<suf>)==3
*
* pathtmp(a,b,c,d) pathtemp(a,L_tmpnam,b,c,0)
* tmpfile() char*p=pathtemp(0,0,0,"tf",&sp);
* remove(p);
* free(p)
* tmpnam(0) static char p[L_tmpnam];
* pathtemp(p,sizeof(p),0,"tn",0)
* tmpnam(p) pathtemp(p,L_tmpnam,0,"tn",0)
* tempnam(d,p) pathtemp(0,d,p,0)
* mktemp(p) pathtemp(0,0,p,0)
*
* if buf==0 then space is malloc'd
* buf size is size
* dir and pfx may be 0
* if pfx contains trailing X's then it is a mktemp(3) template
* otherwise only first 5 chars of pfx are used
* if fdp!=0 then the path is opened O_EXCL and *fdp is the open fd
* malloc'd space returned by successful pathtemp() calls
* must be freed by the caller
*
* generated names are pseudo-randomized to avoid both
*
* / as first pfx char provides tmp file generation control
* 0 returned for unknown ops
*
* /cycle dir specifies TMPPATH cycle control
* automatic (default) cycled with each tmp file
* manual cycled by application with dir=(nil)
* (nil) cycle TMPPATH
* /prefix dir specifies the default prefix (default ast)
* /seed dir specifies pseudo-random generator seed
* 0 or "0" to re-initialize
* /TMPPATH dir overrides the env value
* /TMPDIR dir overrides the env value
*/
#include <ast.h>
#include <ls.h>
#include <tv.h>
#include <tm.h>
static struct
{
char** vec;
char** dir;
int manual;
int seed;
char* pfx;
char* tmpdir;
char* tmppath;
char*
{
register char* d;
register char* b;
register char* s;
register char* x;
int m;
int n;
int l;
int r;
int z;
int attempt;
{
pfx++;
{
if (!dir)
{
}
else
return (char*)pfx;
}
{
return (char*)pfx;
}
{
return (char*)pfx;
}
{
return (char*)pfx;
}
{
tmp.key = (tmp.seed = (tmp.rng = dir ? (uint32_t)strtoul(dir, NiL, 0) : (uint32_t)1) != 0)? (uint32_t)0x63c63cd9L : 0;
return (char*)pfx;
}
{
{
}
return (char*)pfx;
}
{
{
}
return (char*)pfx;
}
return 0;
}
else
{
{
{
n = 2;
s = x;
while (s = strchr(s, ':'))
{
s++;
n++;
}
return 0;
while (x = strchr(x, ':'))
{
*x++ = 0;
}
}
else
{
d = 0;
return 0;
if (d)
}
}
{
}
if (!d && (!*(d = astconf("TMP", NiL, NiL)) || eaccess(d, W_OK|X_OK)) && eaccess(d = TMP1, W_OK|X_OK) && eaccess(d = TMP2, W_OK|X_OK))
return 0;
}
if (!len)
len--;
return 0;
z = 0;
pfx = "ast";
if (buf && dir && (buf == (char*)dir && (buf + strlen(buf) + 1) == (char*)pfx || buf == (char*)pfx && !*dir) && !strcmp((char*)pfx + m + 1, "XXXXX"))
{
d = (char*)dir;
l = 3;
r = 3;
}
{
r = m - l;
m = l;
l = r / 2;
r -= l;
}
{
m = 5;
l = 3;
r = 3;
}
else
{
z = '.';
m = 5;
l = 2;
r = 3;
}
x = b + len;
s = b;
if (d)
{
while (s < x && (n = *d++))
*s++ = n;
if (s < x && s > b && *(s - 1) != '/')
*s++ = '/';
}
if ((x - s) > m)
x = s + m;
while (s < x && (n = *pfx++))
{
if (n == '/' || n == '\\' || n == z)
n = '_';
*s++ = n;
}
*s = 0;
len -= (s - b);
{
{
register int r;
/*
* get a quasi-random coefficient
*/
tmp.rng = (uint32_t)tmp.pid * ((uint32_t)time(NiL) ^ (((uint32_t)integralof(&attempt)) >> 3) ^ (((uint32_t)integralof(tmp.dir)) >> 3));
/*
* Knuth vol.2, page.16, Thm.A
*/
}
/*
* generate a pseudo-random name
*/
sfsprintf(keybuf, sizeof(keybuf), "%07.7.32I*u%07.7.32I*u", sizeof(key), key, sizeof(tmp.key), tmp.key);
if (fdp)
{
{
*fdp = n;
return b;
}
}
return b;
}
if (!buf)
free(b);
return 0;
}