1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T 29 * All Rights Reserved 30 * 31 * Portions of this source code were derived from Berkeley 32 * 4.3 BSD under license from the regents of the University of 33 * California. 34 */ 35 36 #pragma ident "%Z%%M% %I% %E% SMI" 37 38 #include <sys/feature_tests.h> 39 40 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64 41 #define mkstemp mkstemp64 42 #define mkstemps mkstemps64 43 #define libc_mkstemps libc_mkstemps64 /* prefer unique statics */ 44 #pragma weak _mkstemp64 = mkstemp64 45 #else 46 #pragma weak _mkstemp = mkstemp 47 #endif 48 49 #include "lint.h" 50 #include <sys/fcntl.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <errno.h> 54 #include <alloca.h> 55 #include <sys/types.h> 56 #include <sys/stat.h> 57 #include <fcntl.h> 58 59 extern char *libc_mktemps(char *, int); 60 61 static int 62 libc_mkstemps(char *as, int slen) 63 { 64 int fd; 65 int len; 66 char *tstr, *str, *mkret; 67 68 if (as == NULL || *as == NULL) 69 return (-1); 70 71 len = (int)strlen(as); 72 tstr = alloca(len + 1); 73 (void) strcpy(tstr, as); 74 75 if (slen < 0 || slen >= len) 76 return (-1); 77 78 str = tstr + (len - 1 - slen); 79 80 /* 81 * The following for() loop is doing work. mktemp() will generate 82 * a different name each time through the loop. So if the first 83 * name is used then keep trying until you find a free filename. 84 */ 85 86 for (;;) { 87 if (*str == 'X') { /* If no trailing X's don't call mktemp. */ 88 mkret = libc_mktemps(as, slen); 89 if (*mkret == '\0') { 90 return (-1); 91 } 92 } 93 #if _FILE_OFFSET_BITS == 64 94 if ((fd = open64(as, O_CREAT|O_EXCL|O_RDWR, 0600)) != -1) { 95 return (fd); 96 } 97 #else 98 if ((fd = open(as, O_CREAT|O_EXCL|O_RDWR, 0600)) != -1) { 99 return (fd); 100 } 101 #endif /* _FILE_OFFSET_BITS == 64 */ 102 103 /* 104 * If the error condition is other than EEXIST or if the 105 * file exists and there are no X's in the string 106 * return -1. 107 */ 108 109 if ((errno != EEXIST) || (*str != 'X')) { 110 return (-1); 111 } 112 (void) strcpy(as, tstr); 113 } 114 } 115 116 int 117 mkstemp(char *as) 118 { 119 return (libc_mkstemps(as, 0)); 120 } 121 122 int 123 mkstemps(char *as, int slen) 124 { 125 return (libc_mkstemps(as, slen)); 126 } 127