Debian Codegraph @

terps/tads/tads2/ostzposix.c

raw symbols SCIP
1 #include <ctype.h>
2 #include <string.h>
3 #include <time.h>
4
5 #include "os.h"
6 #include "lib.h"
7 #include "osstzprs.h"
8
9 extern void safe_strcpy(char *dst, size_t dstlen, const char *src);
10
11 /* Get the local time zone name, as a zoneinfo database key. Many
12 * modern Unix systems use zoneinfo keys as they native timezone
13 * setting, but there are several different ways of storing this on a
14 * per-process and system-wide basis. We'll try looking for the common
15 * mechanisms.
16 */
17 int
18 os_get_zoneinfo_key( char *buf, size_t buflen )
19 {
20 /* First, try the TZ environment variable. This is used on nearly
21 * all Unix-alikes for a per-process timezone setting, although
22 * it will only contain a zoneinfo key in newer versions. There
23 * are several possible formats for specifying a zoneinfo key:
24 *
25 * TZ=/usr/share/zoneinfo/America/Los_Angeles
26 * - A full absolute path name to a tzinfo file. We'll sense
27 * this by looking for "/zoneinfo/" in the string, and if we
28 * find it, we'll return the portion after /zoneinfo/.
29 *
30 * TZ=America/Los_Angeles
31 * - Just the zoneinfo key, without a path. If we find a string
32 * that contains all alphabetics, undersores, and slashes, and
33 * has at least one internal slash but doesn't start with a
34 * slash, we probably have a zoneinfo key. We'll see if we
35 * can find a matching file in the usual zoneinfo database
36 * locations: /etc/zoneinfo, /usr/share/zoneinfo; if we can,
37 * we'll return the key name, otherwise we'll assume this
38 * isn't actually a zoneinfo key but just happens to look like
39 * one in terms of format.
40 *
41 * TZ=:America/Los_Angeles
42 * TZ=:/etc/zoneinfo/America/Los_Angeles
43 * - POSIX systems generally use the ":" prefix to signify that
44 * this is a zoneinfo path rather than the old-style "EST5EDT"
45 * type of self-contained zone description. If we see a colon
46 * prefix with a relative path (properly formed in terms of
47 * its character content), we'll simply assume this is a
48 * zoneinfo key without even checking for an existing file,
49 * since there's not much else it could be. If we see an
50 * absolute path, we'll search it for /zoneinfo/ and return
51 * the portion after this, again without checking for an
52 * existing file.
53 */
54 const char *tz = getenv("TZ");
55 if (tz != 0 && tz[0] != '\0')
56 {
57 /* check that the string is formatted like a zoneinfo key */
58 #define tzcharok(c) (isalpha(c) != 0 || (c) == '/' || (c) == '_')
59 int fmt_ok = TRUE;
60 const char *p;
61 fmt_ok &= (tz[0] == ':' || tzcharok(tz[0]));
62 for (p = tz + 1 ; *p != '\0' ; ++p)
63 fmt_ok &= tzcharok(*p);
64
65 /* proceed only if it has the right format */
66 if (fmt_ok)
67 {
68 /* check for a leading ':', per POSIX */
69 if (tz[0] == ':')
70 {
71 /* yes, we have a leading ':', so it's almost certainly
72 * a zoneinfo key; if it's an absolute path, find the
73 * part after /zoneinfo/
74 */
75 if (tz[1] == '/')
76 {
77 /* absolute form - look for /zoneinfo/ */
78 const char *z = strstr(tz, "/zoneinfo/");
79 if (z != 0)
80 {
81 /* found it - return the part after /zoneinfo/ */
82 safe_strcpy(buf, buflen, z + 10);
83 return TRUE;
84 }
85 }
86 else
87 {
88 /* relative path - return as-is minus the colon */
89 safe_strcpy(buf, buflen, tz + 1);
90 return TRUE;
91 }
92 }
93 else
94 {
95 /* There's no colon, so it *might* be a zoneinfo key.
96 * If it's an absolute path containing /zoneinfo/, it's
97 * a solid bet. If it's a relative path, look to see
98 * if we can find a file in one of the usual zoneinfo
99 * database locations.
100 */
101 if (tz[0] == '/')
102 {
103 /* absolute path - check for /zoneinfo/ */
104 const char *z = strstr(tz, "/zoneinfo/");
105 if (z != 0)
106 {
107 /* found it - return the part after /zoneinfo/ */
108 safe_strcpy(buf, buflen, z + 10);
109 return TRUE;
110 }
111 }
112 else
113 {
114 /* relative path - look for a tzinfo file in the
115 * usual locations
116 */
117 static const char *dirs[] = {
118 "/etc/zoneinfo",
119 "/usr/share/zoneinfo",
120 0
121 };
122 const char **dir;
123 for (dir = dirs ; *dir != 0 ; ++dir)
124 {
125 /* build this full path */
126 char fbuf[OSFNMAX];
127 os_build_full_path(fbuf, sizeof(fbuf), *dir, tz);
128
129 /* check for a file at this location */
130 if (!osfacc(fbuf))
131 {
132 /* got it - looks like a good zoneinfo key */
133 safe_strcpy(buf, buflen, tz);
134 return TRUE;
135 }
136 }
137 }
138 }
139 }
140 }
141
142 /* No luck with TZ, so try the system-wide settings next.
143 *
144 * If a file called /etc/timezone exists, it's usually a one-line
145 * text file containing the zoneinfo key. Read and return its
146 * contents.
147 */
148 FILE *fp;
149 if ((fp = fopen("/etc/timezone", "r")) != 0)
150 {
151 /* read the one-liner */
152 char lbuf[256];
153 int ok = FALSE;
154 if (fgets(lbuf, sizeof(lbuf), fp) != 0)
155 {
156 /* strip any trailing newline */
157 size_t l = strlen(lbuf);
158 if (l != 0 && lbuf[l-1] == '\n')
159 lbuf[l-1] = '\0';
160
161 /* if it's in absolute format, return the part after
162 * /zoneinfo/; otherwise just return the string
163 */
164 if (lbuf[0] == '/')
165 {
166 /* absoltue path - find /zoneinfo/ */
167 const char *z = strstr(lbuf, "/zoneinfo/");
168 if (z != 0)
169 {
170 safe_strcpy(buf, buflen, z + 10);
171 ok = TRUE;
172 }
173 }
174 else
175 {
176 /* relative notation - return it as-is */
177 safe_strcpy(buf, buflen, lbuf);
178 ok = TRUE;
179 }
180 }
181
182 /* we're done with the file */
183 fclose(fp);
184
185 /* if we got our result, return success */
186 if (ok)
187 return TRUE;
188 }
189
190 /* If /etc/sysconfig/clock exists, read it and look for a line
191 * starting with ZONE=. This contains the zoneinfo key.
192 */
193 if ((fp = fopen("/etc/sysconfig/clock", "r")) != 0)
194 {
195 /* scan the file for ZONE=... */
196 int ok = FALSE;
197 for (;;)
198 {
199 /* read the next line */
200 char lbuf[256];
201 if (fgets(lbuf, sizeof(lbuf), fp) == 0)
202 break;
203
204 /* skip leading spaces */
205 const char *p;
206 for (p = lbuf ; isspace(*p) ; ++p) ;
207
208 /* check for ZONE */
209 if (memicmp(p, "zone", 4) != 0)
210 continue;
211
212 /* skip spaces after ZONE */
213 for (p += 4 ; isspace(*p) ; ++p) ;
214
215 /* check for '=' */
216 if (*p != '=')
217 continue;
218
219 /* skip spaces after the '=' */
220 for (++p ; isspace(*p) ; ++p) ;
221
222 /* if it's in absolute form, look for /zoneinfo/ */
223 if (*p == '/')
224 {
225 const char *z = strstr(p, "/zoneinfo/");
226 if (z != 0)
227 {
228 safe_strcpy(buf, buflen, z + 10);
229 ok = TRUE;
230 }
231 }
232 else
233 {
234 /* relative notation - it's the zoneinfo key */
235 safe_strcpy(buf, buflen, p);
236 ok = TRUE;
237 }
238
239 /* that's our ZONE line, so we're done scanning the file */
240 break;
241 }
242
243 /* done with the file */
244 fclose(fp);
245
246 /* if we got our result, return success */
247 if (ok)
248 return TRUE;
249 }
250
251 /* If /etc/localtime is a symbolic link, the linked file is the
252 * actual zoneinfo file. Resolve the link and return the portion
253 * of the path after "/zoneinfo/".
254 */
255 static const char *elt = "/etc/localtime";
256 unsigned long mode;
257 char linkbuf[OSFNMAX];
258 const char *zi;
259 if (osfmode(elt, FALSE, &mode, NULL)
260 && (mode & OSFMODE_LINK) != 0
261 && os_resolve_symlink(elt, linkbuf, sizeof(linkbuf))
262 && (zi = strstr(linkbuf, "/zoneinfo/")) != 0)
263 {
264 /* it's a link containing /zoneinfo/, so return the portion
265 * after /zoneinfo/
266 */
267 safe_strcpy(buf, buflen, zi + 10);
268 return TRUE;
269 }
270
271 /* well, we're out of ideas - return failure */
272 return FALSE;
273 }
274
275 /* Get timezone information. This can be used as a fallback if the a
276 * zoneinfo key isn't available, or if the caller doesn't use zoneinfo
277 * data.
278 */
279 int
280 os_get_timezone_info( struct os_tzinfo_t *info )
281 {
282 /* try parsing environment variable TZ as a POSIX timezone string */
283 const char *tz = getenv("TZ");
284 if (tz != 0 && oss_parse_posix_tz(info, tz, strlen(tz), TRUE))
285 return TRUE;
286
287 /* GLK: tm_gmtoff isn't POSIX, and since this clearly isn't a
288 * critical function (as it returns failure on Windows), don't
289 * implement it here.
290 */
291 #if 0
292 #ifndef _WIN32
293 /* fall back on localtime() - that'll at least give us the current
294 * timezone name and GMT offset in most cases
295 */
296 time_t timer = time(0);
297 const struct tm *tm = localtime(&timer);
298 if (tm != 0)
299 {
300 memset(info, 0, sizeof(*info));
301 info->is_dst = tm->tm_isdst;
302 if (tm->tm_isdst)
303 {
304 info->dst_ofs = tm->tm_gmtoff;
305 safe_strcpy(info->dst_abbr, sizeof(info->dst_abbr), tm->tm_zone);
306 }
307 else
308 {
309 info->std_ofs = tm->tm_gmtoff;
310 safe_strcpy(info->std_abbr, sizeof(info->std_abbr), tm->tm_zone);
311 }
312 return TRUE;
313 }
314 #endif
315 #endif
316
317 /* no information is available */
318 return FALSE;
319 }