Google
 

Trailing-Edge - PDP-10 Archives - SRI_NIC_PERM_FS_1_19910112 - kcc-5/lib/stdio/mktemp.c
There are 8 other files named mktemp.c in the archive. Click here to see a list.
/*
** mktemp - find name for temporary file
** David Eppstein / Stanford University / 9-Aug-84
** TENEX additions by Ken Harrenstien, SRI 1-Jun-85
*/

#include "c-env.h"
#include "stdio.h"
#include "sys/file.h"

/* ----------------------------------------------------------------------- */
/*      replace cap X's in as with hi-precision time w/ collision fix      */
/* ----------------------------------------------------------------------- */

char *
mktemp(buf)
char *buf;
{
    char *s = buf;
    int pid, i;

    pid = getpid();
#if CPU_PDP10
    pid += (unsigned)pid>>18;		/* Add LH to RH for more hashing */
#endif
    if (pid < 0) pid = -pid;		/* Ensure positive just in case */

    while (*s) s++;			/* skip to end of string */
    while (*--s == 'X') {		/* while in X's of target */
	*s = (pid%10) + '0';		/* add bottom digit */
	pid /= 10;			/* and move back in pid digits */
    }
    if (access(buf, F_OK) == -1)	/* file exist? */
	return buf;			/* nope, so we won! */
    while (*++s) {
	for (i = 'a'; i <= 'z'; i++) {	/* loop from 'a' to 'z' */
	    *s = i;			/* try replacing old with letter */
	    if (access(buf, F_OK) == -1)	/* look again.  file there? */
		return buf;		/* no, so won, return buf ptr */
	}				/* else loop and try next letter */
    }					/* all in use, try next position */
    return NULL;			/* lost our ass!! */
}