17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c4dcc55Scasper * Common Development and Distribution License (the "License"). 67c4dcc55Scasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217257d1b4Sraf 22*5dbfd19aSTheo Schlossnagle /* Copyright (c) 2013 OmniTI Computer Consulting, Inc. All rights reserved. */ 23*5dbfd19aSTheo Schlossnagle 247c478bd9Sstevel@tonic-gate /* 257257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 267c478bd9Sstevel@tonic-gate * Use is subject to license terms. 277257d1b4Sraf */ 287257d1b4Sraf 297257d1b4Sraf /* 307c478bd9Sstevel@tonic-gate * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T 317c478bd9Sstevel@tonic-gate * All Rights Reserved 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 347c478bd9Sstevel@tonic-gate * 4.3 BSD under license from the regents of the University of 357c478bd9Sstevel@tonic-gate * California. 367c478bd9Sstevel@tonic-gate */ 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #include <sys/feature_tests.h> 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #if !defined(_LP64) && _FILE_OFFSET_BITS == 64 417257d1b4Sraf #define mkstemp mkstemp64 427257d1b4Sraf #define mkstemps mkstemps64 43*5dbfd19aSTheo Schlossnagle #define mkostemp mkostemp64 44*5dbfd19aSTheo Schlossnagle #define mkostemps mkostemps64 457c4dcc55Scasper #define libc_mkstemps libc_mkstemps64 /* prefer unique statics */ 467257d1b4Sraf #pragma weak _mkstemp64 = mkstemp64 477c478bd9Sstevel@tonic-gate #else 487257d1b4Sraf #pragma weak _mkstemp = mkstemp 497c478bd9Sstevel@tonic-gate #endif 507c478bd9Sstevel@tonic-gate 517257d1b4Sraf #include "lint.h" 527c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 537c478bd9Sstevel@tonic-gate #include <stdlib.h> 547c478bd9Sstevel@tonic-gate #include <string.h> 557c478bd9Sstevel@tonic-gate #include <errno.h> 567c478bd9Sstevel@tonic-gate #include <alloca.h> 577c478bd9Sstevel@tonic-gate #include <sys/types.h> 587c478bd9Sstevel@tonic-gate #include <sys/stat.h> 597c478bd9Sstevel@tonic-gate #include <fcntl.h> 607c478bd9Sstevel@tonic-gate 617c4dcc55Scasper extern char *libc_mktemps(char *, int); 627c478bd9Sstevel@tonic-gate 637c4dcc55Scasper static int 64*5dbfd19aSTheo Schlossnagle libc_mkstemps(char *as, int slen, int flags) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate int fd; 677c4dcc55Scasper int len; 687c478bd9Sstevel@tonic-gate char *tstr, *str, *mkret; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate if (as == NULL || *as == NULL) 717c478bd9Sstevel@tonic-gate return (-1); 727c478bd9Sstevel@tonic-gate 737c4dcc55Scasper len = (int)strlen(as); 747c4dcc55Scasper tstr = alloca(len + 1); 757c478bd9Sstevel@tonic-gate (void) strcpy(tstr, as); 767c478bd9Sstevel@tonic-gate 777c4dcc55Scasper if (slen < 0 || slen >= len) 787c4dcc55Scasper return (-1); 797c4dcc55Scasper 807c4dcc55Scasper str = tstr + (len - 1 - slen); 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate /* 837c478bd9Sstevel@tonic-gate * The following for() loop is doing work. mktemp() will generate 847c478bd9Sstevel@tonic-gate * a different name each time through the loop. So if the first 857c478bd9Sstevel@tonic-gate * name is used then keep trying until you find a free filename. 867c478bd9Sstevel@tonic-gate */ 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate for (;;) { 897c478bd9Sstevel@tonic-gate if (*str == 'X') { /* If no trailing X's don't call mktemp. */ 907c4dcc55Scasper mkret = libc_mktemps(as, slen); 917c478bd9Sstevel@tonic-gate if (*mkret == '\0') { 927c478bd9Sstevel@tonic-gate return (-1); 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64 96*5dbfd19aSTheo Schlossnagle if ((fd = open64(as, O_CREAT|O_EXCL|O_RDWR|flags, 97*5dbfd19aSTheo Schlossnagle 0600)) != -1) { 987c478bd9Sstevel@tonic-gate return (fd); 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate #else 101*5dbfd19aSTheo Schlossnagle if ((fd = open(as, O_CREAT|O_EXCL|O_RDWR|flags, 102*5dbfd19aSTheo Schlossnagle 0600)) != -1) { 1037c478bd9Sstevel@tonic-gate return (fd); 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate #endif /* _FILE_OFFSET_BITS == 64 */ 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * If the error condition is other than EEXIST or if the 1097c478bd9Sstevel@tonic-gate * file exists and there are no X's in the string 1107c478bd9Sstevel@tonic-gate * return -1. 1117c478bd9Sstevel@tonic-gate */ 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if ((errno != EEXIST) || (*str != 'X')) { 1147c478bd9Sstevel@tonic-gate return (-1); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate (void) strcpy(as, tstr); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate } 1197c4dcc55Scasper 1207c4dcc55Scasper int 1217257d1b4Sraf mkstemp(char *as) 1227c4dcc55Scasper { 123*5dbfd19aSTheo Schlossnagle return (libc_mkstemps(as, 0, 0)); 1247c4dcc55Scasper } 1257c4dcc55Scasper 1267c4dcc55Scasper int 1277257d1b4Sraf mkstemps(char *as, int slen) 1287c4dcc55Scasper { 129*5dbfd19aSTheo Schlossnagle return (libc_mkstemps(as, slen, 0)); 130*5dbfd19aSTheo Schlossnagle } 131*5dbfd19aSTheo Schlossnagle 132*5dbfd19aSTheo Schlossnagle int 133*5dbfd19aSTheo Schlossnagle mkostemp(char *as, int flags) 134*5dbfd19aSTheo Schlossnagle { 135*5dbfd19aSTheo Schlossnagle return (libc_mkstemps(as, 0, flags)); 136*5dbfd19aSTheo Schlossnagle } 137*5dbfd19aSTheo Schlossnagle 138*5dbfd19aSTheo Schlossnagle int 139*5dbfd19aSTheo Schlossnagle mkostemps(char *as, int slen, int flags) 140*5dbfd19aSTheo Schlossnagle { 141*5dbfd19aSTheo Schlossnagle return (libc_mkstemps(as, slen, flags)); 1427c4dcc55Scasper } 143