| 1 | #include <time.h> |
| 2 | #include <pthread.h> |
| 3 | #include <errno.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | |
| 7 | int getdate_err; |
| 8 | |
| 9 | struct tm *getdate(const char *s) |
| 10 | { |
| 11 | 	static struct tm tmbuf; |
| 12 | 	struct tm *ret = 0; |
| 13 | 	char *datemsk = getenv("DATEMSK"); |
| 14 | 	FILE *f = 0; |
| 15 | 	char fmt[100], *p; |
| 16 | 	int cs; |
| 17 | |
| 18 | 	pthread_setcancelstate(PTHREAD_CANCEL_DEFERRED, &cs); |
| 19 | |
| 20 | 	if (!datemsk) { |
| 21 | 		getdate_err = 1; |
| 22 | 		goto out; |
| 23 | 	} |
| 24 | |
| 25 | 	f = fopen(datemsk, "rbe"); |
| 26 | 	if (!f) { |
| 27 | 		if (errno == ENOMEM) getdate_err = 6; |
| 28 | 		else getdate_err = 2; |
| 29 | 		goto out; |
| 30 | 	} |
| 31 | |
| 32 | 	while (fgets(fmt, sizeof fmt, f)) { |
| 33 | 		p = strptime(s, fmt, &tmbuf); |
| 34 | 		if (p && !*p) { |
| 35 | 			ret = &tmbuf; |
| 36 | 			goto out; |
| 37 | 		} |
| 38 | 	} |
| 39 | |
| 40 | 	if (ferror(f)) getdate_err = 5; |
| 41 | 	else getdate_err = 7; |
| 42 | out: |
| 43 | 	if (f) fclose(f); |
| 44 | 	pthread_setcancelstate(cs, 0); |
| 45 | 	return ret; |
| 46 | } |