PatchworkOS
Loading...
Searching...
No Matches
strncat_s.c
Go to the documentation of this file.
2#include <errno.h>
3#include <stdint.h>
4#include <stdlib.h>
5#include <string.h>
6
8
9errno_t strncat_s(char* _RESTRICT s1, rsize_t s1max, const char* _RESTRICT s2, rsize_t n)
10{
11 char* dest = s1;
12 const char* src = s2;
13
14 if (s1 != NULL && s2 != NULL && s1max <= RSIZE_MAX && n <= RSIZE_MAX && s1max != 0)
15 {
16 while (*dest)
17 {
18 if (--s1max == 0 || dest++ == s2)
19 {
20 goto runtime_constraint_violation;
21 }
22 }
23
24 do
25 {
26 if (n-- == 0)
27 {
28 *dest = '\0';
29 return 0;
30 }
31
32 if (s1max-- == 0 || dest == s2 || src == s1)
33 {
34 goto runtime_constraint_violation;
35 }
36 } while ((*dest++ = *src++));
37
38 return 0;
39 }
40
41runtime_constraint_violation:
42
43 if (s1 != NULL && s1max > 0 && s1max <= RSIZE_MAX)
44 {
45 s1[0] = '\0';
46 }
47
49 return EINVAL;
50}
constraint_handler_t _constraintHandler
#define _CONSTRAINT_VIOLATION(e)
int errno_t
Definition errno_t.h:4
#define EINVAL
Invalid argument.
Definition errno.h:142
#define NULL
Pointer error value.
Definition NULL.h:23
#define _RESTRICT
Definition config.h:17
__SIZE_TYPE__ rsize_t
Definition rsize_t.h:4
errno_t strncat_s(char *_RESTRICT s1, rsize_t s1max, const char *_RESTRICT s2, rsize_t n)
Definition strncat_s.c:9