1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #include "maindefines.h"
4 #include <string.h>
5 
6 /*
7  * This file has to be C90 compatible, as it is not only used by the engine,
8  * but also by AIs, which might be compiled with compilers (for example VS)
9  * that do not support C99.
10  *
11  * Using these functions also allows us to easily check where we are
12  * sometimes writing to too small buffer, or where our source is bad,
13  * for example when it is too long.
14  */
15 
16 #ifdef	__cplusplus
17 extern "C" {
18 #endif
19 
safe_strcpy(char * destination,size_t destinationSize,const char * source)20 char* safe_strcpy(char* destination, size_t destinationSize, const char* source)
21 {
22 	if ((destination != NULL) && (destinationSize > 0)) {
23 		destination[destinationSize - 1] = '\0';
24 		destination = STRNCPY(destination, source, destinationSize - 1);
25 	}
26 
27 	return destination;
28 }
29 
safe_strcat(char * destination,size_t destinationSize,const char * source)30 char* safe_strcat(char* destination, size_t destinationSize, const char* source)
31 {
32 	if ((destination != NULL) && (destinationSize > 0)) {
33 		destination[destinationSize - 1] = '\0';
34 		destination = STRNCAT(destination, source, destinationSize - strlen(destination) - 1);
35 	}
36 
37 	return destination;
38 }
39 
40 #ifdef	__cplusplus
41 } // extern "C"
42 #endif
43