/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/*
* mktemp() expects a string with up to six trailing 'X's.
* These will be overlaid with letters, digits and symbols from
* the portable filename character set. If every combination thus
* inserted leads to an existing file name, the string is shortened
* to length zero and a pointer to a null string is returned.
*
* The guarantee made by mktime() to the caller is that the
* generated file name string will not match the string
* produced by any other concurrent process using mktemp().
* To guarantee uniqueness across the process-id space,
* the process-id of the caller is encoded into the string.
* To allow repeated calls within the same process to generate
* different strings on each call, a sequence number is encoded
* into the string along with process-id.
*
* The encoding is performed using radix-64 (6 bits per character),
* with 64 characters taken from the portable file name character set.
* This allows the six X's to be a representation of a 36-bit integer
* composed of bit fields:
* ( pid | seq )
* where the process-id occupies the high-order bits and the sequence
* number occupies the low-order bits. The size of the pid field is
* not fixed at the traditional 15 bits (MAXPID = 30000); the system
* now allows a larger process-id space and MAXPID is obtained from
* the system with a call to sysconf(_SC_MAXPID).
*
* mktime() should fail if fewer than six X's are presented to it.
* However, this has been traditionally accepted and is preserved
* in the present code. The consequence is that the 36-bit integer
* is reduced to a (6*N)-bit integer, where N is the number of X's.
* mktime() fails immediately if the resulting integer is not large
* enough to contain MAXPID.
*
* In an attempt to confuse and thwart hackers, the starting
* sequence number is randomized using the current time.
*/
#include "lint.h"
#include "mtlib.h"
#include <string.h>
#include <unistd.h>
#include <thread.h>
#include <synch.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
/*
* 64-bit digits, must be from the POSIX "portable file name character set".
*/
static char
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_',
};
char *
{
/* statics are protected by this static mutex */
static int pidshift = 0;
static int previous_try = 0;
int try;
int tryshift;
int max_try;
char *s;
char *first_x;
int len;
return (as); /* a pointer to a null string is returned. */
/*
* Randomize the starting sequence number in
* an attempt to confuse and thwart hackers.
* Use the low 12 bits of the time in milliseconds.
*/
previous_pid = pid;
}
/* for all possible values of pid, 0 <= pid < (1 << pidshift) */
if (pidshift == 0) /* one-time initialization */
/* count the X's */
xcnt = 0;
goto fail;
xcnt++;
len--;
--s;
}
/* fail if we don't have enough X's to represent MAXPID */
/*
* Some broken programs call mktemp() repeatedly,
* passing the same string without reinserting the X's.
* Check to see if this is such a call by testing
* the trailing characters of the string for a
* match with the process-id.
*/
int c;
int i;
c = *--s;
for (i = 0; i < 64; i++)
if (c == chars[i])
break;
if (i == 64)
goto fail;
}
return (as);
}
goto fail;
}
/* we can try sequence numbers in the range 0 <= try < max_try */
if (previous_try >= max_try)
previous_try = 0;
try = previous_try;
for (;;) {
/* num is up to a 36-bit integer ... */
int i;
/* ... which we represent backwards in base 64 */
num >>= 6;
}
break; /* unrecoverable error */
/* remember where we left off for the next call */
return (as);
}
try = 0;
if (try == previous_try)
break;
}
fail:
*as = '\0';
return (as);
}
char *
{
return (libc_mktemps(template, 0));
}