Trailing-Edge
-
PDP-10 Archives
-
SRI_NIC_PERM_FS_1_19910112
-
c/kcc/bugtnm.c
There are no other files named bugtnm.c in the archive.
2-Apr-90 16:48:35-PDT,2748;000000000005
Return-Path: <[email protected]>
Received: from saqqara.cis.ohio-state.edu by NIC.DDN.MIL with TCP; Mon, 2 Apr 90 16:48:20 PDT
Received: by saqqara.cis.ohio-state.edu (5.61-kk/5.900402)
id AA09634; Mon, 2 Apr 90 19:47:19 -0400
Date: 02 Apr 90 17:06:56 EDT
From: <[email protected]>
To: <[email protected]>
Subject: tmpnam()
Message-Id: <"CSI 5715-6078"@CompuServe.COM>
Hi Ken,
Someone for some reason looked at the names generated by tmpnam(),
and discovered that they were sometimes longer than 6 characters. This
probably doesn't cause any harm, as the system will truncate them to six,
but it may cause some funny-ness in searching the 100-name space for one
that doesn't already exist: some names will be generated more often than
others. Also, the constant TMP_MAX is defined as 512, which is 5 times
more than the naming scheme actually will generate. This may cause us
to search for a free name longer than necessary, if in fact they're all
used up.
Here's a copy of tmpnam.c which does a modulo 100 on the discreet-
name count, instead of anding it with 0177 (which is frequently more than
the 2 digits allowed). I also redefined TMP_MAX to 100, which might be
more properly done in STDDEF.H; what do you think?
Michael
/*
** TMPNAM - create a unique temporary file name
**
** (c) Copyright Ken Harrenstien 1989
** for all changes after v.6, 13-Apr-1988
** (c) Copyright Ian Macky, SRI International 1986
**
** This code conforms with the description of the tmpnam()
** function in the ANSI X3J11 C language standard, section
** 4.9.4.4
**
*/
#include <c-env.h>
#if SYS_T20+SYS_10X+SYS_T10+SYS_CSI+SYS_WTS+SYS_ITS /* Systems supported */
#include <stdio.h>
#include <time.h>
#include <sys/file.h> /* For access() */
extern int getpid(), access(); /* Syscalls */
#if SYS_T20+SYS_10X
#define TMPFMT(pid, ver) "TMPNAM-%o.TMP.%d", pid, ver
#else /* nnnCmm.TMP */
#define TMPFMT(pid, ver) "%03.3dC%02.02d.TMP", pid, (ver)%100
/* can only have 100 unique names... */
#undef TMP_MAX
#define TMP_MAX 100
#endif
char *
tmpnam(s)
char *s;
{
static char tmpbuf[L_tmpnam]; /* storage for filename */
static int newver = 0;
int i;
int pid = getpid();
if (!s) s = tmpbuf; /* if they didn't supply a buf, use ours */
if (newver == 0)
newver = (int)time(0) & 0777; /* Get random non-neg start seed */
for (i = 0; i < TMP_MAX; ++i) { /* Avoid infinite loop */
sprintf(s, TMPFMT(pid, ++newver)); /* Make a filename */
if (access(s, F_OK) != 0) /* Check for existence */
return s; /* Doesn't exist, win. */
}
return NULL; /* Failed... */
}
#endif /* T20+10X+T10+CSI+WAITS+ITS */