dirfwstat.c - 9base - revived minimalist port of Plan 9 userland to Unix
(HTM) git clone git://git.suckless.org/9base
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
dirfwstat.c (1154B)
---
1 #define NOPLAN9DEFINES
2 #include <u.h>
3 #include <libc.h>
4 #include <sys/time.h>
5 #include <sys/stat.h>
6
7 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__linux__)
8 /* do nothing -- futimes exists and is fine */
9
10 # if defined(__UCLIBC__)
11 /* use futimesat */
12 static int
13 futimes(int fd, struct timeval *tv)
14 {
15 return futimesat(fd, 0, tv);
16 }
17
18 # endif
19 #elif defined(__SunOS5_9__)
20 /* use futimesat */
21 static int
22 futimes(int fd, struct timeval *tv)
23 {
24 return futimesat(fd, 0, tv);
25 }
26
27 #else
28 /* provide dummy */
29 /* rename just in case -- linux provides an unusable one */
30 #undef futimes
31 #define futimes myfutimes
32 static int
33 futimes(int fd, struct timeval *tv)
34 {
35 werrstr("futimes not available");
36 return -1;
37 }
38
39 #endif
40
41 int
42 dirfwstat(int fd, Dir *dir)
43 {
44 int ret;
45 struct timeval tv[2];
46
47 ret = 0;
48 if(~dir->mode != 0){
49 if(fchmod(fd, dir->mode) < 0)
50 ret = -1;
51 }
52 if(~dir->mtime != 0){
53 tv[0].tv_sec = dir->mtime;
54 tv[0].tv_usec = 0;
55 tv[1].tv_sec = dir->mtime;
56 tv[1].tv_usec = 0;
57 if(futimes(fd, tv) < 0)
58 ret = -1;
59 }
60 if(~dir->length != 0){
61 if(ftruncate(fd, dir->length) < 0)
62 ret = -1;
63 }
64 return ret;
65 }
66