]> git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/time/getdate.c
Define _ALL_SOURCE when generating predefined-macros.txt.
[wasi-libc.git] / libc-top-half / musl / src / time / getdate.c
1 #include <time.h>
2 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
3 #include <pthread.h>
4 #endif
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 int getdate_err;
10
11 struct tm *getdate(const char *s)
12 {
13 static struct tm tmbuf;
14 struct tm *ret = 0;
15 char *datemsk = getenv("DATEMSK");
16 FILE *f = 0;
17 char fmt[100], *p;
18 int cs;
19
20 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
21 pthread_setcancelstate(PTHREAD_CANCEL_DEFERRED, &cs);
22 #endif
23
24 if (!datemsk) {
25 getdate_err = 1;
26 goto out;
27 }
28
29 f = fopen(datemsk, "rbe");
30 if (!f) {
31 if (errno == ENOMEM) getdate_err = 6;
32 else getdate_err = 2;
33 goto out;
34 }
35
36 while (fgets(fmt, sizeof fmt, f)) {
37 p = strptime(s, fmt, &tmbuf);
38 if (p && !*p) {
39 ret = &tmbuf;
40 goto out;
41 }
42 }
43
44 if (ferror(f)) getdate_err = 5;
45 else getdate_err = 7;
46 out:
47 if (f) fclose(f);
48 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
49 pthread_setcancelstate(cs, 0);
50 #endif
51 return ret;
52 }