/* file/renover.c version librock_CHISEL _summary Rename a file, removing existing file 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_renover #include #include #include #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_renover /* License description features are documented at http://www.mibsoftware.com/librock/ */ /**************************************************************/ #include /* librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ void *librock_LIDESC_renover[] = { "\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_RenameOver librock_RenameOver /* For librock_WRAP section */ /**************************************************************/ #endif #ifndef librock_NOIMPL_RenameOver /**************************************************************/ const char *librock_body_RenameOver(const char *tempname,const char *newname) {/* 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_RenameOver /* librock_RenameOver - rename a file, even when file exists. */ /**/ #include const char * librock_body_RenameOver( const char *tempname, const char *newname); /* This function exists for portable operation on Win32 and Unix. On Win32, the standard library rename() will not rename over the top of existing files. (This function removes existing files if necessary.) On Win32, if you have the tempname file open, you must close the file first, or the operation will fail. */ /* access(),strcmp(),unlink(),rename() */ /* 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 */ /*-------------------------------------------*/ if (rename(tempname,newname)) { /* If the OS operation failed, try to find out why. */ if (!strcmp(tempname,newname)) { return "E-1-Names are the same"; } if (access(tempname,0)) { return "E-2-tempname does not exist"; } if (!access(newname,0)) { /* newname exists. On Win32, we have to remove the old first */ /* Could rename the old to a temp name, and then we wouldn't have to do the unlink. */ if (_unlink(newname)) { return "E-3-could not remove old file"; } if (rename(tempname,newname)) { /* MAJOR ERROR! newname was deleted! */ return "E-4-rename failed"; } } else { if (rename(tempname,newname)) { return "E-5-rename failed"; } } } return 0; } /**************************************************************/ #endif /* NOIMP section */ /* $Log: renover.c,v $ Revision 1.6 2002/08/01 20:16:43 forrest@mibsoftware.com rights=#1 Moved CVS log to end. Changed LIDESC MD5 to HC. Revision 1.5 2002/04/09 03:41:28 forrest@mibsoftware.com rights=#1 Updated LICENSE in manual. Revision 1.4 2002/02/10 02:25:02 forrest@mibsoftware.com rights=#1 Coding standards. Standardized chg log. Fixed lidesc. Improved man page Revision 1.3 2002/01/30 16:08:04 forrest@mibsoftware.com rights=#1 Renamed some .h files Revision 1.2 2002/01/29 20:18:12 forrest@mibsoftware.com rights=#1 Use access() instead of stat() for portability Revision 1.1 2002/01/29 14:31:50 forrest@mibsoftware.com rights=#1 Moved rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */