[net.eunice] temporary file names

keith@seismo.UUCP (Keith Bostic) (09/18/84)

Just got back from a news vacation -- found a long ago article suggesting
that people use mktemp(3) to produce unique file names -- and a small
program that allowed it to be called from the shell.

This is a dangerous fix on EUNICE machines.  The EUNICE shell does not
increment process id's as expected under UNIX.  Just as an example:

#include <stdio.h>

main()
{
	static char	buf[] = "fileXXXXXX";
	char	*mktemp();

	printf("file name: %s\n",mktemp(buf));
}

when run twice in a row will produce the same file name.

		Keith 
			ARPA: keith@seismo 
			UUCP: seismo!keith

chip@t4test.UUCP (Chip Rosenthal) (09/20/84)

--- REFERENCED ARTICLE ---------------------------------------------

>From: keith@seismo.UUCP (Keith Bostic)
>Date: Mon, 17-Sep-84 15:08:51 PDT
>
>This is a dangerous fix on EUNICE machines.  The EUNICE shell does not
>increment process id's as expected under UNIX.

--------------------------------------------------------------------

That is true...but `mktemp' works anyway.  As suggested, `mktemp'
just tries to stick the PID on then end of your template.  If that
already exists, it inserts an `a' before the PID.  If that exists,
it tries it with a `b'.  The result is that I will personally double
your money back guarantee you that you will be able to get at least
27 unique filenames for a given process ID.  What `mktemp' does after
`z', I'm not sure.  Who knows...maybe someday I'll find out the *hard*
way.

(Hey...we're getting some discussions going in net.eunice.  I love it!!)

-- 

Chip Rosenthal, Intel/Santa Clara
{ idi|intelca|icalqa|kremvax|qubix|ucscc } ! t4test ! { chip|news }

keith@seismo.UUCP (Keith Bostic) (09/24/84)

> --- REFERENCED ARTICLE ---------------------------------------------
> 
> >From: keith@seismo.UUCP (Keith Bostic)
> >Date: Mon, 17-Sep-84 15:08:51 PDT
> >
> >This is a dangerous fix on EUNICE machines.  The EUNICE shell does not
> >increment process id's as expected under UNIX.
>
> --------------------------------------------------------------------
> That is true...but `mktemp' works anyway.  As suggested, `mktemp'
> just tries to stick the PID on then end of your template.  If that
> already exists, it inserts an `a' before the PID.  If that exists,
> it tries it with a `b'.  The result is that I will personally double
> your money back guarantee you that you will be able to get at least
> 27 unique filenames for a given process ID.  What `mktemp' does after
> `z', I'm not sure.  Who knows...maybe someday I'll find out the *hard*
> way.

Okay -- go for it -- put this in your system *instead* of mktemp and all
your nasty little problems go away.  Because, interestingly enough,
mktemp returns "/" after 'z'.  Which is probably one of the most brain-
damaged pieces of software written since the world began.

Anyway -- here is a mktemp that gets around the problem EUNICE has in
incrementing the process id, 'cause it will give you more temporary files
than a cat will kittens.  It's behavior (up to 'z') is identical to the
standard versions, and it's just as fast, if not faster.

		Keith 
			ARPA: keith@seismo 
			UUCP: seismo!keith

............... cut on the dotted line.................

#include <ctype.h>

char *
mktemp(as)
register char	*as;
{
	register char	*start,		/* note start of X's */
			*trv;		/* travel through string */
	unsigned	pid;

	pid = getpid();
	for(trv = as;*trv;++trv);	/* fill in pid */
	while(*--trv == 'X') {
		*trv = (pid % 10) + '0';
		pid /= 10;
	}
	for(start = ++trv;;) {
		if (access(as,0)) return(as);
		for(trv = start;;) {
			if (!*trv) return((char *)0);
			if (*trv == 'z') *trv++ = 'a';
			else {
				if (isdigit(*trv)) *trv = 'a';
				else ++*trv;
				break;
			}
		}
	}
}