Mercurial > emacs
comparison src/editfns.c @ 23213:3bfc1e9b0377
(emacs_memftime): New function.
(Fformat_time_string): Use it to handle null bytes in formats
correctly.
author | Paul Eggert <eggert@twinsun.com> |
---|---|
date | Tue, 08 Sep 1998 03:56:09 +0000 |
parents | d7bd20e02b1d |
children | 90e5d916ebd9 |
comparison
equal
deleted
inserted
replaced
23212:42fcd022d4e5 | 23213:3bfc1e9b0377 |
---|---|
842 if (CONSP (low)) | 842 if (CONSP (low)) |
843 low = Fcar (low); | 843 low = Fcar (low); |
844 CHECK_NUMBER (low, 0); | 844 CHECK_NUMBER (low, 0); |
845 *result = (XINT (high) << 16) + (XINT (low) & 0xffff); | 845 *result = (XINT (high) << 16) + (XINT (low) & 0xffff); |
846 return *result >> 16 == XINT (high); | 846 return *result >> 16 == XINT (high); |
847 } | |
848 } | |
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; | |
847 } | 894 } |
848 } | 895 } |
849 | 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, |
923 { | 970 { |
924 char *buf = (char *) alloca (size + 1); | 971 char *buf = (char *) alloca (size + 1); |
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 } |
938 } | 988 } |
939 | 989 |