diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/editfns.c | 56 |
1 files changed, 53 insertions, 3 deletions
diff --git a/src/editfns.c b/src/editfns.c index 8949b183947..619280d73d5 100644 --- a/src/editfns.c +++ b/src/editfns.c | |||
| @@ -847,6 +847,53 @@ lisp_time_argument (specified_time, result) | |||
| 847 | } | 847 | } |
| 848 | } | 848 | } |
| 849 | 849 | ||
| 850 | /* Write information into buffer S of size MAXSIZE, according to the | ||
| 851 | FORMAT of length FORMAT_LEN, using time information taken from *TP. | ||
| 852 | Return the number of bytes written, not including the terminating | ||
| 853 | '\0'. If S is NULL, nothing will be written anywhere; so to | ||
| 854 | determine how many bytes would be written, use NULL for S and | ||
| 855 | ((size_t) -1) for MAXSIZE. | ||
| 856 | |||
| 857 | This function behaves like emacs_strftime, except it allows null | ||
| 858 | bytes in FORMAT. */ | ||
| 859 | static size_t | ||
| 860 | emacs_memftime (s, maxsize, format, format_len, tp) | ||
| 861 | char *s; | ||
| 862 | size_t maxsize; | ||
| 863 | const char *format; | ||
| 864 | size_t format_len; | ||
| 865 | const struct tm *tp; | ||
| 866 | { | ||
| 867 | size_t total = 0; | ||
| 868 | |||
| 869 | for (;;) | ||
| 870 | { | ||
| 871 | size_t len; | ||
| 872 | size_t result; | ||
| 873 | |||
| 874 | if (s) | ||
| 875 | s[0] = '\1'; | ||
| 876 | |||
| 877 | result = emacs_strftime (s, maxsize, format, tp); | ||
| 878 | |||
| 879 | if (s) | ||
| 880 | { | ||
| 881 | if (result == 0 && s[0] != '\0') | ||
| 882 | return 0; | ||
| 883 | s += result + 1; | ||
| 884 | } | ||
| 885 | |||
| 886 | maxsize -= result + 1; | ||
| 887 | total += result; | ||
| 888 | len = strlen (format); | ||
| 889 | if (len == format_len) | ||
| 890 | return total; | ||
| 891 | total++; | ||
| 892 | format += len + 1; | ||
| 893 | format_len -= len + 1; | ||
| 894 | } | ||
| 895 | } | ||
| 896 | |||
| 850 | /* | 897 | /* |
| 851 | DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0, | 898 | DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0, |
| 852 | "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\ | 899 | "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\ |
| @@ -925,13 +972,16 @@ DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0, | |||
| 925 | int result; | 972 | int result; |
| 926 | 973 | ||
| 927 | buf[0] = '\1'; | 974 | buf[0] = '\1'; |
| 928 | result = emacs_strftime (buf, size, XSTRING (format_string)->data, | 975 | result = emacs_memftime (buf, size, XSTRING (format_string)->data, |
| 976 | STRING_BYTES (XSTRING (format_string)), | ||
| 929 | tm); | 977 | tm); |
| 930 | if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0')) | 978 | if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0')) |
| 931 | return build_string (buf); | 979 | return make_string (buf, result); |
| 932 | 980 | ||
| 933 | /* If buffer was too small, make it bigger and try again. */ | 981 | /* If buffer was too small, make it bigger and try again. */ |
| 934 | result = emacs_strftime (NULL, 0x7fffffff, XSTRING (format_string)->data, | 982 | result = emacs_memftime (NULL, (size_t) -1, |
| 983 | XSTRING (format_string)->data, | ||
| 984 | STRING_BYTES (XSTRING (format_string)), | ||
| 935 | tm); | 985 | tm); |
| 936 | size = result + 1; | 986 | size = result + 1; |
| 937 | } | 987 | } |