blob: 6553b55e2ff90067b492b5ce65cb71008c994552 (
plain)
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
|
/* miscstr.c -- misc string routines */
/* $Header$ */
#include <stdlib.h>
#include <string.h>
#include "../include/stdc.h"
/*
* exceptionally, this (C++) header file is safe to #include from C,
* and contains a prototype for AHFinderDirect_Strlcat() .
*/
#include "util.hh"
/******************************************************************************/
/*@@
@routine AHFinderDirect_Strlcat
@date 16.Feb.2003
@author Jonathan Thornburg <jthorn@aei.mpg.de>
@desc This function implements the strcat() function
described in
http://www.openbsd.org/papers/strlcpy-paper.ps
The strlcat(3) function appends the null-terminated string
src to the end of dst. It will append at most
size - strlen(dst) - 1
characters, and always null-terminates the result.
(Hence this function never overflows the destination buffer.)
strlcat(3) is a replacement for strncat(3). In comparison
to strncat(3), strlcat(3) is safer and easier to use:
it guarantees null termination of the destination buffer,
and the size parameter is easy to specify without danger
of off-by-one errors.
@enddesc
@var dst
@vdesc A non-null pointer to the destination buffer.
@vtype char* dst
@endvar
@var src
@vdesc A non-null pointer to the source string.
@vtype const char* dst
@endvar
@var dst_size
@vdesc The size of the destination buffer.
@vtype size_t dst_size
@endvar
@returntype size_t
@returndesc This function returns the length of the string it
tries to create, i.e. strlen(src) + strlen(dst() .
@endreturndesc
@@*/
size_t AHFinderDirect_Strlcat(char* dst, const char* src, size_t dst_size)
{
const size_t src_len = strlen(src);
const size_t dst_len = strlen(dst);
const int dst_remaining = dst_size - dst_len - 1;
if (dst_remaining > 0)
then strncat(dst, src, dst_remaining);
return src_len + dst_len;
}
|