diff options
Diffstat (limited to 'src/timefns.c')
| -rw-r--r-- | src/timefns.c | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/src/timefns.c b/src/timefns.c index 94cfddf0da9..71d5e10872a 100644 --- a/src/timefns.c +++ b/src/timefns.c | |||
| @@ -1312,11 +1312,12 @@ or (if you need time as a string) `format-time-string'. */) | |||
| 1312 | ((size_t) -1) for MAXSIZE. | 1312 | ((size_t) -1) for MAXSIZE. |
| 1313 | 1313 | ||
| 1314 | This function behaves like nstrftime, except it allows NUL | 1314 | This function behaves like nstrftime, except it allows NUL |
| 1315 | bytes in FORMAT and it does not support nanoseconds. */ | 1315 | bytes in FORMAT. */ |
| 1316 | static size_t | 1316 | static size_t |
| 1317 | emacs_nmemftime (char *s, size_t maxsize, const char *format, | 1317 | emacs_nmemftime (char *s, size_t maxsize, const char *format, |
| 1318 | size_t format_len, const struct tm *tp, timezone_t tz, int ns) | 1318 | size_t format_len, const struct tm *tp, timezone_t tz, int ns) |
| 1319 | { | 1319 | { |
| 1320 | int saved_errno = errno; | ||
| 1320 | size_t total = 0; | 1321 | size_t total = 0; |
| 1321 | 1322 | ||
| 1322 | /* Loop through all the NUL-terminated strings in the format | 1323 | /* Loop through all the NUL-terminated strings in the format |
| @@ -1326,30 +1327,25 @@ emacs_nmemftime (char *s, size_t maxsize, const char *format, | |||
| 1326 | '\0' byte so we must invoke it separately for each such string. */ | 1327 | '\0' byte so we must invoke it separately for each such string. */ |
| 1327 | for (;;) | 1328 | for (;;) |
| 1328 | { | 1329 | { |
| 1329 | size_t len; | 1330 | errno = 0; |
| 1330 | size_t result; | 1331 | size_t result = nstrftime (s, maxsize, format, tp, tz, ns); |
| 1331 | 1332 | if (result == 0 && errno != 0) | |
| 1333 | return result; | ||
| 1332 | if (s) | 1334 | if (s) |
| 1333 | s[0] = '\1'; | 1335 | s += result + 1; |
| 1334 | |||
| 1335 | result = nstrftime (s, maxsize, format, tp, tz, ns); | ||
| 1336 | |||
| 1337 | if (s) | ||
| 1338 | { | ||
| 1339 | if (result == 0 && s[0] != '\0') | ||
| 1340 | return 0; | ||
| 1341 | s += result + 1; | ||
| 1342 | } | ||
| 1343 | 1336 | ||
| 1344 | maxsize -= result + 1; | 1337 | maxsize -= result + 1; |
| 1345 | total += result; | 1338 | total += result; |
| 1346 | len = strlen (format); | 1339 | size_t len = strlen (format); |
| 1347 | if (len == format_len) | 1340 | if (len == format_len) |
| 1348 | return total; | 1341 | break; |
| 1349 | total++; | 1342 | total++; |
| 1350 | format += len + 1; | 1343 | format += len + 1; |
| 1351 | format_len -= len + 1; | 1344 | format_len -= len + 1; |
| 1352 | } | 1345 | } |
| 1346 | |||
| 1347 | errno = saved_errno; | ||
| 1348 | return total; | ||
| 1353 | } | 1349 | } |
| 1354 | 1350 | ||
| 1355 | static Lisp_Object | 1351 | static Lisp_Object |
| @@ -1379,10 +1375,11 @@ format_time_string (char const *format, ptrdiff_t formatlen, | |||
| 1379 | 1375 | ||
| 1380 | while (true) | 1376 | while (true) |
| 1381 | { | 1377 | { |
| 1382 | buf[0] = '\1'; | 1378 | errno = 0; |
| 1383 | len = emacs_nmemftime (buf, size, format, formatlen, tmp, tz, ns); | 1379 | len = emacs_nmemftime (buf, size, format, formatlen, tmp, tz, ns); |
| 1384 | if ((0 < len && len < size) || (len == 0 && buf[0] == '\0')) | 1380 | if (len != 0 || errno == 0) |
| 1385 | break; | 1381 | break; |
| 1382 | eassert (errno == ERANGE); | ||
| 1386 | 1383 | ||
| 1387 | /* Buffer was too small, so make it bigger and try again. */ | 1384 | /* Buffer was too small, so make it bigger and try again. */ |
| 1388 | len = emacs_nmemftime (NULL, SIZE_MAX, format, formatlen, tmp, tz, ns); | 1385 | len = emacs_nmemftime (NULL, SIZE_MAX, format, formatlen, tmp, tz, ns); |