Bug 99 - Does not work properly on EBCDIC (instead of ASCII) mainframes with OS/390. I changed priv_mktemp.c
Does not work properly on EBCDIC (instead of ASCII) mainframes with OS/390. I...
Status: RESOLVED FIXED
Product: Mktemp
Classification: Unclassified
Component: Mktemp
1.4
IBM Other
: normal normal
Assigned To: Todd C. Miller
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2003-02-11 07:51 MST by Dirk Schwartzkopff
Modified: 2003-04-01 11:12 MST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dirk Schwartzkopff 2003-02-11 07:51:41 MST
/*	$OpenBSD: mktemp.c,v 1.5 1998/06/21 22:14:00 millert Exp $	*/

/*
 * Copyright (c) 1987, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "config.h"

#ifndef lint
static const char rcsid[] = "$Id: priv_mktemp.c,v 1.3 2001/10/03 02:12:14 
millert Exp $";
#endif /* not lint */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#include <ctype.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */

#include <extern.h>

static int _gettemp __P((char *, int *, int, int));

#if 0
int
mkstemps(path, slen)
	char *path;
	int slen;
{
	int fd;

	return (_gettemp(path, &fd, 0, slen) ? fd : -1);
}
#endif

int
priv_mkstemp(path)
	char *path;
{
	int fd;

	return (_gettemp(path, &fd, 0, 0) ? fd : -1);
}

char *
priv_mkdtemp(path)
	char *path;
{
	return(_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL);
}

static int
_gettemp(path, doopen, domkdir, slen)
	char *path;
	register int *doopen;
	int domkdir;
	int slen;
{
	register char *start, *trv, *suffp;
	struct stat sbuf;
	int pid, rval;
	char alphabet[] = 
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

	if (doopen && domkdir) {
		errno = EINVAL;
		return(0);
	}

	for (trv = path; *trv; ++trv)
		;
	trv -= slen;
	suffp = trv;
	--trv;
	if (trv < path) {
		errno = EINVAL;
		return (0);
	}
	pid = getpid();
	while (*trv == 'X' && pid != 0) {
		*trv-- = (pid % 10) + '0';
		pid /= 10;
	}
	while (*trv == 'X') {
		char c;

		pid = (get_random() & 0xffff) % (26+26);
		c = alphabet[pid];
		*trv-- = c;
	}
	start = trv + 1;

	/*
	 * check the target directory; if you have six X's and it
	 * doesn't exist this runs for a *very* long time.
	 */
	if (doopen || domkdir) {
		for (;; --trv) {
			if (trv <= path)
				break;
			if (*trv == '/') {
				*trv = '\0';
				rval = stat(path, &sbuf);
				*trv = '/';
				if (rval != 0)
					return(0);
				if (!S_ISDIR(sbuf.st_mode)) {
					errno = ENOTDIR;
					return(0);
				}
				break;
			}
		}
	}

	for (;;) {
		if (doopen) {
			if ((*doopen =
			    open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
				return(1);
			if (errno != EEXIST)
				return(0);
		} else if (domkdir) {
			if (mkdir(path, 0700) == 0)
				return(1);
			if (errno != EEXIST)
				return(0);
		} else if (lstat(path, &sbuf))
			return(errno == ENOENT ? 1 : 0);

		/* tricky little algorithm for backward compatibility */
		for (trv = start;;) {
             		int i;
			if (!*trv)
				return (0);
			if (*trv == 'Z') {
				if (trv == suffp)
					return (0);
				*trv++ = 'a';
			} else {
				if (isdigit((unsigned char)(*trv)))
					*trv = 'a';
				else if (*trv == 'z')	/* inc from z to A */
					*trv = 'A';
				else {
					if (trv == suffp)
						return (0);
					for (i=0;(char)*trv != alphabet[i];i++);
					*trv = alphabet[i+1];                                    

				}
				break;
			}
		}
	}
	/*NOTREACHED*/
}
Comment 1 Dirk Schwartzkopff 2003-02-11 07:56:17 MST
changes:
char alphabet[] = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

c = alphabet[pid];

for (i=0;(char)*trv != alphabet[i];i++);
*trv = alphabet[i+1];

The reason is: In ASCII (for example) a+1=b and i+1=j but in EBCDIC a+1=b 
and i+8=j
Comment 2 Todd C. Miller 2003-03-22 14:52:20 MST
Is there some #define I can test for to know if I am on an EBCDIC machine?
Comment 3 Todd C. Miller 2003-03-23 18:18:32 MST
I've added a configure test for EBCDIC and support in mktemp(1) that doesn't require the traversal of alphabet[].  Can you try ftp://ftp.mktemp.org/pub/mktemp/beta/mktemp-1.5rc1.tar.gz and verify that it does the right thing on your system?
Comment 4 Dirk Schwartzkopff 2003-04-01 07:12:21 MST
Works great on OS/390 2.10 and z/OS 1.4
Thanks