blob: 74cb4cf7bb7479b07aa1a5f020dcaa9f1b727c19 [file] [log] [blame] [raw]
/* A part of the Windows CE C Extra Library (celibc)
Copyright 2007-2015 PC GO Ld.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include <string.h>
#include <unistd.h>
#include <windows.h>
#ifndef FILE_ATTRIBUTE_INROM
#define FILE_ATTRIBUTE_INROM FILE_ATTRIBUTE_DEVICE
#endif
#ifndef FILE_ATTRIBUTE_ROMMODULE
#define FILE_ATTRIBUTE_ROMMODULE FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
#endif
int rename(const char *oldfile, const char *newfile) {
size_t oldfile_len = strlen(oldfile) + 1;
size_t newfile_len = strlen(newfile) + 1;
wchar_t woldfile[oldfile_len];
wchar_t wnewfile[newfile_len];
mbstowcs(woldfile, oldfile, oldfile_len);
mbstowcs(wnewfile, newfile, newfile_len);
unsigned long int attrib = GetFileAttributes(wnewfile);
if(attrib != (unsigned long int)-1 && !(attrib & FILE_ATTRIBUTE_DIRECTORY) &&
!(attrib & FILE_ATTRIBUTE_INROM) && !(attrib & FILE_ATTRIBUTE_ROMMODULE) && unlink(newfile) < 0) return -1;
if(!MoveFileW(woldfile, wnewfile)) return -1;
return 0;
}