Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
212 dhylands 1
/****************************************************************************
2
*
3
*     Copyright (c) 2003 Dave Hylands
4
*           All Rights Reserved
5
*
6
*       Permission is granted to any individual or institution to use, copy, or
7
*       redistribute this software so long as it is not sold for profit, and that
8
*       this copyright notice is retained.
9
*
10
****************************************************************************/
11
/**
12
*
13
*  @file    StrMaxCpy.cpp
14
*
15
*  @brief   Implements StrMaxCpy, a bounded string copy routine.
16
*
17
****************************************************************************/
18
 
19
/* ---- Include Files ---------------------------------------------------- */
20
 
21
#include "Str.h"
22
#include <string.h>
23
 
24
/* ---- Public Variables ------------------------------------------------- */
25
/* ---- Private Constants and Types -------------------------------------- */
26
/* ---- Private Variables ------------------------------------------------ */
27
/* ---- Private Function Prototypes -------------------------------------- */
28
/* ---- Functions -------------------------------------------------------- */
29
 
30
/**
31
 * @addtogroup Str
32
 * @{
33
 */
34
 
35
/***************************************************************************/
36
/**
37
*   Copies the source to the destination, but makes sure that the
38
*   destination string (including terminating null), doesn't exceed
39
*   maxLen.
40
*
41
*   @param  dst     (out) Place to store the string copy.
42
*   @param  src     (in)  String to copy.
43
*   @param  maxLen  (in)  Maximum number of characters to copy into @a dst.
44
*
45
*   @return A pointer to the destination string.
46
*/
47
 
48
char *StrMaxCpy( char *dst, const char *src, size_t maxLen )
49
{
50
        if ( maxLen < 1 )
51
        {
52
                /*
53
                 * There's no room in the buffer?
54
                 */
55
 
56
                return "";
57
        }
58
 
59
        if ( maxLen == 1 )
60
        {
61
                /*
62
                 * There's only room for the terminating null character
63
                 */
64
 
65
                dst[ 0 ] = '\0';
66
                return dst;
67
        }
68
 
69
        /*
70
         * The Visual C++ version of strncpy writes to every single character
71
         * of the destination buffer, so we use a length one character smaller
72
         * and write in our own null (if required).
73
     *
74
     * This allows the caller to store a sentinel in the last byte of the
75
     * buffer to detect overflows (if desired).
76
         */
77
 
78
        strncpy( dst, src, maxLen - 1 );
79
        if (( strlen( src ) + 1 ) >= maxLen )
80
        {
81
                /*
82
                 * The string exactly fits, or probably overflows the buffer.
83
                 * Write in the terminating null character since strncpy doesn't in
84
                 * this particular case.
85
                 *
86
                 * We don't do this arbitrarily so that the caller can use a sentinel
87
                 * in the very end of the buffer to detect buffer overflows.
88
                 */
89
 
90
                dst[ maxLen - 1 ] = '\0';
91
        }
92
 
93
        return dst;
94
 
95
} /* StrMaxCpy */
96
 
97
/** @} */
98