summaryrefslogtreecommitdiffstats
path: root/man7/string_copying.7
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 19:41:07 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 19:41:07 +0000
commit3af6d22bb3850ab2bac67287e3a3d3b0e32868e5 (patch)
tree3ee7a3ec64525911fa865bb984c86d997d855527 /man7/string_copying.7
parentAdding debian version 6.05.01-1. (diff)
downloadmanpages-3af6d22bb3850ab2bac67287e3a3d3b0e32868e5.tar.xz
manpages-3af6d22bb3850ab2bac67287e3a3d3b0e32868e5.zip
Merging upstream version 6.7.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'man7/string_copying.7')
-rw-r--r--man7/string_copying.7647
1 files changed, 299 insertions, 348 deletions
diff --git a/man7/string_copying.7 b/man7/string_copying.7
index 814eabd..43bc00d 100644
--- a/man7/string_copying.7
+++ b/man7/string_copying.7
@@ -2,18 +2,17 @@
.\"
.\" SPDX-License-Identifier: BSD-3-Clause
.\"
-.TH string_copying 7 2023-07-29 "Linux man-pages 6.05.01"
+.TH string_copying 7 2023-12-17 "Linux man-pages 6.7"
.\" ----- NAME :: -----------------------------------------------------/
.SH NAME
stpcpy,
strcpy, strcat,
stpecpy,
+strtcpy,
strlcpy, strlcat,
stpncpy,
strncpy,
-zustr2ustp, zustr2stp,
-strncat,
-ustpcpy, ustr2stp
+strncat
\- copying strings and character sequences
.\" ----- SYNOPSIS :: -------------------------------------------------/
.SH SYNOPSIS
@@ -22,63 +21,57 @@ ustpcpy, ustr2stp
.nf
// Chain-copy a string.
.BI "char *stpcpy(char *restrict " dst ", const char *restrict " src );
-.PP
+.P
// Copy/catenate a string.
.BI "char *strcpy(char *restrict " dst ", const char *restrict " src );
.BI "char *strcat(char *restrict " dst ", const char *restrict " src );
-.PP
+.P
// Chain-copy a string with truncation.
.BI "char *stpecpy(char *" dst ", char " end "[0], const char *restrict " src );
-.PP
+.P
// Copy/catenate a string with truncation.
-.BI "size_t strlcpy(char " dst "[restrict ." sz "], \
+.BI "ssize_t strtcpy(char " dst "[restrict ." dsize "], \
const char *restrict " src ,
-.BI " size_t " sz );
-.BI "size_t strlcat(char " dst "[restrict ." sz "], \
+.BI " size_t " dsize );
+.BI "size_t strlcpy(char " dst "[restrict ." dsize "], \
const char *restrict " src ,
-.BI " size_t " sz );
+.BI " size_t " dsize );
+.BI "size_t strlcat(char " dst "[restrict ." dsize "], \
+const char *restrict " src ,
+.BI " size_t " dsize );
.fi
.\" ----- SYNOPSIS :: Null-padded character sequences --------/
.SS Null-padded character sequences
.nf
-// Zero a fixed-width buffer, and
-// copy a string into a character sequence with truncation.
-.BI "char *stpncpy(char " dst "[restrict ." sz "], \
+// Fill a fixed-size buffer with characters from a string
+// and pad with null bytes.
+.BI "char *strncpy(char " dst "[restrict ." dsize "], \
const char *restrict " src ,
-.BI " size_t " sz );
-.PP
-// Zero a fixed-width buffer, and
-// copy a string into a character sequence with truncation.
-.BI "char *strncpy(char " dst "[restrict ." sz "], \
+.BI " size_t " dsize );
+.BI "char *stpncpy(char " dst "[restrict ." dsize "], \
const char *restrict " src ,
-.BI " size_t " sz );
-.PP
+.BI " size_t " dsize );
+.P
// Chain-copy a null-padded character sequence into a character sequence.
-.BI "char *zustr2ustp(char *restrict " dst ", \
-const char " src "[restrict ." sz ],
-.BI " size_t " sz );
-.PP
+.I mempcpy(dst, src, strnlen(src, NITEMS(src)));
+.P
// Chain-copy a null-padded character sequence into a string.
-.BI "char *zustr2stp(char *restrict " dst ", \
-const char " src "[restrict ." sz ],
-.BI " size_t " sz );
-.PP
+.I stpcpy(mempcpy(dst, src, strnlen(src, NITEMS(src))), \[dq]\[dq]);
+.P
// Catenate a null-padded character sequence into a string.
-.BI "char *strncat(char *restrict " dst ", const char " src "[restrict ." sz ],
-.BI " size_t " sz );
+.BI "char *strncat(char *restrict " dst ", const char " src "[restrict ." ssize ],
+.BI " size_t " ssize );
.fi
-.\" ----- SYNOPSIS :: Measured character sequences --------------------/
-.SS Measured character sequences
+.\" ----- SYNOPSIS :: Known-length character sequences --------------------/
+.SS Known-length character sequences
.nf
-// Chain-copy a measured character sequence.
-.BI "char *ustpcpy(char *restrict " dst ", \
-const char " src "[restrict ." len ],
-.BI " size_t " len );
-.PP
-// Chain-copy a measured character sequence into a string.
-.BI "char *ustr2stp(char *restrict " dst ", \
-const char " src "[restrict ." len ],
+// Chain-copy a known-length character sequence.
+.BI "void *mempcpy(void " dst "[restrict ." len "], \
+const void " src "[restrict ." len ],
.BI " size_t " len );
+.P
+// Chain-copy a known-length character sequence into a string.
+.I stpcpy(mempcpy(dst, src, len), \[dq]\[dq]);
.fi
.SH DESCRIPTION
.\" ----- DESCRIPTION :: Terms (and abbreviations) :: -----------------/
@@ -86,7 +79,7 @@ const char " src "[restrict ." len ],
.\" ----- DESCRIPTION :: Terms (and abbreviations) :: string (str) ----/
.TP
.IR "string " ( str )
-is a sequence of zero or more non-null characters followed by a null byte.
+is a sequence of zero or more non-null characters followed by a null character.
.\" ----- DESCRIPTION :: Terms (and abbreviations) :: null-padded character seq
.TP
.I character sequence
@@ -96,15 +89,18 @@ However, with appropriate care,
a string can be used in the place of a character sequence.
.RS
.TP
-.IR "null-padded character sequence " ( zustr )
-Character sequences can be contained in fixed-width buffers,
+.I null-padded character sequence
+Character sequences can be contained in fixed-size buffers,
which contain padding null bytes after the character sequence,
to fill the rest of the buffer
without affecting the character sequence;
however, those padding null bytes are not part of the character sequence.
-.\" ----- DESCRIPTION :: Terms (and abbreviations) :: measured character sequence
+Don't confuse null-padded with null-terminated:
+null-padded means 0 or more padding null bytes,
+while null-terminated means exactly 1 terminating null character.
+.\" ----- DESCRIPTION :: Terms (and abbreviations) :: known-length character sequence
.TP
-.IR "measured character sequence " ( ustr )
+.I known-length character sequence
Character sequence delimited by its length.
It may be a slice of a larger character sequence,
or even of a string.
@@ -116,10 +112,10 @@ is the number of non-null characters in a string or character sequence.
It is the return value of
.I strlen(str)
and of
-.IR "strnlen(ustr, sz)" .
-.\" ----- DESCRIPTION :: Terms (and abbreviations) :: size (sz) -------/
+.IR "strnlen(buf, size)" .
+.\" ----- DESCRIPTION :: Terms (and abbreviations) :: size ------------/
.TP
-.IR "size " ( sz )
+.I size
refers to the entire buffer
where the string or character sequence is contained.
.\" ----- DESCRIPTION :: Terms (and abbreviations) :: end -------------/
@@ -127,7 +123,7 @@ where the string or character sequence is contained.
.I end
is the name of a pointer to one past the last element of a buffer.
It is equivalent to
-.IR &str[sz] .
+.IR &str[size] .
It is used as a sentinel value,
to be able to truncate strings or character sequences
instead of overrunning the containing buffer.
@@ -141,7 +137,7 @@ the writing starts at the first element pointed to by
.TP
.I catenate
This term is used when
-a function first finds the terminating null byte in
+a function first finds the terminating null character in
.IR dst ,
and then starts writing at that position.
.\" ----- DESCRIPTION :: Terms (and abbreviations) :: chain -----------/
@@ -149,12 +145,12 @@ and then starts writing at that position.
.I chain
This term is used when
it's the programmer who provides
-a pointer to the terminating null byte in the string
+a pointer to the terminating null character in the string
.I dst
(or one after the last character in a character sequence),
and the function starts writing at that location.
The function returns
-a pointer to the new location of the terminating null byte
+a pointer to the new location of the terminating null character
(or one after the last character in a character sequence)
after the call,
so that the programmer can use it to chain such calls.
@@ -166,11 +162,11 @@ However, newer functions that copy while allowing chaining
cover both use cases with a single API.
They are also algorithmically faster,
since they don't need to search for
-the terminating null byte of the existing string.
+the terminating null character of the existing string.
However, functions that catenate have a much simpler use,
so if performance is not important,
it can make sense to use them for improving readability.
-.PP
+.P
The pointer returned by functions that allow chaining
is a byproduct of the copy operation,
so it has no performance costs.
@@ -180,7 +176,7 @@ have names of the form
.RB * stp *(),
since it's common to name the pointer just
.IR p .
-.PP
+.P
Chain-copying functions that truncate
should accept a pointer to the end of the destination buffer,
and have names of the form
@@ -191,14 +187,14 @@ This allows not having to recalculate the remaining size after each call.
The first thing to note is that programmers should be careful with buffers,
so they always have the correct size,
and truncation is not necessary.
-.PP
+.P
In most cases,
truncation is not desired,
and it is simpler to just do the copy.
Simpler code is safer code.
Programming against programming mistakes by adding more code
just adds more points where mistakes can be made.
-.PP
+.P
Nowadays,
compilers can detect most programmer errors with features like
compiler warnings,
@@ -208,22 +204,25 @@ static analyzers, and
.BR ftm (7)).
Keeping the code simple
helps these overflow-detection features be more precise.
-.PP
+.P
When validating user input,
+code should normally not truncate,
+but instead fail and prevent the copy at all.
+.P
+In some cases,
however,
it makes sense to truncate.
-Remember to check the return value of such function calls.
-.PP
+.P
Functions that truncate:
.IP \[bu] 3
-.BR stpecpy (3)
-is the most efficient string copy function that performs truncation.
-It only requires to check for truncation once after all chained calls.
+.BR stpecpy ()
+.IP \[bu]
+.BR strtcpy ()
.IP \[bu]
.BR strlcpy (3bsd)
and
.BR strlcat (3bsd)
-are similar, but less efficient when chained.
+are similar, but have important performance problems; see BUGS.
.IP \[bu]
.BR stpncpy (3)
and
@@ -233,30 +232,29 @@ but rather null-padded character sequences.
.\" ----- DESCRIPTION :: Null-padded character sequences --------------/
.SS Null-padded character sequences
For historic reasons,
-some standard APIs,
+some standard APIs and file formats,
such as
-.BR utmpx (5),
-use null-padded character sequences in fixed-width buffers.
+.BR utmpx (5)
+and
+.BR tar (1),
+use null-padded character sequences in fixed-size buffers.
To interface with them,
specialized functions need to be used.
-.PP
-To copy strings into them, use
-.BR stpncpy (3).
-.PP
-To copy from an unterminated string within a fixed-width buffer into a string,
-ignoring any trailing null bytes in the source fixed-width buffer,
-you should use
-.BR zustr2stp (3)
+.P
+To copy bytes from strings into these buffers, use
+.BR strncpy (3)
or
-.BR strncat (3).
-.PP
-To copy from an unterminated string within a fixed-width buffer
-into a character sequence,
-ignoring any trailing null bytes in the source fixed-width buffer,
-you should use
-.BR zustr2ustp (3).
-.\" ----- DESCRIPTION :: Measured character sequences -----------------/
-.SS Measured character sequences
+.BR stpncpy (3).
+.P
+To read a null-padded character sequence,
+use
+.IR "strnlen(src,\ NITEMS(src))" ,
+and then you can treat it as a known-length character sequence;
+or use
+.BR strncat (3)
+directly.
+.\" ----- DESCRIPTION :: Known-length character sequences -----------------/
+.SS Known-length character sequences
The simplest character sequence copying function is
.BR mempcpy (3).
It requires always knowing the length of your character sequences,
@@ -266,39 +264,34 @@ since you always know the length of your character sequences,
and can do the minimal copies and length measurements.
.BR mempcpy (3)
copies character sequences,
-so you need to explicitly set the terminating null byte if you need a string.
-.PP
-However,
-for keeping type safety,
-it's good to add a wrapper that uses
-.I char\~*
-instead of
-.IR void\~* :
-.BR ustpcpy (3).
-.PP
+so you need to explicitly set the terminating null character
+if you need a string.
+.P
In programs that make considerable use of strings or character sequences,
and need the best performance,
using overlapping character sequences can make a big difference.
It allows holding subsequences of a larger character sequence,
while not duplicating memory
nor using time to do a copy.
-.PP
+.P
However, this is delicate,
since it requires using character sequences.
C library APIs use strings,
so programs that use character sequences
will have to take care of differentiating strings from character sequences.
-.PP
-To copy a measured character sequence, use
-.BR ustpcpy (3).
-.PP
-To copy a measured character sequence into a string, use
-.BR ustr2stp (3).
-.PP
-Because these functions ask for the length,
-and a string is by nature composed of a character sequence of the same length
-plus a terminating null byte,
-a string is also accepted as input.
+.P
+To copy a known-length character sequence, use
+.BR mempcpy (3).
+.P
+To copy a known-length character sequence into a string, use
+.IR "\%stpcpy(mempcpy(dst,\ src,\ len),\ \[dq]\[dq])" .
+.P
+A string is also accepted as input,
+because
+.BR mempcpy (3)
+asks for the length,
+and a string is composed of a character sequence of the same length
+plus a terminating null character.
.\" ----- DESCRIPTION :: String vs character sequence -----------------/
.SS String vs character sequence
Some functions only operate on strings.
@@ -319,12 +312,14 @@ List of functions:
.BR strcpy (3),
.BR strcat (3)
.IP \[bu]
-.BR stpecpy (3)
+.BR stpecpy ()
+.IP \[bu]
+.BR strtcpy ()
.IP \[bu]
.BR strlcpy (3bsd),
.BR strlcat (3bsd)
.PD
-.PP
+.P
Other functions require an input string,
but create a character sequence as output.
These functions have confusing names,
@@ -336,7 +331,7 @@ List of functions:
.IP \[bu]
.BR strncpy (3)
.PD
-.PP
+.P
Other functions operate on an input character sequence,
and create an output string.
Functions that catenate
@@ -347,29 +342,19 @@ holds a string before the call.
has an even more misleading name than the functions above.
List of functions:
.IP \[bu] 3
-.PD 0
-.BR zustr2stp (3)
-.IP \[bu]
.BR strncat (3)
-.IP \[bu]
-.BR ustr2stp (3)
-.PD
-.PP
+.P
Other functions operate on an input character sequence
to create an output character sequence.
List of functions:
.IP \[bu] 3
-.PD 0
-.BR ustpcpy (3)
-.IP \[bu]
-.BR zustr2stp (3)
-.PD
+.BR mempcpy (3)
.\" ----- DESCRIPTION :: Functions :: ---------------------------------/
.SS Functions
.\" ----- DESCRIPTION :: Functions :: stpcpy(3) -----------------------/
.TP
.BR stpcpy (3)
-This function copies the input string into a destination string.
+Copy the input string into a destination string.
The programmer is responsible for allocating a buffer large enough.
It returns a pointer suitable for chaining.
.\" ----- DESCRIPTION :: Functions :: strcpy(3), strcat(3) ------------/
@@ -377,16 +362,16 @@ It returns a pointer suitable for chaining.
.BR strcpy (3)
.TQ
.BR strcat (3)
-These functions copy and catenate the input string into a destination string.
+Copy and catenate the input string into a destination string.
The programmer is responsible for allocating a buffer large enough.
The return value is useless.
.IP
.BR stpcpy (3)
is a faster alternative to these functions.
-.\" ----- DESCRIPTION :: Functions :: stpecpy(3) ----------------------/
+.\" ----- DESCRIPTION :: Functions :: stpecpy() -----------------------/
.TP
-.BR stpecpy (3)
-This function copies the input string into a destination string.
+.BR stpecpy ()
+Chain-copy the input string into a destination string.
If the destination buffer,
limited by a pointer to its end,
isn't large enough to hold the copy,
@@ -397,12 +382,24 @@ Truncation needs to be detected only once after the last chained call.
.IP
This function is not provided by any library;
see EXAMPLES for a reference implementation.
+.\" ----- DESCRIPTION :: Functions :: strtcpy() -----------------------/
+.TP
+.BR strtcpy ()
+Copy the input string into a destination string.
+If the destination buffer isn't large enough to hold the copy,
+the resulting string is truncated
+(but it is guaranteed to be null-terminated).
+It returns the length of the string,
+or \-1 if it truncated.
+.IP
+This function is not provided by any library;
+see EXAMPLES for a reference implementation.
.\" ----- DESCRIPTION :: Functions :: strlcpy(3bsd), strlcat(3bsd) ----/
.TP
.BR strlcpy (3bsd)
.TQ
.BR strlcat (3bsd)
-These functions copy and catenate the input string into a destination string.
+Copy and catenate the input string into a destination string.
If the destination buffer,
limited by its size,
isn't large enough to hold the copy,
@@ -410,19 +407,23 @@ the resulting string is truncated
(but it is guaranteed to be null-terminated).
They return the length of the total string they tried to create.
.IP
-.BR stpecpy (3)
-is a simpler alternative to these functions.
+Check BUGS before using these functions.
+.IP
+.BR strtcpy ()
+and
+.BR stpecpy ()
+are better alternatives to these functions.
.\" ----- DESCRIPTION :: Functions :: stpncpy(3) ----------------------/
.TP
.BR stpncpy (3)
-This function copies the input string into
-a destination null-padded character sequence in a fixed-width buffer.
+Copy the input string into
+a destination null-padded character sequence in a fixed-size buffer.
If the destination buffer,
limited by its size,
isn't large enough to hold the copy,
the resulting character sequence is truncated.
Since it creates a character sequence,
-it doesn't need to write a terminating null byte.
+it doesn't need to write a terminating null character.
It's impossible to distinguish truncation by the result of the call,
from a character sequence that just fits the destination buffer;
truncation should be detected by
@@ -437,147 +438,105 @@ except for the useless return value.
.IP
.BR stpncpy (3)
is a more useful alternative to this function.
-.\" ----- DESCRIPTION :: Functions :: zustr2ustp(3) --------------------/
-.TP
-.BR zustr2ustp (3)
-This function copies the input character sequence,
-contained in a null-padded fixed-width buffer,
-into a destination character sequence.
-The programmer is responsible for allocating a buffer large enough.
-It returns a pointer suitable for chaining.
-.IP
-A truncating version of this function doesn't exist,
-since the size of the original character sequence is always known,
-so it wouldn't be very useful.
-.IP
-This function is not provided by any library;
-see EXAMPLES for a reference implementation.
-.\" ----- DESCRIPTION :: Functions :: zustr2stp(3) --------------------/
+.\" ----- DESCRIPTION :: Functions :: strncat(3) ----------------------/
.TP
-.BR zustr2stp (3)
-This function copies the input character sequence,
-contained in a null-padded fixed-width buffer,
+.BR strncat (3)
+Catenate the input character sequence,
+contained in a null-padded fixed-size buffer,
into a destination string.
The programmer is responsible for allocating a buffer large enough.
-It returns a pointer suitable for chaining.
-.IP
-A truncating version of this function doesn't exist,
-since the size of the original character sequence is always known,
-so it wouldn't be very useful.
+The return value is useless.
.IP
-This function is not provided by any library;
-see EXAMPLES for a reference implementation.
-.\" ----- DESCRIPTION :: Functions :: strncat(3) ----------------------/
-.TP
-.BR strncat (3)
Do not confuse this function with
.BR strncpy (3);
they are not related at all.
.IP
-This function catenates the input character sequence,
-contained in a null-padded fixed-width buffer,
-into a destination string.
-The programmer is responsible for allocating a buffer large enough.
-The return value is useless.
-.IP
-.BR zustr2stp (3)
+.I \%stpcpy(mempcpy(dst,\ src,\ strnlen(src,\ NITEMS(src))),\ \[dq]\[dq])
is a faster alternative to this function.
-.\" ----- DESCRIPTION :: Functions :: ustpcpy(3) ----------------------/
+.\" ----- DESCRIPTION :: Functions :: mempcpy(3) ----------------------/
.TP
-.BR ustpcpy (3)
-This function copies the input character sequence,
+.BR mempcpy (3)
+Copy the input character sequence,
limited by its length,
into a destination character sequence.
The programmer is responsible for allocating a buffer large enough.
It returns a pointer suitable for chaining.
-.\" ----- DESCRIPTION :: Functions :: ustr2stp(3) ---------------------/
-.TP
-.BR ustr2stp (3)
-This function copies the input character sequence,
-limited by its length,
-into a destination string.
-The programmer is responsible for allocating a buffer large enough.
-It returns a pointer suitable for chaining.
.\" ----- RETURN VALUE :: ---------------------------------------------/
.SH RETURN VALUE
-The following functions return
-a pointer to the terminating null byte in the destination string.
-.IP \[bu] 3
-.PD 0
+.TP
.BR stpcpy (3)
-.IP \[bu]
-.BR ustr2stp (3)
-.IP \[bu]
-.BR zustr2stp (3)
-.PD
-.PP
-The following function returns
-a pointer to the terminating null byte in the destination string,
-except when truncation occurs;
-if truncation occurs,
-it returns a pointer to the end of the destination buffer.
-.IP \[bu] 3
-.BR stpecpy (3)
-.PP
-The following function returns
-a pointer to one after the last character
-in the destination character sequence;
-if truncation occurs,
-that pointer is equivalent to
-a pointer to the end of the destination buffer.
-.IP \[bu] 3
+A pointer to the terminating null character in the destination string.
+.TP
+.BR stpecpy ()
+A pointer to the terminating null character in the destination string,
+on success.
+On error,
+NULL is returned,
+and
+.I errno
+is set to indicate the error.
+.TP
+.BR mempcpy (3)
+.TQ
.BR stpncpy (3)
-.PP
-The following functions return
-a pointer to one after the last character
+A pointer to one after the last character
in the destination character sequence.
-.IP \[bu] 3
-.PD 0
-.BR zustr2ustp (3)
-.IP \[bu]
-.BR ustpcpy (3)
-.PD
-.PP
-The following functions return
-the length of the total string that they tried to create
-(as if truncation didn't occur).
-.IP \[bu] 3
-.BR strlcpy (3bsd),
+.TP
+.BR strtcpy ()
+The length of the string,
+on success.
+On error,
+\-1 is returned,
+and
+.I errno
+is set to indicate the error.
+.TP
+.BR strlcpy (3bsd)
+.TQ
.BR strlcat (3bsd)
-.PP
-The following functions return the
-.I dst
-pointer,
-which is useless.
-.IP \[bu] 3
-.PD 0
-.BR strcpy (3),
+The length of the total string that they tried to create
+(as if truncation didn't occur).
+.TP
+.BR strcpy (3)
+.TQ
.BR strcat (3)
-.IP \[bu]
+.TQ
.BR strncpy (3)
-.IP \[bu]
+.TQ
.BR strncat (3)
-.PD
+The
+.I dst
+pointer,
+which is useless.
+.\" ----- ERRORS ------------------------------------------------------/
+.SH ERRORS
+Most of these functions don't set
+.IR errno .
+.TP
+.BR stpecpy ()
+.TQ
+.BR strtcpy ()
+.RS
+.TP
+.B ENOBUFS
+.I dsize
+was
+.BR 0 .
+.TP
+.B E2BIG
+The string has been truncated.
+.RE
.\" ----- NOTES :: strscpy(9) -----------------------------------------/
.SH NOTES
The Linux kernel has an internal function for copying strings,
-which is similar to
-.BR stpecpy (3),
-except that it can't be chained:
-.TP
-.BR strscpy (9)
-This function copies the input string into a destination string.
-If the destination buffer,
-limited by its size,
-isn't large enough to hold the copy,
-the resulting string is truncated
-(but it is guaranteed to be null-terminated).
-It returns the length of the destination string, or
+.BR strscpy (9),
+which is identical to
+.BR strtcpy (),
+except that it returns
.B \-E2BIG
-on truncation.
-.IP
-.BR stpecpy (3)
-is a simpler and faster alternative to this function.
+instead of \-1
+and it doesn't set
+.IR errno .
.\" ----- CAVEATS :: --------------------------------------------------/
.SH CAVEATS
Don't mix chain calls to truncating and non-truncating functions.
@@ -591,8 +550,28 @@ Calling a non-truncating function after a truncating one is necessarily wrong.
.SH BUGS
All catenation functions share the same performance problem:
.UR https://www.joelonsoftware.com/\:2001/12/11/\:back\-to\-basics/
-Shlemiel the painter
+Shlemiel the painter
.UE .
+As a mitigation,
+compilers are able to transform some calls to catenation functions
+into normal copy functions,
+since
+.I strlen(dst)
+is usually a byproduct of the previous copy.
+.P
+.BR strlcpy (3)
+and
+.BR strlcat (3)
+need to read the entire
+.I src
+string,
+even if the destination buffer is small.
+This makes them vulnerable to Denial of Service (DoS) attacks
+if an attacker can control the length of the
+.I src
+string.
+And if not,
+they're still unnecessarily slow.
.\" ----- EXAMPLES :: -------------------------------------------------/
.SH EXAMPLES
The following are examples of correct use of each of these functions.
@@ -619,43 +598,43 @@ strcat(buf, "!");
len = strlen(buf);
puts(buf);
.EE
-.\" ----- EXAMPLES :: stpecpy(3) --------------------------------------/
+.\" ----- EXAMPLES :: stpecpy() ---------------------------------------/
.TP
-.BR stpecpy (3)
+.BR stpecpy ()
.EX
-end = buf + sizeof(buf);
+end = buf + NITEMS(buf);
p = buf;
p = stpecpy(p, end, "Hello ");
p = stpecpy(p, end, "world");
p = stpecpy(p, end, "!");
-if (p == end) {
- p\-\-;
+if (p == NULL) {
+ len = NITEMS(buf) \- 1;
goto toolong;
}
len = p \- buf;
puts(buf);
.EE
+.\" ----- EXAMPLES :: strtcpy() ---------------------------------------/
+.TP
+.BR strtcpy ()
+.EX
+len = strtcpy(buf, "Hello world!", NITEMS(buf));
+if (len == \-1)
+ goto toolong;
+puts(buf);
+.EE
.\" ----- EXAMPLES :: strlcpy(3bsd), strlcat(3bsd) --------------------/
.TP
.BR strlcpy (3bsd)
.TQ
.BR strlcat (3bsd)
.EX
-if (strlcpy(buf, "Hello ", sizeof(buf)) >= sizeof(buf))
+if (strlcpy(buf, "Hello ", NITEMS(buf)) >= NITEMS(buf))
goto toolong;
-if (strlcat(buf, "world", sizeof(buf)) >= sizeof(buf))
+if (strlcat(buf, "world", NITEMS(buf)) >= NITEMS(buf))
goto toolong;
-len = strlcat(buf, "!", sizeof(buf));
-if (len >= sizeof(buf))
- goto toolong;
-puts(buf);
-.EE
-.\" ----- EXAMPLES :: strscpy(9) --------------------------------------/
-.TP
-.BR strscpy (9)
-.EX
-len = strscpy(buf, "Hello world!", sizeof(buf));
-if (len == \-E2BIG)
+len = strlcat(buf, "!", NITEMS(buf));
+if (len >= NITEMS(buf))
goto toolong;
puts(buf);
.EE
@@ -663,43 +642,40 @@ puts(buf);
.TP
.BR stpncpy (3)
.EX
-p = stpncpy(buf, "Hello world!", sizeof(buf));
-if (sizeof(buf) < strlen("Hello world!"))
+p = stpncpy(u->ut_user, "alx", NITEMS(u->ut_user));
+if (NITEMS(u->ut_user) < strlen("alx"))
goto toolong;
-len = p \- buf;
-for (size_t i = 0; i < sizeof(buf); i++)
- putchar(buf[i]);
+len = p \- u->ut_user;
+fwrite(u->ut_user, 1, len, stdout);
.EE
.\" ----- EXAMPLES :: strncpy(3) --------------------------------------/
.TP
.BR strncpy (3)
.EX
-strncpy(buf, "Hello world!", sizeof(buf));
-if (sizeof(buf) < strlen("Hello world!"))
+strncpy(u->ut_user, "alx", NITEMS(u->ut_user));
+if (NITEMS(u->ut_user) < strlen("alx"))
goto toolong;
-len = strnlen(buf, sizeof(buf));
-for (size_t i = 0; i < sizeof(buf); i++)
- putchar(buf[i]);
+len = strnlen(u->ut_user, NITEMS(u->ut_user));
+fwrite(u->ut_user, 1, len, stdout);
.EE
-.\" ----- EXAMPLES :: zustr2ustp(3) -----------------------------------/
+.\" ----- EXAMPLES :: mempcpy(dst, src, strnlen(src, NITEMS(src))) ----/
.TP
-.BR zustr2ustp (3)
+.I mempcpy(dst, src, strnlen(src, NITEMS(src)))
.EX
+char buf[NITEMS(u->ut_user)];
p = buf;
-p = zustr2ustp(p, "Hello ", 6);
-p = zustr2ustp(p, "world", 42); // Padding null bytes ignored.
-p = zustr2ustp(p, "!", 1);
+p = mempcpy(p, u->ut_user, strnlen(u->ut_user, NITEMS(u->ut_user)));
len = p \- buf;
-printf("%.*s\en", (int) len, buf);
+fwrite(buf, 1, len, stdout);
.EE
-.\" ----- EXAMPLES :: zustr2stp(3) ------------------------------------/
+.\" ----- EXAMPLES :: stpcpy(mempcpy(dst, src, strnlen(src, NITEMS(src))), "")
.TP
-.BR zustr2stp (3)
+.I stpcpy(mempcpy(dst, src, strnlen(src, NITEMS(src))), \[dq]\[dq])
.EX
+char buf[NITEMS(u->ut_user) + 1];
p = buf;
-p = zustr2stp(p, "Hello ", 6);
-p = zustr2stp(p, "world", 42); // Padding null bytes ignored.
-p = zustr2stp(p, "!", 1);
+p = mempcpy(p, u->ut_user, strnlen(u->ut_user, NITEMS(u->ut_user)));
+p = stpcpy(p, "");
len = p \- buf;
puts(buf);
.EE
@@ -707,102 +683,77 @@ puts(buf);
.TP
.BR strncat (3)
.EX
-buf[0] = \[aq]\e0\[aq]; // There's no 'cpy' function to this 'cat'.
-strncat(buf, "Hello ", 6);
-strncat(buf, "world", 42); // Padding null bytes ignored.
-strncat(buf, "!", 1);
+char buf[NITEMS(u->ut_user) + 1];
+strcpy(buf, "");
+strncat(buf, u->ut_user, NITEMS(u->ut_user));
len = strlen(buf);
puts(buf);
.EE
-.\" ----- EXAMPLES :: ustpcpy(3) --------------------------------------/
+.\" ----- EXAMPLES :: mempcpy(3) --------------------------------------/
.TP
-.BR ustpcpy (3)
+.BR mempcpy (3)
.EX
p = buf;
-p = ustpcpy(p, "Hello ", 6);
-p = ustpcpy(p, "world", 5);
-p = ustpcpy(p, "!", 1);
+p = mempcpy(p, "Hello ", 6);
+p = mempcpy(p, "world", 5);
+p = mempcpy(p, "!", 1);
len = p \- buf;
-printf("%.*s\en", (int) len, buf);
+fwrite(buf, 1, len, stdout);
.EE
-.\" ----- EXAMPLES :: ustr2stp(3) -------------------------------------/
+.\" ----- EXAMPLES :: stpcpy(mempcpy(), "") ---------------------------/
.TP
-.BR ustr2stp (3)
+.I stpcpy(mempcpy(dst, src, len), \[dq]\[dq])
.EX
p = buf;
-p = ustr2stp(p, "Hello ", 6);
-p = ustr2stp(p, "world", 5);
-p = ustr2stp(p, "!", 1);
+p = mempcpy(p, "Hello ", 6);
+p = mempcpy(p, "world", 5);
+p = mempcpy(p, "!", 1);
+p = stpcpy(p, "");
len = p \- buf;
puts(buf);
.EE
.\" ----- EXAMPLES :: Implementations :: ------------------------------/
.SS Implementations
Here are reference implementations for functions not provided by libc.
-.PP
+.P
.in +4n
.EX
/* This code is in the public domain. */
\&
-.\" ----- EXAMPLES :: Implementations :: stpecpy(3) -------------------/
+.\" ----- EXAMPLES :: Implementations :: stpecpy() --------------------/
char *
.IR stpecpy "(char *dst, char end[0], const char *restrict src)"
{
- char *p;
+ size_t dlen;
\&
if (dst == NULL)
return NULL;
- if (dst == end)
- return end;
-\&
- p = memccpy(dst, src, \[aq]\e0\[aq], end \- dst);
- if (p != NULL)
- return p \- 1;
\&
- /* truncation detected */
- end[\-1] = \[aq]\e0\[aq];
- return end;
+ dlen = strtcpy(dst, src, end \- dst);
+ return (dlen == \-1) ? NULL : dst + dlen;
}
\&
-.\" ----- EXAMPLES :: Implementations :: zustr2ustp(3) ----------------/
-char *
-.IR zustr2ustp "(char *restrict dst, const char *restrict src, size_t sz)"
+.\" ----- EXAMPLES :: Implementations :: strtcpy() --------------------/
+ssize_t
+.IR strtcpy "(char *restrict dst, const char *restrict src, size_t dsize)"
{
- return ustpcpy(dst, src, strnlen(src, sz));
-}
+ bool trunc;
+ size_t dlen, slen;
\&
-.\" ----- EXAMPLES :: Implementations :: zustr2stp(3) -----------------/
-char *
-.IR zustr2stp "(char *restrict dst, const char *restrict src, size_t sz)"
-{
- char *p;
+ if (dsize == 0) {
+ errno = ENOBUFS;
+ return \-1;
+ }
\&
- p = zustr2ustp(dst, src, sz);
- *p = \[aq]\e0\[aq];
+ slen = strnlen(src, dsize);
+ trunc = (slen == dsize);
+ dlen = slen \- trunc;
\&
- return p;
+ stpcpy(mempcpy(dst, src, dlen), "");
+ if (trunc)
+ errno = E2BIG;
+ return trunc ? \-1 : slen;
}
-\&
-.\" ----- EXAMPLES :: Implementations :: ustpcpy(3) -------------------/
-char *
-.IR ustpcpy "(char *restrict dst, const char *restrict src, size_t len)"
-{
- return mempcpy(dst, src, len);
-}
-\&
-.\" ----- EXAMPLES :: Implementations :: ustr2stp(3) ------------------/
-char *
-.IR ustr2stp "(char *restrict dst, const char *restrict src, size_t len)"
-{
- char *p;
-\&
- p = ustpcpy(dst, src, len);
- *p = \[aq]\e0\[aq];
-\&
- return p;
-}
-.EE
-.in
.\" ----- SEE ALSO :: -------------------------------------------------/
.SH SEE ALSO
.BR bzero (3),