1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
.\"-------
.\" Dd distance to space vertically before a "display"
.\" These are what n/troff use for interparagraph distance
.\"-------
.if t .nr Dd .4v
.if n .nr Dd 1v
.\"-------
.\" Sp space down the interparagraph distance
.\"-------
.de Sp
.sp \\n(Ddu
..
.\"-------
.\" Ds begin a display, indented .5 inches from the surrounding text.
.\"
.\" Note that uses of Ds and De may NOT be nested.
.\"-------
.de Ds
.Sp
.in +0.5i
.nf
..
.\"-------
.\" De end a display (no trailing vertical spacing)
.\"-------
.de De
.fi
.in
..
.\"-------
.\" Xs begin a display
.\"
.\" Note that uses of Ds and De may NOT be nested.
.\"-------
.de Xs
.Sp
.nf
..
.\"-------
.\" Xe end a display (no trailing vertical spacing)
.\"-------
.de Xe
.fi
.in
..
.TH Strn 3 "" "West Interactive Corporation"
.SH NAME
Strncpy(), Strncat() - Safe string copying functions
.SH "SYNOPSIS"
.Xs
cc \-I/usr/src/include \. \. \. \-L/usr/src/lib -lStrn
#include <string.h>
#include <Strn.h>
char *Strncpy(char *dst, char *src, size_t max);
char *Strncat(char *dst, char *src, size_t max);
.Xe
.\"-------
.SH "DESCRIPTION"
.\"-------
.PP
The
.I Strn
library provides bounds-safe versions of string copying and concatenation
functions.
These are useful with fixed length character arrays because the functions
will not overwrite past the end of the array, unlike
.IR strcat " or " "strncat" "."
The functions also guarantee that a string is nul-terminated, unlike
.IR strncpy " or " "strncat" "."
.PP
For example:
.Ds
char bar1[8], foo[8], bar2[8];
.De
In the following block, Strncpy correctly sets this to "hello w",
using seven characters + a nul terminator, so the array is full and
nul-terminated.
.Ds
Strncpy(foo, "hello world", sizeof(foo));
.De
strncpy will write 8 characters, but it won't nul-terminate.
If you tried to print "foo" you would get "hello wo" and garbage.
.Ds
strncpy(foo, "hello world", sizeof(foo));
.De
These cooperate to guarantee that the bounds of "foo" are not violated.
Here, you would get "hello" and then "hello w".
.Ds
Strncpy(foo, "hello", sizeof(foo));
Strncat(foo, " world", sizeof(foo));
.De
These two would overflow, with this usage.
The strncpy would work, but then the strncat would happily write
past the end of foo.
.Ds
strncpy(foo, "hello", sizeof(foo));
strncat(foo, " world", sizeof(foo));
.De
To get the result I want, I would have
to do this, which with all the extra
overhead in the third argument is unacceptable.
.Ds
strncpy(foo, "hello", sizeof(foo));
strncat(foo, " world", sizeof(foo) - strlen(foo) - 1);
.De
In actual use, you don't usually call
.IR Strncpy " or " Strncat " directly."
The library's header file includes two macros which fill in the third
argument with sizeof(dst) for you.
That way, they can be used as direct replacements of
.IR strcpy " and " strcat
most of the time:
.Ds
char fullName[32];
strcpy(fullName, firstName);
strcat(fullName, " ");
strcat(fullName, lastName);
.De
This can be transformed into the following, and you can rest assured
that you won't trash the stack if for some reason "firstName" + "lastName"
would exceed the bounds of "fullName."
.Ds
STRNCPY(fullName, firstName);
STRNCAT(fullName, " ");
STRNCAT(fullName, lastName);
.De
.\"-------
.SH "SEE ALSO"
.\"-------
.IR strcpy (3),
.IR strncpy (3),
.IR strcat (3),
.IR strncat (3),
.IR sprintf (3).
|