/* mkstemp librock_CHISEL _summary Correct temporary file creation Copyright (c) 2001-2002, Forrest J. Cavalier III, doing business as Mib Software, Saylorsburg Pennsylvania USA Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 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. Neither the name of the author 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 COPYRIGHT HOLDERS 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. */ #ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_mkstemp #include #include #include #include #include #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_mkstemp /* License description features are documented at http://www.mibsoftware.com/librock/ */ /**************************************************************/ #include /* librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ void *librock_LIDESC_mkstemp[] = { "\n" __FILE__ librock_LIDESC_librock "\n", 0 }; /**************************************************************/ #endif #endif #ifndef librock_WRAP /* Function wrapping and tracing features are documented at http://www.mibsoftware.com/librock/ */ /**************************************************************/ #define librock_body_mkstemp librock_mkstemp /* For librock_WRAP section */ /**************************************************************/ #endif #ifndef librock_NOIMPL_mkstemp /**************************************************************/ int librock_body_mkstemp(char *pszTemplate) {/* Copyright 1998-2002, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_mkstemp /* librock_mkstemp - Create a named temporary file. */ /**/ #include int librock_mkstemp( char *pszTemplate); /* Create and open a temporary file, being careful never to clobber existing files. This uses mktemp() and open() correctly to avoid a race condition. (If you do not need the name, consider using tmpfile() instead.) pszTemplate should end with 'XXXXXX'. It is modified to be the filename on successful return. IMPORTANT: Be sure to strcpy a new template with each call. (Follow the example below.) Typical use is */ char buf[20]; strcpy(buf,"aaXXXXXX"); fd = mkstemp(buf); if (fd < 0) { /* Error! */ } /* mktemp() open() malloc() free() strcpy() strlen() */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ int fd; char *path = malloc(strlen(pszTemplate)+1); if (!path) { return -1; } do { strcpy(path,pszTemplate); if (!mktemp(path)) { free(path); return -1; } fd = open(path, O_CREAT|O_EXCL|O_RDWR|librock_O_BINARY, 0600); } while (fd < 0); strcpy(pszTemplate,path); free(path); return fd; } /**************************************************************/ #endif /* NOIMP section */ /* $Log: mkstemp.c,v $ Revision 1.4 2002/08/01 20:18:53 forrest@mibsoftware.com rights=#1 Moved CVS log to end. Changed LIDESC MD5 to HC. Revision 1.3 2002/04/09 03:41:28 forrest@mibsoftware.com rights=#1 Updated LICENSE in manual. Revision 1.2 2002/03/16 22:16:00 forrest@mibsoftware.com rights=#1 Fixed WRAP section Revision 1.1 2002/03/16 22:15:12 forrest@mibsoftware.com rights=#1 Initial rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */