Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rename - rename a file |
| 3 | */ |
| 4 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5 | /* Copyright (C) 1999 Free Software Foundation, Inc. |
| 6 | |
| 7 | This file is part of GNU Bash, the Bourne Again SHell. |
| 8 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 9 | Bash is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 3 of the License, or |
| 12 | (at your option) any later version. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 13 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 14 | Bash is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 18 | |
| 19 | You should have received a copy of the GNU General Public License |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 20 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 22 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 23 | #include <config.h> |
| 24 | |
| 25 | #if !defined (HAVE_RENAME) |
| 26 | |
| 27 | #include <bashtypes.h> |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 28 | #include <posixstat.h> |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 29 | |
| 30 | #if defined (HAVE_UNISTD_H) |
| 31 | # include <unistd.h> |
| 32 | #endif |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 33 | #include <errno.h> |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 34 | |
| 35 | #include <stdc.h> |
| 36 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 37 | #ifndef errno |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 38 | #include <errno.h> |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 41 | int |
| 42 | rename (from, to) |
| 43 | const char *from, *to; |
| 44 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 45 | struct stat fb, tb; |
| 46 | |
| 47 | if (stat (from, &fb) < 0) |
| 48 | return -1; |
| 49 | |
| 50 | if (stat (to, &tb) < 0) |
| 51 | { |
| 52 | if (errno != ENOENT) |
| 53 | return -1; |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | if (fb.st_dev == tb.st_dev && fb.st_ino == tb.st_ino) |
| 58 | return 0; /* same file */ |
| 59 | if (unlink (to) < 0 && errno != ENOENT) |
| 60 | return -1; |
| 61 | } |
| 62 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 63 | if (link (from, to) < 0) |
| 64 | return (-1); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 65 | |
| 66 | if (unlink (from) < 0 && errno != ENOENT) |
| 67 | { |
| 68 | int e = errno; |
| 69 | unlink (to); |
| 70 | errno = e; |
| 71 | return (-1); |
| 72 | } |
| 73 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 74 | return (0); |
| 75 | } |
| 76 | #endif /* !HAVE_RENAME */ |