summaryrefslogtreecommitdiff
path: root/src/string
diff options
context:
space:
mode:
Diffstat (limited to 'src/string')
-rw-r--r--src/string/memcpy_s.c35
-rw-r--r--src/string/memmove_s.c34
-rw-r--r--src/string/memset_s.c33
-rw-r--r--src/string/strcat_s.c29
-rw-r--r--src/string/strcpy_s.c29
-rw-r--r--src/string/strerror_s.c35
-rw-r--r--src/string/strerrorlen_s.c13
-rw-r--r--src/string/strncat_s.c44
-rw-r--r--src/string/strncpy_s.c43
-rw-r--r--src/string/strnlen_s.c17
-rw-r--r--src/string/strtok_s.c26
11 files changed, 338 insertions, 0 deletions
diff --git a/src/string/memcpy_s.c b/src/string/memcpy_s.c
new file mode 100644
index 00000000..366a879c
--- /dev/null
+++ b/src/string/memcpy_s.c
@@ -0,0 +1,35 @@
+#include "string.h"
+#include "sys/_nonstd_.h"
+
+/** copy memory **/
+errno_t memcpy_s(void * restrict s1, rsize_t s1max, const void * restrict s2, rsize_t n)
+{
+ __C_EXT(1, 201112L);
+ __ASSERT_NONNULL(s1);
+ __ASSERT_NONNULL(s2);
+ __ASSERT_NOOVERLAP(s1, s2, n);
+
+ char *dst = (char*)s1, *src = (char*)s2;
+ int i = 0;
+ while (i < n) {
+ dst[i] = src[i];
+ i++;
+ }
+ return dst;
+}
+
+/***
+The fn(memcpy) copies arg(n) bytes from the object at arg(s2) to the object at
+arg(s1).
+***/
+
+/* UNSPECIFIED: - */
+/* UNDEFINED: if the objects overlap */
+/* IMPLEMENTATION: - */
+/* LOCALE: - */
+
+/* RETURN: the value of arg(s1) */
+
+/*
+CEXT1(201112)
+*/
diff --git a/src/string/memmove_s.c b/src/string/memmove_s.c
new file mode 100644
index 00000000..c489161d
--- /dev/null
+++ b/src/string/memmove_s.c
@@ -0,0 +1,34 @@
+#include "string.h"
+#include "stdlib.h"
+#include "sys/_nonstd_.h"
+
+/** move memory **/
+errno_t memmove_s(void *s1, rsize_t s1max, const void *s2, rsize_t n)
+{
+ __C_EXT(1, 201112L);
+ __ASSERT_NONNULL(s1);
+ __ASSERT_NONNULL(s2);
+
+ void *buf = malloc(n);
+ memcpy(buf, s2, n);
+ memcpy(s1, buf, n);
+ free(buf);
+ return s1;
+}
+
+/***
+The fn(memmove) function copies arg(n) bytes of memory from the object at
+arg(s2) to the object at arg(s1). If arg(s1) and arg(s2) overlap, the memory
+is copied so that the arg(n) bytes are safely written to arg(s1).
+***/
+
+/* UNSPECIFIED: - */
+/* UNDEFINED: - */
+/* IMPLEMENTATION: - */
+/* LOCALE: - */
+
+/* RETURN: the value of arg(s1) */
+
+/*
+CEXT1(201112)
+*/
diff --git a/src/string/memset_s.c b/src/string/memset_s.c
new file mode 100644
index 00000000..71e3add1
--- /dev/null
+++ b/src/string/memset_s.c
@@ -0,0 +1,33 @@
+#include "string.h"
+
+/** fill memory **/
+errno_t memset_s(void *s, rsize_t smax, int c, rsize_t n)
+{
+ __C_EXT(1, 201112L);
+ __ASSERT_NONNULL(s);
+
+ unsigned char *_s = (unsigned char *)s;
+ int i = 0;
+
+ while (i < n) {
+ _s[i] = (unsigned char)c;
+ }
+
+ return s;
+}
+
+/***
+The fn(memset) function fills the first arg(n) bytes of memory at arg(s) with
+the value arg(c) (converted to an type(unsigned char)).
+***/
+
+/* UNSPECIFIED: - */
+/* UNDEFINED: - */
+/* IMPLEMENTATION: - */
+/* LOCALE: - */
+
+/* RETURN: the value of arg(s) */
+
+/*
+CEXT1(201112)
+*/
diff --git a/src/string/strcat_s.c b/src/string/strcat_s.c
new file mode 100644
index 00000000..14db822d
--- /dev/null
+++ b/src/string/strcat_s.c
@@ -0,0 +1,29 @@
+#include "string.h"
+
+/** concatenate strings **/
+errno_t strcat_s(char * restrict s1, rsize_t s1max, const char * restrict s2)
+{
+ __C_EXT(1, 201112L);
+ __ASSERT_NONNULL(s1);
+ __ASSERT_NONNULL(s2);
+ __ASSERT_NOOVERLAP(s1, s2, strlen(s1) + strlen(s2));
+
+ return strncat(s1, s2, strlen(s2));
+}
+
+/***
+The fn(strcat) function appends a copy of the string at arg(s2) to the end of
+the string at arg(s1). The first byte of arg(s2) will overwrite the terminating
+null character of arg(s1).
+***/
+
+/* UNSPECIFIED: - */
+/* UNDEFINED: if arg(s1) and arg(s2) overlap */
+/* IMPLEMENTATION: - */
+/* LOCALE: - */
+
+/* RETURN: the value of arg(s1) */
+
+/*
+CEXT1(201112)
+*/
diff --git a/src/string/strcpy_s.c b/src/string/strcpy_s.c
new file mode 100644
index 00000000..15e624b4
--- /dev/null
+++ b/src/string/strcpy_s.c
@@ -0,0 +1,29 @@
+#include "string.h"
+#include <limits.h>
+
+/** copy string **/
+errno_t strcpy_s(char * restrict s1, rsize_t s1max, const char * restrict s2)
+{
+ __C_EXT(1, 201112L);
+ __ASSERT_NONNULL(s1);
+ __ASSERT_NONNULL(s2);
+ __ASSERT_NOOVERLAP(s1, s2, strlen(s2));
+
+ return strncpy(s1, s2, strlen(s2));
+}
+
+/***
+The fn(strcpy) function copies the string at arg(s2) to arg(s1), up to and
+including the terminating char(\0).
+***/
+
+/* UNSPECIFIED: - */
+/* UNDEFINED: if arg(s1) and arg(s2) overlap */
+/* IMPLEMENTATION: - */
+/* LOCALE: - */
+
+/* RETURN: the value of arg(s1) */
+
+/*
+CEXT1(201112)
+*/
diff --git a/src/string/strerror_s.c b/src/string/strerror_s.c
new file mode 100644
index 00000000..293bb81d
--- /dev/null
+++ b/src/string/strerror_s.c
@@ -0,0 +1,35 @@
+#include "string.h"
+#include "errno.h"
+#include "__strerror.h"
+
+/** convert error number to string **/
+errno_t strerror_s(char *s, rsize_t maxsize, errno_t errnum)
+{
+ __C_EXT(1, 201112L);
+ if (errnum > __nstrerror || __strerror[errnum] == NULL) {
+ if (snprintf(s, maxsize, "Uknown error [%d]", errnum) < maxsize) {
+ return 0;
+ }
+ return 1;
+ }
+
+ strncpy(s, __strerror[errnum], maxsize);
+ return errnum;
+}
+
+/***
+The fn(strerror_s) converts the error number arg(errnum) to an error message
+string. The string returned should not be modified, and may be overwritten by
+subsequent calls.
+***/
+
+/* UNSPECIFIED: - */
+/* UNDEFINED: - */
+/* IMPLEMENTATION: - */
+/* LOCALE: LC_MESSAGES */
+
+/* RETURN: a pointer to the error message string */
+
+/*
+CEXT1(201112)
+*/
diff --git a/src/string/strerrorlen_s.c b/src/string/strerrorlen_s.c
new file mode 100644
index 00000000..8b0d99bf
--- /dev/null
+++ b/src/string/strerrorlen_s.c
@@ -0,0 +1,13 @@
+#include "string.h"
+
+size_t strerrorlen_s(errno_t errnum)
+{
+ __C_EXT(1, 201112L);
+ char buffer[1024];
+ strerror_s(buffer, sizeof(buffer), errnum);
+ return strlen(buffer);
+}
+
+/*
+CEXT1(201112)
+*/
diff --git a/src/string/strncat_s.c b/src/string/strncat_s.c
new file mode 100644
index 00000000..2c6d65e6
--- /dev/null
+++ b/src/string/strncat_s.c
@@ -0,0 +1,44 @@
+#include "string.h"
+
+/** concatenate bounded string **/
+errno_t strncat_s(char * restrict s1, rsize_t s1max, const char * restrict s2, rsize_t n)
+{
+ __C_EXT(1, 201112L);
+ //__ASSERT_NONNULL(s1);
+ //__ASSERT_NONNULL(s2);
+ //__ASSERT_NOOVERLAP(s1, s2, strlen(s1) + strlen(s2));
+
+ char *append = s1 + strlen(s1);
+ for (size_t i = 0; i < n; i++) {
+ *append = s2[i];
+ if (*append == '\0') {
+ break;
+ }
+ append++;
+ }
+
+ if (append[-1] != '\0') {
+ *append = '\0';
+ }
+
+ return s1;
+}
+
+/***
+The fn(strncat) function appends a copy of the frist arg(n) bytes of the string
+at arg(s2) to the end of the string at arg(s1). The first byte of arg(s2) will
+overwrite the terminating null character of arg(s1). No characters after the
+first char(\0) will be copied. The resulting string will always be null
+terminated.
+***/
+
+/* UNSPECIFIED: - */
+/* UNDEFINED: arg(s1) and arg(s2) overlap */
+/* IMPLEMENTATION: - */
+/* LOCALE: - */
+
+/* RETURN: the value of arg(s1) */
+
+/*
+CEXT1(201112)
+*/
diff --git a/src/string/strncpy_s.c b/src/string/strncpy_s.c
new file mode 100644
index 00000000..39b045a9
--- /dev/null
+++ b/src/string/strncpy_s.c
@@ -0,0 +1,43 @@
+#include "string.h"
+
+/** copy bounded string **/
+errno_t strncpy_s(char * restrict s1, rsize_t s1max, const char * restrict s2, rsize_t n)
+{
+ __C_EXT(1, 201112L);
+ __ASSERT_NONNULL(s1);
+ __ASSERT_NONNULL(s2);
+ __ASSERT_NOOVERLAP(s1, s2, n);
+
+ size_t i;
+ for (i = 0; i < n; i++) {
+ s1[i] = s2[i];
+ if (s1[i] == '\0') {
+ break;
+ }
+ }
+
+ while (i < n) {
+ s1[i++] = '\0';
+ }
+
+ return s1;
+}
+
+/***
+The fn(strncpy) function copies up to arg(n) bytes from the string at arg(s2)
+to arg(s1). If a char(\0) is encountered, null characters are appended to
+arg(s1) until arg(n) bytes have been written. If no null characters are copied
+in the first arg(n) bytes of arg(s2), the resulting string will not be null
+terminated.
+***/
+
+/* UNSPECIFIED: - */
+/* UNDEFINED: if arg(s1) and arg(s2) overlap */
+/* IMPLEMENTATION: - */
+/* LOCALE: - */
+
+/* RETURN: the value of arg(s1) */
+
+/*
+CEXT1(201112)
+*/
diff --git a/src/string/strnlen_s.c b/src/string/strnlen_s.c
new file mode 100644
index 00000000..f1221961
--- /dev/null
+++ b/src/string/strnlen_s.c
@@ -0,0 +1,17 @@
+#include "string.h"
+
+size_t strnlen_s(const char *s, size_t maxsize)
+{
+ __C_EXT(1, 201112L);
+ size_t i = 0;
+ while (i < maxlen) {
+ if (s[i] == '\0')
+ return i;
+ i++;
+ }
+ return i;
+}
+
+/*
+CEXT1(201112)
+*/
diff --git a/src/string/strtok_s.c b/src/string/strtok_s.c
new file mode 100644
index 00000000..9742d09c
--- /dev/null
+++ b/src/string/strtok_s.c
@@ -0,0 +1,26 @@
+#include "string.h"
+#include "stddef.h"
+
+char * strtok_s(char * restrict s1, rsize_t * restrict s1max, const char * restrict s2, char **restrict ptr)
+{
+ __C_EXT(1, 201112L);
+ int i = 0;
+
+ if (s == NULL)
+ s = *lasts;
+
+ while (i < strlen (s)) {
+ if (strchr (sep, s[i]) == NULL) {
+ i++;
+ } else {
+ s[i] = '\0';
+ *lasts = &(s[i+1]);
+ return s;
+ }
+ }
+ return NULL;
+}
+
+/*
+CEXT1(201112)
+*/