From 58160829b43c8fb326d6429a65aae5265f521c8c Mon Sep 17 00:00:00 2001 From: leitner Date: Tue, 14 Jul 2020 19:55:38 +0000 Subject: [PATCH] add man pages for fmt_copybytes and fmt_copybytes_sizeof_minus1 --- fmt.h | 5 +---- fmt/fmt_copybytes.3 | 15 +++++++++++++++ fmt/fmt_copybytes_sizeof_minus1.3 | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 fmt/fmt_copybytes.3 create mode 100644 fmt/fmt_copybytes_sizeof_minus1.3 diff --git a/fmt.h b/fmt.h index bbcc043..7c0858d 100644 --- a/fmt.h +++ b/fmt.h @@ -129,10 +129,7 @@ static inline size_t fmt_copybytes(char* dest,const char* src,size_t n) { * fmt_copybytes_sizeof_minus1(dest, "\x01\x02\x03\x04"); * since we are technically passing a string, sizeof will include the * finishing 0 byte which we neither need nor want */ -static inline size_t fmt_copybytes_sizeof_minus1(char* dest,const char* src) { - if (dest) byte_copy(dest,sizeof(src)-1,src); - return sizeof(src)-1; -} +#define fmt_copybytes_sizeof_minus1(dest,src) fmt_copybytes(dest,src,sizeof(src)-1) /* "foo" -> " foo" * write padlen-srclen spaces, if that is >= 0. Then copy srclen diff --git a/fmt/fmt_copybytes.3 b/fmt/fmt_copybytes.3 new file mode 100644 index 0000000..300a326 --- /dev/null +++ b/fmt/fmt_copybytes.3 @@ -0,0 +1,15 @@ +.TH fmt_copybytes 3 +.SH NAME +fmt_copybytes \- write binary data +.SH SYNTAX +.B #include + +size_t \fBfmt_copybytes\fP(char *\fIdest\fR,const char *\fIsource\fR, size_t limit); +.SH DESCRIPTION +fmt_copybytes copies \fIlimit\fP bytes from \fIsource\fP to \fIdest\fP and returns \fIlimit\fP. + +This is a convenience wrapper interface around +\fImemcpy\fP/\fIbyte_copy\fP to make it work like the other fmt +functions. +.SH "SEE ALSO" +byte_copy(3), memcpy(3) diff --git a/fmt/fmt_copybytes_sizeof_minus1.3 b/fmt/fmt_copybytes_sizeof_minus1.3 new file mode 100644 index 0000000..0182f66 --- /dev/null +++ b/fmt/fmt_copybytes_sizeof_minus1.3 @@ -0,0 +1,26 @@ +.TH fmt_copybytes_sizeof_minus1 3 +.SH NAME +fmt_copybytes_sizeof_minus1 \- write binary data +.SH SYNTAX +.B #include + +size_t \fBfmt_copybytes_sizeof_minus1\fP(char *\fIdest\fR,const char *\fIsource\fR); +.SH DESCRIPTION +This macro will copy sizeof(\fIsource\fP)-1 bytes from \fIsource\fP to \fIdest\fP. + +This is a convenience wrapper interface around +\fIfmt_copybytes\fP for the situation where you construct a binary +message by concatenating hex constant strings like this: + + fmt_copybytes_sizeof_minus1(dest, "\\x01\\x02\\x03\\x04"); + +Since the source is technically a string, the compiler will add a 0 byte +and sizeof() will return the size including that 0 byte. That is why we +have a minus 1 in this function, so we do not copy or count that 0 byte. + +.SH NOTE +Caution: \fIsource\fP must not have side effects or those will be +evaluated twice. + +.SH "SEE ALSO" +fmt_copybytes(3)