blob: f6d1578949ed0782a2f59e1e8bd9bb7c018473f2 [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 "stat.h"
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <shared.h>
#if _WCE >= 3
void GetCurrentFT(FILETIME *);
#endif
/*
Change the access time of FILE to TSP[0] and
the modification time of FILE to TSP[1].
*/
int futimens(int fd, const struct timespec *tsp) {
if(!tsp) {
errno = EACCES;
return -1;
}
FILETIME ft[2], *atime = NULL, *mtime = NULL;
if(tsp[0].tv_nsec != UTIME_OMIT) {
atime = ft;
if(tsp[0].tv_nsec == UTIME_NOW) {
#if _WCE >= 3
GetCurrentFT(atime);
#else
SYSTEMTIME st;
GetSystemTime(&st);
if(!SystemTimeToFileTime(&st, atime)) return -1;
#endif
} else __timespec_to_filetime(tsp, atime);
}
if(tsp[1].tv_nsec != UTIME_OMIT) {
mtime = ft + 1;
if(tsp[1].tv_nsec == UTIME_NOW) {
#if _WCE >= 3
GetCurrentFT(mtime);
#else
SYSTEMTIME st;
GetSystemTime(&st);
if(!SystemTimeToFileTime(&st, mtime)) return -1;
#endif
} else __timespec_to_filetime(tsp + 1, mtime);
}
return SetFileTime((void *)fd, NULL, atime, mtime) ? 0 : -1;
}
int utimensat(int dirfd, const char *file, const struct timespec *tsp, int flags) {
if(!file) {
errno = EINVAL;
return -1;
}
if(!tsp) {
errno = EACCES;
return -1;
}
if(!*file) {
errno = ENOENT;
return -1;
}
int fd = open(file, O_WRONLY);
if(fd == -1) return -1;
int r = futimens(fd, tsp);
if(close(fd) < 0) return -1;
return r;
}