/*-1* Copyright (c) 2015 Jilles Tjoelker2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/cdefs.h>27#include <sys/stat.h>2829#include <errno.h>30#include <fcntl.h>31#include <time.h>3233#ifndef UTIME_NOW34#define UTIME_NOW -135#define UTIME_OMIT -236#endif3738int39futimens(int fd, const struct timespec times[2])40{41struct timeval now, tv[2], *tvp;42struct stat sb;43int osreldate;4445if (times == NULL || (times[0].tv_nsec == UTIME_NOW &&46times[1].tv_nsec == UTIME_NOW))47tvp = NULL;48else if (times[0].tv_nsec == UTIME_OMIT &&49times[1].tv_nsec == UTIME_OMIT)50return (0);51else {52if ((times[0].tv_nsec < 0 || times[0].tv_nsec > 999999999) &&53times[0].tv_nsec != UTIME_NOW &&54times[0].tv_nsec != UTIME_OMIT) {55errno = EINVAL;56return (-1);57}58if ((times[1].tv_nsec < 0 || times[1].tv_nsec > 999999999) &&59times[1].tv_nsec != UTIME_NOW &&60times[1].tv_nsec != UTIME_OMIT) {61errno = EINVAL;62return (-1);63}64tv[0].tv_sec = times[0].tv_sec;65tv[0].tv_usec = times[0].tv_nsec / 1000;66tv[1].tv_sec = times[1].tv_sec;67tv[1].tv_usec = times[1].tv_nsec / 1000;68tvp = tv;69if (times[0].tv_nsec == UTIME_OMIT ||70times[1].tv_nsec == UTIME_OMIT) {71if (fstat(fd, &sb) == -1)72return (-1);73if (times[0].tv_nsec == UTIME_OMIT) {74tv[0].tv_sec = sb.st_atim.tv_sec;75tv[0].tv_usec = sb.st_atim.tv_nsec / 1000;76}77if (times[1].tv_nsec == UTIME_OMIT) {78tv[1].tv_sec = sb.st_mtim.tv_sec;79tv[1].tv_usec = sb.st_mtim.tv_nsec / 1000;80}81}82if (times[0].tv_nsec == UTIME_NOW ||83times[1].tv_nsec == UTIME_NOW) {84if (gettimeofday(&now, NULL) == -1)85return (-1);86if (times[0].tv_nsec == UTIME_NOW)87tv[0] = now;88if (times[1].tv_nsec == UTIME_NOW)89tv[1] = now;90}91}92return (futimes(fd, tvp));93}949596