From 6c701b6e9f99c53630a511f4a0a0cd13db8dea80 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Tue, 2 Jan 2018 01:22:17 +0300 Subject: [PATCH] Add implementation for strcat(). Fixes #514 (#516) --- darwin/string.go | 6 ++++++ noarch/string.go | 12 +++++++++++- program/function_definition.go | 6 ++++++ tests/string.c | 11 ++++++++++- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/darwin/string.go b/darwin/string.go index 3f5b980ee..b5eada3fa 100644 --- a/darwin/string.go +++ b/darwin/string.go @@ -19,3 +19,9 @@ func BuiltinObjectSize(ptr []byte, theType int) int { func BuiltinStrncpy(dest, src []byte, len, size int) []byte { return noarch.Strncpy(dest, src, len) } + +// BuiltinStrcat is for __builtin___strcat_chk +// https://opensource.apple.com/source/Libc/Libc-763.12/include/secure/_string.h.auto.html +func BuiltinStrcat(dest, src []byte, _ int) []byte { + return noarch.Strcat(dest, src) +} diff --git a/noarch/string.go b/noarch/string.go index 967841a12..89c73fcc7 100644 --- a/noarch/string.go +++ b/noarch/string.go @@ -26,7 +26,7 @@ func Strcpy(dest, src []byte) []byte { // We only need to copy until the first NULL byte. Make sure we also // include that NULL byte on the end. - if c == 0 { + if c == '\x00' { break } } @@ -59,3 +59,13 @@ func Strncpy(dest, src []byte, len int) []byte { return dest } + +// Strcat - concatenate strings +// Appends a copy of the source string to the destination string. +// The terminating null character in destination is overwritten by the first +// character of source, and a null-character is included at the end +// of the new string formed by the concatenation of both in destination. +func Strcat(dest, src []byte) []byte { + Strcpy(dest[Strlen(dest):], src) + return dest +} diff --git a/program/function_definition.go b/program/function_definition.go index 2e330a149..368fffa7e 100644 --- a/program/function_definition.go +++ b/program/function_definition.go @@ -168,6 +168,8 @@ var builtInFunctionDefinitions = []string{ "int fsetpos(FILE*, int*) -> noarch.Fsetpos", // string.h + "char* strcat(char *, const char *) -> noarch.Strcat", + "char* strcpy(const char*, char*) -> noarch.Strcpy", // should be: "char* strncpy(const char*, char*, size_t) -> noarch.Strncpy", "char* strncpy(const char*, char*, int) -> noarch.Strncpy", @@ -185,6 +187,10 @@ var builtInFunctionDefinitions = []string{ // should be: size_t __builtin_object_size(const void*, int) "int __builtin_object_size(const char*, int) -> darwin.BuiltinObjectSize", + // see https://opensource.apple.com/source/Libc/Libc-763.12/include/secure/_string.h.auto.html + "char* __builtin___strcat_chk(char *, const char *, int) -> darwin.BuiltinStrcat", + "char* __inline_strcat_chk(char *, const char *) -> noarch.Strcat", + // stdlib.h "int abs(int) -> noarch.Abs", "double atof(const char *) -> noarch.Atof", diff --git a/tests/string.c b/tests/string.c index c65dfd095..6b0df3241 100644 --- a/tests/string.c +++ b/tests/string.c @@ -3,7 +3,7 @@ int main() { - plan(24); + plan(25); diag("TODO: __builtin_object_size") // https://github.com/elliotchance/c2go/issues/359 @@ -81,6 +81,15 @@ int main() // is_eq(strlen(NULL), 0); is_eq(strlen("fo\0o"), 2); } + { + diag("strcat") + char str[80]; + strcpy (str,"these "); + strcat (str,"strings "); + strcat (str,"are "); + strcat (str,"concatenated."); + is_streq(str,"these strings are concatenated."); + } done_testing(); }