xref: /onnv-gate/usr/src/lib/libc/port/gen/mkstemp.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51475Scasper  * Common Development and Distribution License (the "License").
61475Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
25*6812Sraf  */
26*6812Sraf 
27*6812Sraf /*
280Sstevel@tonic-gate  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
290Sstevel@tonic-gate  * All Rights Reserved
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
320Sstevel@tonic-gate  * 4.3 BSD under license from the regents of the University of
330Sstevel@tonic-gate  * California.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <sys/feature_tests.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
41*6812Sraf #define	mkstemp		mkstemp64
42*6812Sraf #define	mkstemps	mkstemps64
431475Scasper #define	libc_mkstemps	libc_mkstemps64		/* prefer unique statics */
44*6812Sraf #pragma weak _mkstemp64 = mkstemp64
450Sstevel@tonic-gate #else
46*6812Sraf #pragma weak _mkstemp = mkstemp
470Sstevel@tonic-gate #endif
480Sstevel@tonic-gate 
49*6812Sraf #include "lint.h"
500Sstevel@tonic-gate #include <sys/fcntl.h>
510Sstevel@tonic-gate #include <stdlib.h>
520Sstevel@tonic-gate #include <string.h>
530Sstevel@tonic-gate #include <errno.h>
540Sstevel@tonic-gate #include <alloca.h>
550Sstevel@tonic-gate #include <sys/types.h>
560Sstevel@tonic-gate #include <sys/stat.h>
570Sstevel@tonic-gate #include <fcntl.h>
580Sstevel@tonic-gate 
591475Scasper extern char *libc_mktemps(char *, int);
600Sstevel@tonic-gate 
611475Scasper static int
libc_mkstemps(char * as,int slen)621475Scasper libc_mkstemps(char *as, int slen)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	int	fd;
651475Scasper 	int	len;
660Sstevel@tonic-gate 	char	*tstr, *str, *mkret;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	if (as == NULL || *as == NULL)
690Sstevel@tonic-gate 		return (-1);
700Sstevel@tonic-gate 
711475Scasper 	len = (int)strlen(as);
721475Scasper 	tstr = alloca(len + 1);
730Sstevel@tonic-gate 	(void) strcpy(tstr, as);
740Sstevel@tonic-gate 
751475Scasper 	if (slen < 0 || slen >= len)
761475Scasper 		return (-1);
771475Scasper 
781475Scasper 	str = tstr + (len - 1 - slen);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	/*
810Sstevel@tonic-gate 	 * The following for() loop is doing work.  mktemp() will generate
820Sstevel@tonic-gate 	 * a different name each time through the loop.  So if the first
830Sstevel@tonic-gate 	 * name is used then keep trying until you find a free filename.
840Sstevel@tonic-gate 	 */
850Sstevel@tonic-gate 
86*6812Sraf 	for (;;) {
870Sstevel@tonic-gate 		if (*str == 'X') { /* If no trailing X's don't call mktemp. */
881475Scasper 			mkret = libc_mktemps(as, slen);
890Sstevel@tonic-gate 			if (*mkret == '\0') {
900Sstevel@tonic-gate 				return (-1);
910Sstevel@tonic-gate 			}
920Sstevel@tonic-gate 		}
930Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
940Sstevel@tonic-gate 		if ((fd = open64(as, O_CREAT|O_EXCL|O_RDWR, 0600)) != -1) {
950Sstevel@tonic-gate 			return (fd);
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate #else
980Sstevel@tonic-gate 		if ((fd = open(as, O_CREAT|O_EXCL|O_RDWR, 0600)) != -1) {
990Sstevel@tonic-gate 			return (fd);
1000Sstevel@tonic-gate 		}
1010Sstevel@tonic-gate #endif  /* _FILE_OFFSET_BITS == 64 */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 		/*
1040Sstevel@tonic-gate 		 * If the error condition is other than EEXIST or if the
1050Sstevel@tonic-gate 		 * file exists and there are no X's in the string
1060Sstevel@tonic-gate 		 * return -1.
1070Sstevel@tonic-gate 		 */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 		if ((errno != EEXIST) || (*str != 'X')) {
1100Sstevel@tonic-gate 			return (-1);
1110Sstevel@tonic-gate 		}
1120Sstevel@tonic-gate 		(void) strcpy(as, tstr);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate }
1151475Scasper 
1161475Scasper int
mkstemp(char * as)117*6812Sraf mkstemp(char *as)
1181475Scasper {
1191475Scasper 	return (libc_mkstemps(as, 0));
1201475Scasper }
1211475Scasper 
1221475Scasper int
mkstemps(char * as,int slen)123*6812Sraf mkstemps(char *as, int slen)
1241475Scasper {
1251475Scasper 	return (libc_mkstemps(as, slen));
1261475Scasper }
127