Skip to content

Commit 8fa0198

Browse files
author
Sebastien Bourdeauducq
committedMay 31, 2012
software/libbase: add strcat strncat
1 parent 20b137f commit 8fa0198

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
 

‎software/include/base/string.h

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ char *strcpy(char *dest, const char *src);
2929
char *strncpy(char *dest, const char *src, size_t count);
3030
int strcmp(const char *cs, const char *ct);
3131
int strncmp(const char *cs, const char *ct, size_t count);
32+
char *strcat(char *dest, const char *src);
33+
char *strncat(char *dest, const char *src, size_t n);
3234
size_t strlen(const char *s);
3335
size_t strnlen(const char *s, size_t count);
3436
size_t strspn(const char *s, const char *accept);

‎software/libbase/libc.c

+42
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,48 @@ int strncmp(const char *cs, const char *ct, size_t count)
160160
return __res;
161161
}
162162

163+
/**
164+
* strcat - Append one %NUL-terminated string to another
165+
* @dest: The string to be appended to
166+
* @src: The string to append to it
167+
*/
168+
char *strcat(char *dest, const char *src)
169+
{
170+
char *tmp = dest;
171+
172+
while (*dest)
173+
dest++;
174+
while ((*dest++ = *src++) != '\0')
175+
;
176+
return tmp;
177+
}
178+
179+
/**
180+
* strncat - Append a length-limited, %NUL-terminated string to another
181+
* @dest: The string to be appended to
182+
* @src: The string to append to it
183+
* @count: The maximum numbers of bytes to copy
184+
*
185+
* Note that in contrast to strncpy(), strncat() ensures the result is
186+
* terminated.
187+
*/
188+
char *strncat(char *dest, const char *src, size_t count)
189+
{
190+
char *tmp = dest;
191+
192+
if (count) {
193+
while (*dest)
194+
dest++;
195+
while ((*dest++ = *src++) != 0) {
196+
if (--count == 0) {
197+
*dest = '\0';
198+
break;
199+
}
200+
}
201+
}
202+
return tmp;
203+
}
204+
163205
/**
164206
* strlen - Find the length of a string
165207
* @s: The string to be sized

0 commit comments

Comments
 (0)
Please sign in to comment.