From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/EXAMPLES/strncpy.c | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 reference/C/EXAMPLES/strncpy.c (limited to 'reference/C/EXAMPLES/strncpy.c') diff --git a/reference/C/EXAMPLES/strncpy.c b/reference/C/EXAMPLES/strncpy.c new file mode 100644 index 0000000..c6c0054 --- /dev/null +++ b/reference/C/EXAMPLES/strncpy.c @@ -0,0 +1,48 @@ + +/**************************************************************** + * + * Purpose: Program to demonstrate the 'strncpy' function. + * Author: M J Leslie + * Date: 03-Feb-96 + * + ****************************************************************/ + +#include /* strcpy */ + +void SafeCopy(char *Dest, int DestSize, char *Source); + +main() +{ + char Text1[20]="Tracy Sorrell"; /* string buffer */ + char Text2[10]="Martin"; /* string buffer */ + + printf (" Original string contents are: %s\n", Text2); + + SafeCopy(Text2, sizeof(Text2), Text1); + + printf (" New string contents are: %s\n", Text2); + + strcpy(Text2, "Alex"); + + printf (" Final string contents are: %s\n", Text2); + +} + +/****************************************************************/ + +void SafeCopy( + char *Dest, /* Destination buffer. */ + int DestSize, + char *Source) /* Source data. */ +{ + + /* ... Copy 'Source' into 'Dest'. + * ... 'Dest' is padded with NULLs if 'Source' is smaller.. */ + + strncpy(Dest, Source, DestSize); + + /* ... Safety net! Add the NULL just in case 'Source' is larger + * ... than 'Dest'. */ + + Dest[DestSize-1] = '\0'; +} -- cgit v1.2.3-54-g00ecf