some coverity fixes.
[minix.git] / lib / libc / sys-minix / stat.c
blobf84f45c00088d92a2dd24856ba7d79a9c67d1124
1 #include <sys/cdefs.h>
2 #include "namespace.h"
3 #include <lib.h>
5 #include <sys/stat.h>
6 #include <string.h>
8 /* XXX until that st_Xtime macroses used, we have to undefine them,
9 * because of minix_prev_stat
11 #undef st_atime
12 #undef st_ctime
13 #undef st_mtime
15 static void prev_stat2new_stat(struct stat *new, struct minix_prev_stat *prev)
17 /* Copy field by field because of st_gid type mismath and
18 * difference in order after atime.
20 new->st_dev = prev->st_dev;
21 new->st_ino = prev->st_ino;
22 new->st_mode = prev->st_mode;
23 new->st_nlink = prev->st_nlink;
24 new->st_uid = prev->st_uid;
25 new->st_gid = prev->st_gid;
26 new->st_rdev = prev->st_rdev;
27 new->st_size = prev->st_size;
28 new->st_atimespec.tv_sec = prev->st_atime;
29 new->st_mtimespec.tv_sec = prev->st_mtime;
30 new->st_ctimespec.tv_sec = prev->st_ctime;
33 __weak_alias(_stat, __stat50);
34 __weak_alias(_lstat, __lstat50);
35 __weak_alias(_fstat, __fstat50);
37 int stat(const char *name, struct stat *buffer)
39 message m;
40 int r;
41 struct minix_prev_stat old_sb;
43 m.m1_i1 = strlen(name) + 1;
44 m.m1_p1 = (char *) __UNCONST(name);
45 m.m1_p2 = (char *) buffer;
47 if((r = _syscall(VFS_PROC_NR, STAT, &m)) >= 0 || errno != ENOSYS)
48 return r;
50 errno = 0;
52 /* ENOSYS: new binary and old VFS, fallback to PREV_STAT.
53 * User has struct stat (buffer), VFS still fills minix_prev_stat.
55 m.m1_i1 = strlen(name) + 1;
56 m.m1_p1 = (char *) __UNCONST(name);
57 m.m1_p2 = (char *) &old_sb;
59 if((r = _syscall(VFS_PROC_NR, PREV_STAT, &m)) < 0)
60 return r;
62 memset(buffer, 0, sizeof(struct stat));
63 prev_stat2new_stat(buffer, &old_sb);
65 return r;
68 int _fstat(int fd, struct stat *buffer) { return fstat(fd, buffer); }
70 int fstat(int fd, struct stat *buffer)
72 message m;
73 int r;
74 struct minix_prev_stat old_sb;
76 m.m1_i1 = fd;
77 m.m1_p1 = (char *) buffer;
79 if((r = _syscall(VFS_PROC_NR, FSTAT, &m)) >= 0 || errno != ENOSYS)
80 return r;
82 errno = 0;
84 /* ENOSYS: new binary and old VFS, fallback to PREV_STAT.
85 * User has struct stat (buffer), VFS still fills minix_prev_stat.
87 m.m1_i1 = fd;
88 m.m1_p1 = (char *) &old_sb;
90 if((r = _syscall(VFS_PROC_NR, PREV_FSTAT, &m)) < 0)
91 return r;
93 memset(buffer, 0, sizeof(struct stat));
94 prev_stat2new_stat(buffer, &old_sb);
96 return r;
99 int _lstat(const char *name, struct stat *buffer) { return lstat(name, buffer); }
101 int lstat(const char *name, struct stat *buffer)
103 message m;
104 int r;
105 struct minix_prev_stat old_sb;
107 m.m1_i1 = strlen(name) + 1;
108 m.m1_p1 = (char *) __UNCONST(name);
109 m.m1_p2 = (char *) buffer;
111 if((r = _syscall(VFS_PROC_NR, LSTAT, &m)) >= 0 || errno != ENOSYS)
112 return r;
114 errno = 0;
116 /* ENOSYS: new binary and old VFS, fallback to PREV_STAT.
117 * User has struct stat (buffer), VFS still fills minix_prev_stat.
119 m.m1_i1 = strlen(name) + 1;
120 m.m1_p1 = (char *) __UNCONST(name);
121 m.m1_p2 = (char *) &old_sb;
123 if((r = _syscall(VFS_PROC_NR, PREV_LSTAT, &m)) < 0)
124 return r;
126 memset(buffer, 0, sizeof(struct stat));
127 prev_stat2new_stat(buffer, &old_sb);
129 return r;