xref: /reactos/sdk/lib/crt/time/futime.c (revision 1734f297)
1 /*
2  * COPYRIGHT:   LGPL, See LGPL.txt in the top level directory
3  * PROJECT:     ReactOS CRT library
4  * FILE:        lib/sdk/crt/time/futime.c
5  * PURPOSE:     Implementation of _futime
6  * PROGRAMERS:  Wine team
7  */
8 
9 /*
10  * msvcrt.dll file functions
11  *
12  * Copyright 1996,1998 Marcus Meissner
13  * Copyright 1996 Jukka Iivonen
14  * Copyright 1997,2000 Uwe Bonnes
15  * Copyright 2000 Jon Griffiths
16  * Copyright 2004 Eric Pouech
17  * Copyright 2004 Juan Lang
18  *
19  * This library is free software; you can redistribute it and/or
20  * modify it under the terms of the GNU Lesser General Public
21  * License as published by the Free Software Foundation; either
22  * version 2.1 of the License, or (at your option) any later version.
23  *
24  * This library is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27  * Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public
30  * License along with this library; if not, write to the Free Software
31  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32  *
33  * TODO
34  * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
35  */
36 
37 #include <precomp.h>
38 #define RC_INVOKED 1 // to prevent inline functions
39 #include <time.h>
40 #include <sys/utime.h>
41 #include "bitsfixup.h"
42 #include <internal/wine/msvcrt.h>
43 
44 ioinfo* get_ioinfo(int fd);
45 void release_ioinfo(ioinfo *info);
46 
47 /******************************************************************************
48  * \name _futime
49  * \brief Set a file's modification time.
50  * \param [out] ptimeb Pointer to a structure of type struct _timeb that
51  *        receives the current time.
52  * \sa http://msdn.microsoft.com/en-us/library/95e68951.aspx
53  */
54 int
55 _futime(int fd, struct _utimbuf *filetime)
56 {
57     ioinfo *info = get_ioinfo(fd);
58     FILETIME at, wt;
59 
60     if (info->handle == INVALID_HANDLE_VALUE)
61     {
62         release_ioinfo(info);
63         return -1;
64     }
65 
66     if (!filetime)
67     {
68         time_t currTime;
69         _time(&currTime);
70         RtlSecondsSince1970ToTime((ULONG)currTime,
71                                   (LARGE_INTEGER *)&at);
72         wt = at;
73     }
74     else
75     {
76         RtlSecondsSince1970ToTime((ULONG)filetime->actime,
77                                   (LARGE_INTEGER *)&at);
78         if (filetime->actime == filetime->modtime)
79         {
80             wt = at;
81         }
82         else
83         {
84             RtlSecondsSince1970ToTime((ULONG)filetime->modtime,
85                                       (LARGE_INTEGER *)&wt);
86         }
87     }
88 
89     if (!SetFileTime(info->handle, NULL, &at, &wt))
90     {
91         release_ioinfo(info);
92         _dosmaperr(GetLastError());
93         return -1 ;
94     }
95     release_ioinfo(info);
96     return 0;
97 }
98