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 5*1475Scasper * Common Development and Distribution License (the "License"). 6*1475Scasper * 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 */ 210Sstevel@tonic-gate /* 22*1475Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate * 250Sstevel@tonic-gate * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T 260Sstevel@tonic-gate * All Rights Reserved 270Sstevel@tonic-gate * 280Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 290Sstevel@tonic-gate * 4.3 BSD under license from the regents of the University of 300Sstevel@tonic-gate * California. 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include <sys/feature_tests.h> 360Sstevel@tonic-gate 370Sstevel@tonic-gate #if !defined(_LP64) && _FILE_OFFSET_BITS == 64 380Sstevel@tonic-gate #pragma weak mkstemp64 = _mkstemp64 39*1475Scasper #pragma weak mkstemps64 = _mkstemps64 400Sstevel@tonic-gate #define _mkstemp _mkstemp64 41*1475Scasper #define _mkstemps _mkstemps64 42*1475Scasper #define libc_mkstemps libc_mkstemps64 /* prefer unique statics */ 430Sstevel@tonic-gate #else 440Sstevel@tonic-gate #pragma weak mkstemp = _mkstemp 45*1475Scasper #pragma weak mkstemps = _mkstemps 460Sstevel@tonic-gate #endif 470Sstevel@tonic-gate 480Sstevel@tonic-gate #include "synonyms.h" 490Sstevel@tonic-gate #include <sys/fcntl.h> 500Sstevel@tonic-gate #include <stdlib.h> 510Sstevel@tonic-gate #include <string.h> 520Sstevel@tonic-gate #include <errno.h> 530Sstevel@tonic-gate #include <alloca.h> 540Sstevel@tonic-gate #include <sys/types.h> 550Sstevel@tonic-gate #include <sys/stat.h> 560Sstevel@tonic-gate #include <fcntl.h> 570Sstevel@tonic-gate 58*1475Scasper extern char *libc_mktemps(char *, int); 590Sstevel@tonic-gate 60*1475Scasper static int 61*1475Scasper libc_mkstemps(char *as, int slen) 620Sstevel@tonic-gate { 630Sstevel@tonic-gate int fd; 64*1475Scasper int len; 650Sstevel@tonic-gate char *tstr, *str, *mkret; 660Sstevel@tonic-gate 670Sstevel@tonic-gate if (as == NULL || *as == NULL) 680Sstevel@tonic-gate return (-1); 690Sstevel@tonic-gate 70*1475Scasper len = (int)strlen(as); 71*1475Scasper tstr = alloca(len + 1); 720Sstevel@tonic-gate (void) strcpy(tstr, as); 730Sstevel@tonic-gate 74*1475Scasper if (slen < 0 || slen >= len) 75*1475Scasper return (-1); 76*1475Scasper 77*1475Scasper str = tstr + (len - 1 - slen); 780Sstevel@tonic-gate 790Sstevel@tonic-gate /* 800Sstevel@tonic-gate * The following for() loop is doing work. mktemp() will generate 810Sstevel@tonic-gate * a different name each time through the loop. So if the first 820Sstevel@tonic-gate * name is used then keep trying until you find a free filename. 830Sstevel@tonic-gate */ 840Sstevel@tonic-gate 850Sstevel@tonic-gate for (; ; ) { 860Sstevel@tonic-gate if (*str == 'X') { /* If no trailing X's don't call mktemp. */ 87*1475Scasper mkret = libc_mktemps(as, slen); 880Sstevel@tonic-gate if (*mkret == '\0') { 890Sstevel@tonic-gate return (-1); 900Sstevel@tonic-gate } 910Sstevel@tonic-gate } 920Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64 930Sstevel@tonic-gate if ((fd = open64(as, O_CREAT|O_EXCL|O_RDWR, 0600)) != -1) { 940Sstevel@tonic-gate return (fd); 950Sstevel@tonic-gate } 960Sstevel@tonic-gate #else 970Sstevel@tonic-gate if ((fd = open(as, O_CREAT|O_EXCL|O_RDWR, 0600)) != -1) { 980Sstevel@tonic-gate return (fd); 990Sstevel@tonic-gate } 1000Sstevel@tonic-gate #endif /* _FILE_OFFSET_BITS == 64 */ 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate /* 1030Sstevel@tonic-gate * If the error condition is other than EEXIST or if the 1040Sstevel@tonic-gate * file exists and there are no X's in the string 1050Sstevel@tonic-gate * return -1. 1060Sstevel@tonic-gate */ 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate if ((errno != EEXIST) || (*str != 'X')) { 1090Sstevel@tonic-gate return (-1); 1100Sstevel@tonic-gate } 1110Sstevel@tonic-gate (void) strcpy(as, tstr); 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate } 114*1475Scasper 115*1475Scasper int 116*1475Scasper _mkstemp(char *as) 117*1475Scasper { 118*1475Scasper return (libc_mkstemps(as, 0)); 119*1475Scasper } 120*1475Scasper 121*1475Scasper int 122*1475Scasper _mkstemps(char *as, int slen) 123*1475Scasper { 124*1475Scasper return (libc_mkstemps(as, slen)); 125*1475Scasper } 126