Source:NetHack 5.0.0/src/date.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to date.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/date.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 5.0  date.c  $NHDT-Date: 1655402414 2022/06/16 18:00:14 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.3 $ */
2. /* Copyright (c) Michael Allison, 2021.                           */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. #include "config.h"
6. #include "hacklib.h"
7. #ifdef Snprintf
8. #undef Snprintf
9. #endif
10. #define Snprintf(str, size, ...) \
11.     nh_snprintf(__func__, __LINE__, str, size, __VA_ARGS__)
12. 
13. /* these are in extern.h but we don't include hack.h */
14. void populate_nomakedefs(struct version_info *) NONNULLARG1;
15. void free_nomakedefs(void);
16. 
17. extern unsigned long md_ignored_features(void);
18. extern char *mdlib_version_string(char *, const char *);
19. extern char *version_id_string(char *, size_t, const char *);
20. extern char *bannerc_string(char *, size_t, const char *);
21. 
22. /* nomakedefs_populated: flag for whether 'nomakedefs' should be freed */
23. static int nomakedefs_populated = 0;
24. 
25. struct nomakedefs_s nomakedefs = {
26.     /* https://groups.google.com/forum/#!original/
27.        comp.sources.games/91SfKYg_xzI/dGnR3JnspFkJ */
28.     "Tue, 28-Jul-87 13:18:57 EDT",
29.     "Version 1.0, built Jul 28 13:18:57 1987.",
30.     (const char *) 0,   /* git_sha */
31.     (const char *) 0,   /* git_branch */
32.     (const char *) 0,   /* git_prefix */
33.     "1.0.0-0",
34.     "NetHack Version 1.0.0-0 - last build Tue Jul 28 13:18:57 1987.",
35.     0x01010000UL,
36.     0x00000000UL,
37.     0x00000000UL,
38.     0x00000000UL,
39.     554476737UL,
40. };
41. 
42. #if defined(__DATE__) && defined(__TIME__)
43. 
44. #define extract_field(t,s,n,z)    \
45.     do {                          \
46.         for (i = 0; i < n; ++i)   \
47.             t[i] = s[i + z];      \
48.         t[i] = '\0';              \
49.     } while (0)
50. 
51. void
52. populate_nomakedefs(struct version_info *version)
53. {
54.     int i;
55.     char tmpbuf1[BUFSZ], tmpbuf2[BUFSZ], *strp;
56.     const char *mth[] = {
57.         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
58.         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
59.     struct tm t = {0};
60.     time_t timeresult;
61. 
62.     /*
63.      * In a cross-compiled environment, you can't execute
64.      * the target binaries during the build, so we can't
65.      * use makedefs to write the values of the build
66.      * date and time to a file for retrieval. Not for
67.      * information meaningful to the target execution
68.      * environment.
69.      *
70.      * How can we capture the build date/time of the target
71.      * binaries in such a situation?  We need to rely on the
72.      * cross-compiler itself to do it for us during the
73.      * cross-compile.
74.      *
75.      * To that end, we are going to make use of the
76.      * following pre-defined preprocessor macros for this:
77.      *    gcc, msvc, clang   __DATE__  "Feb 12 1996"
78.      *    gcc, msvc, clang   __TIME__  "23:59:01"
79.      *
80.      */
81. 
82.     Snprintf(tmpbuf1, sizeof tmpbuf1, "%s %s", __DATE__, __TIME__);
83.     /* "Feb 12 1996 23:59:01"
84.         01234567890123456789  */
85.     if ((int) strlen(tmpbuf1) == 20) {
86.         extract_field(tmpbuf2, tmpbuf1, 4, 7);   /* year */
87.         t.tm_year = atoi(tmpbuf2) - 1900;
88.         extract_field(tmpbuf2, tmpbuf1, 3, 0);   /* mon */
89.         for (i = 0; i < SIZE(mth); ++i)
90.             if (!case_insensitive_comp(tmpbuf2, mth[i])) {
91.                 t.tm_mon = i;
92.                 break;
93.             }
94.         extract_field(tmpbuf2, tmpbuf1, 2, 4);   /* mday */
95.         strp = tmpbuf2;
96.         if (*strp == ' ')
97.             strp++;
98.         t.tm_mday = atoi(strp);
99.         extract_field(tmpbuf2, tmpbuf1, 2, 12);  /* hour */
100.         t.tm_hour = atoi(tmpbuf2);
101.         extract_field(tmpbuf2, tmpbuf1, 2, 15);  /* min  */
102.         t.tm_min = atoi(tmpbuf2);
103.         extract_field(tmpbuf2, tmpbuf1, 2, 18);  /* sec  */
104.         t.tm_sec = atoi(tmpbuf2);
105.         timeresult = mktime(&t);
106.         nomakedefs.build_time = (unsigned long) timeresult;
107.         nomakedefs.build_date = dupstr(tmpbuf1);
108.     }
109. 
110.     nomakedefs.version_number = version->incarnation;
111.     nomakedefs.version_features = version->feature_set;
112.     nomakedefs.ignored_features = md_ignored_features();
113.     nomakedefs.version_sanity1 = version->entity_count;
114.     nomakedefs.version_string = dupstr(mdlib_version_string(tmpbuf2, "."));
115.     nomakedefs.version_id = dupstr(
116.            version_id_string(tmpbuf2, sizeof tmpbuf2, nomakedefs.build_date));
117.     nomakedefs.copyright_banner_c = dupstr(
118.               bannerc_string(tmpbuf2, sizeof tmpbuf2, nomakedefs.build_date));
119. #ifdef NETHACK_GIT_SHA
120.     nomakedefs.git_sha = dupstr(NETHACK_GIT_SHA);
121. #endif
122. #ifdef NETHACK_GIT_BRANCH
123.     nomakedefs.git_branch = dupstr(NETHACK_GIT_BRANCH);
124. #endif
125. #ifdef NETHACK_GIT_PREFIX
126.     nomakedefs.git_prefix = dupstr(NETHACK_GIT_PREFIX);
127. #endif
128. 
129.     nomakedefs_populated = 1;
130.     return;
131. }
132. 
133. void
134. free_nomakedefs(void)
135. {
136.     /* can't just free non-Null values because they're initialized at
137.        compile-time with static strings and won't have dynamic values
138.        unless populate_nomakedefs() has been called */
139.     if (!nomakedefs_populated)
140.         return;
141. 
142.     if (nomakedefs.build_date)
143.         free((genericptr_t) nomakedefs.build_date),
144.             nomakedefs.build_date = 0;
145.     if (nomakedefs.version_string)
146.         free((genericptr_t) nomakedefs.version_string),
147.             nomakedefs.version_string = 0;
148.     if (nomakedefs.version_id)
149.         free((genericptr_t) nomakedefs.version_id),
150.             nomakedefs.version_id = 0;
151.     if (nomakedefs.copyright_banner_c)
152.         free((genericptr_t) nomakedefs.copyright_banner_c),
153.             nomakedefs.copyright_banner_c = 0;
154. #ifdef NETHACK_GIT_SHA
155.     if (nomakedefs.git_sha)
156.         free((genericptr_t) nomakedefs.git_sha),
157.             nomakedefs.git_sha = 0;
158. #endif
159. #ifdef NETHACK_GIT_BRANCH
160.     if (nomakedefs.git_branch)
161.         free((genericptr_t) nomakedefs.git_branch),
162.             nomakedefs.git_branch = 0;
163. #endif
164. #ifdef NETHACK_GIT_PREFIX
165.     if (nomakedefs.git_prefix)
166.         free((genericptr_t) nomakedefs.git_prefix),
167.             nomakedefs.git_prefix = 0;
168. #endif
169. 
170.     /* values are Null now; dynamic vs static doesn't really matter anymore */
171.     nomakedefs_populated = 0;
172.     return;
173. }
174. 
175. #endif /* __DATE__ && __TIME__ */
176. 
177. 
178. /*date.c*/