| /* 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 <sys/time.h> |
| #include <windows.h> |
| #include <string.h> |
| #include <errno.h> |
| #include <shared.h> |
| |
| #if _WCE >= 3 |
| void GetCurrentFT(FILETIME *); |
| #endif |
| |
| int utimes(const char *filename, const struct timeval *times) { |
| if(!filename) { |
| errno = EFAULT; |
| return -1; |
| } |
| if(!*filename) { |
| errno = ENOENT; |
| return -1; |
| } |
| |
| FILETIME ft[2]; |
| if(times) { |
| __timeval_to_filetime(times, ft); |
| __timeval_to_filetime(times + 1, ft + 1); |
| } else { |
| #if _WCE >= 3 |
| GetCurrentFT(ft); |
| #else |
| SYSTEMTIME st; |
| GetSystemTime(&st); |
| if(!SystemTimeToFileTime(&st, ft)) return -1; |
| #endif |
| ft[1] = (FILETIME)ft[0]; |
| } |
| |
| size_t len = strlen(filename) + 1; |
| wchar_t wfile[len]; |
| mbstowcs(wfile, filename, len); |
| void *fh = CreateFileW(wfile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| if(fh == (void *)-1) return -1; |
| int r = SetFileTime(fh, NULL, ft, ft + 1); |
| if(!CloseHandle(fh)) return -1; |
| return r; |
| } |