aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert1998-09-08 03:56:09 +0000
committerPaul Eggert1998-09-08 03:56:09 +0000
commit70ebbe5f8186ec17af0af30e653a36dec6ea4cbe (patch)
tree9f93bec612da0c5690d3e0656a1fc43a3a7e94fc /src
parent8bedbe9dc24e2b2d170eaa4f23f7477f07d1ba75 (diff)
downloademacs-70ebbe5f8186ec17af0af30e653a36dec6ea4cbe.tar.gz
emacs-70ebbe5f8186ec17af0af30e653a36dec6ea4cbe.zip
(emacs_memftime): New function.
(Fformat_time_string): Use it to handle null bytes in formats correctly.
Diffstat (limited to 'src')
-rw-r--r--src/editfns.c56
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. */
859static size_t
860emacs_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/*
851DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0, 898DEFUN ("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 }