* New version 2.21.999
[alpine.git] / pith / osdep / rename.c
blobda940ad84b250e89f677445f0a96ef98b347f6b6
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: rename.c 761 2007-10-23 22:35:18Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2013-2018 Eduardo Chappa
8 * Copyright 2006 University of Washington
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include <system.h>
20 #include "err_desc.h"
21 #include "../charconv/utf8.h"
22 #include "../charconv/filesys.h"
23 #include "rename.h"
26 /*----------------------------------------------------------------------
27 Rename a file
29 Args: tmpfname -- Old name of file
30 fname -- New name of file
32 Result: File is renamed. Returns 0 on success, else -1 on error
33 and errno is valid.
34 ----*/
35 int
36 rename_file(char *tmpfname, char *fname)
38 #if HAVE_RENAME
39 return(our_rename(tmpfname, fname));
40 #else
41 # if defined(_WINDOWS)
42 int ret;
45 * DOS rename doesn't unlink destination for us...
47 if((ret = our_unlink(fname)) && (errno == EPERM)){
48 ret = -5;
50 else{
51 ret = our_rename(tmpfname, fname);
52 if(ret)
53 ret = -1;
56 return(ret);
57 # else
58 int status;
60 our_unlink(fname);
61 if ((status = link(tmpfname, fname)) != 0)
62 return(status);
64 our_unlink(tmpfname);
65 return(0);
66 # endif
67 #endif