$ cat strncpy.c
#include <stdio.h>
#define MSL 16
main() {
char a[MSL];
strncpy(a,"Well how do you do my friend?\n",MSL);
printf("%s",a);
}
jlambert@atlas ~
$ gcc strncpy.c ; ./a
Well how do you !al@
$ cat strncpy.c
#include <stdio.h>
#define MSL 16
main() {
char a[MSL];
strncpy(a,"Well how do you do my friend?\n",MSL);
printf("%s",a);
}
jlambert@atlas ~
$ gcc strncpy.c ; ./a
Well how do you !al@
char a[MSL];
char a[MSL + 1];
strncpy(buf, "Crashy long buffer here", MAX_STRING_LENGTH - 1);
char * my_strncpy ( char * destination, const char * source, size_t num ) {
strncpy(destinacion, source);
destination[num-1] = '\0';
return destination;
}
#define my_strncpy(dst,src,sz) strncpy(dst, src, sz); (dst)[sz] = '\0';
$ cat strncpy.c
#define MSL 16
main() {
char a[MSL];
char b[MSL];
char c[MSL];
/* proper use of strncpy */
strncpy(a,"Well how do you do my friend.\n",MSL);
a[MSL-1] = '\0';
printf("%s",a);
/* proper use of strncat */
strncpy(b,"Hello",MSL);
strncat(b,", how do you do my friend.\n",MSL-strlen(b));
b[MSL-1] = '\0';
printf("%s",b);
/* proper use of snprintf */
snprintf(c,MSL,"Well how do you do my friend.\n");
printf("%s",c);
}
jlambert@atlas ~
$ gcc strncpy.c ; ./a
Well how do youHello, how do yWell how do you