add _DEFAULT_SOURCE wherever _BSD_SOURCE was used
[libc-test] / src / functional / stat.c
1 #include <sys/stat.h>
2 #include <errno.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <time.h>
6 #include <stdint.h>
7 #include <unistd.h>
8 #include "test.h"
9
10 #define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
11
12 int main(void)
13 {
14         struct stat st;
15         FILE *f;
16         time_t t;
17
18         if (TEST(stat(".",&st)==0, "errno = %s\n", strerror(errno))) {
19                 TEST(S_ISDIR(st.st_mode), "\n");
20                 TEST(st.st_nlink>0, "%ju\n", (uintmax_t)st.st_nlink);
21                 t = time(0);
22                 TEST(st.st_ctime<=t, "%jd > %jd\n", (intmax_t)st.st_ctime, (intmax_t)t);
23                 TEST(st.st_mtime<=t, "%jd > %jd\n", (intmax_t)st.st_mtime, (intmax_t)t);
24                 TEST(st.st_atime<=t, "%jd > %jd\n", (intmax_t)st.st_atime, (intmax_t)t);
25         }
26
27         if (TEST(stat("/dev/null",&st)==0, "errno = %s\n", strerror(errno))) {
28                 TEST(S_ISCHR(st.st_mode), "\n");
29         }
30
31         if ((f = tmpfile())) {
32                 fputs("hello", f);
33                 fflush(f);
34                 if (TEST(fstat(fileno(f),&st)==0, "errnp = %s\n", strerror(errno))) {
35                         TEST(st.st_uid==geteuid(), "%d vs %d\n", (int)st.st_uid, (int)geteuid());
36                         TEST(st.st_gid==getegid(), "%d vs %d\n", (int)st.st_uid, (int)geteuid());
37                         TEST(st.st_size==5, "%jd vs 5\n", (intmax_t)st.st_size);
38                 }
39                 fclose(f);
40         }
41
42         return t_status;
43 }