Skip to content

Commit

Permalink
Add implementation for strcat(). Fixes #514 (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin8105 authored and elliotchance committed Jan 1, 2018
1 parent 49fef27 commit 6c701b6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
6 changes: 6 additions & 0 deletions darwin/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 11 additions & 1 deletion noarch/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
6 changes: 6 additions & 0 deletions program/function_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
11 changes: 10 additions & 1 deletion tests/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

int main()
{
plan(24);
plan(25);

diag("TODO: __builtin_object_size")
// https://github.com/elliotchance/c2go/issues/359
Expand Down Expand Up @@ -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();
}

0 comments on commit 6c701b6

Please sign in to comment.