1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*** |
6 | *tcsncat_s.inl - general implementation of _tcscpy_s |
7 | * |
8 | |
9 | * |
10 | *Purpose: |
11 | * This file contains the general algorithm for strncat_s and its variants. |
12 | * |
13 | ****/ |
14 | |
15 | _FUNC_PROLOGUE |
16 | errno_t __cdecl _FUNC_NAME(_CHAR *_DEST, size_t _SIZE, const _CHAR *_SRC, size_t _COUNT) |
17 | { |
18 | _CHAR *p; |
19 | size_t available; |
20 | |
21 | if (_COUNT == 0 && _DEST == NULL && _SIZE == 0) |
22 | { |
23 | /* this case is allowed; nothing to do */ |
24 | _RETURN_NO_ERROR; |
25 | } |
26 | |
27 | /* validation section */ |
28 | _VALIDATE_STRING(_DEST, _SIZE); |
29 | if (_COUNT != 0) |
30 | { |
31 | _VALIDATE_POINTER_RESET_STRING(_SRC, _DEST, _SIZE); |
32 | } |
33 | |
34 | p = _DEST; |
35 | available = _SIZE; |
36 | while (available > 0 && *p != 0) |
37 | { |
38 | p++; |
39 | available--; |
40 | } |
41 | |
42 | if (available == 0) |
43 | { |
44 | _RESET_STRING(_DEST, _SIZE); |
45 | _RETURN_DEST_NOT_NULL_TERMINATED(_DEST, _SIZE); |
46 | } |
47 | |
48 | if (_COUNT == _TRUNCATE) |
49 | { |
50 | while ((*p++ = *_SRC++) != 0 && --available > 0) |
51 | { |
52 | } |
53 | } |
54 | else |
55 | { |
56 | _ASSERT_EXPR((!_CrtGetCheckCount() || _COUNT < available), "Buffer is too small" ); |
57 | |
58 | while (_COUNT > 0 && (*p++ = *_SRC++) != 0 && --available > 0) |
59 | { |
60 | _COUNT--; |
61 | } |
62 | if (_COUNT == 0) |
63 | { |
64 | *p = 0; |
65 | } |
66 | } |
67 | |
68 | if (available == 0) |
69 | { |
70 | if (_COUNT == _TRUNCATE) |
71 | { |
72 | _DEST[_SIZE - 1] = 0; |
73 | _RETURN_TRUNCATE; |
74 | } |
75 | _RESET_STRING(_DEST, _SIZE); |
76 | _RETURN_BUFFER_TOO_SMALL(_DEST, _SIZE); |
77 | } |
78 | _FILL_STRING(_DEST, _SIZE, _SIZE - available + 1); |
79 | _RETURN_NO_ERROR; |
80 | } |
81 | |
82 | |