pathtemp.c revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2007 AT&T Knowledge Ventures *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Knowledge Ventures *
* *
* A copy of the License is available at *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* 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)
*
* if buf==0 then space is malloc'd
* buf size is size
* dir and pfx may be 0
* 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)
* /TMPPATH dir overrides the env value
* /TMPDIR dir overrides the env value
*/
#include <ast.h>
#include <ls.h>
#include <tm.h>
#define ATTEMPT 10
#define TMP_ENV "TMPDIR"
#define TMP_PATH_ENV "TMPPATH"
#define TMP1 "/tmp"
static struct
{
char** vec;
char** dir;
unsigned long key;
unsigned long rng;
int manual;
char* pfx;
char* tmpdir;
char* tmppath;
char*
{
register char* d;
register char* b;
register char* s;
register char* x;
char* fmt;
int m;
int n;
int z;
int attempt;
{
pfx++;
{
if (!dir)
{
}
else
return (char*)pfx;
}
{
return (char*)pfx;
}
{
{
}
return (char*)pfx;
}
{
{
}
return (char*)pfx;
}
return 0;
}
{
{
{
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;
if (buf && dir && pfx && (buf == (char*)dir && (buf + strlen(buf) + 1) == (char*)pfx || buf == (char*)pfx && !*dir) && !strcmp((char*)pfx + strlen(pfx) + 1, "XXXXX"))
{
z = 0;
d = (char*)dir;
fmt = "%03.3.32lu%03.3.32lu";
}
else
{
z = '.';
m = 5;
fmt = "%02.2.32lu.%03.3.32lu";
}
x = b + len;
s = b;
if (d)
{
while (s < x && (n = *d++))
*s++ = n;
if (s < x && s > b && *(s - 1) != '/')
*s++ = '/';
}
pfx = "ast";
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 = (unsigned long)tmp.pid * ((unsigned long)time(NiL) ^ (((unsigned long)(&attempt)) >> 3) ^ (((unsigned long)tmp.dir) >> 3));
/*
* Knuth vol.2, page.16, Thm.A
*/
}
/*
* generate a pseudo-random name
*/
if (fdp)
{
{
*fdp = n;
return b;
}
}
return b;
}
if (!buf)
free(b);
return 0;
}