scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

0021-strncpy.c (709B)


      1 #include <assert.h>
      2 #include <stdio.h>
      3 #include <string.h>
      4 
      5 #define SIZ 6
      6 
      7 /*
      8 output:
      9 testing
     10 test1
     11 test2
     12 test3
     13 done
     14 end:
     15 */
     16 
     17 int
     18 main()
     19 {
     20 	char *s, buf[SIZ];
     21 
     22 	puts("testing");
     23 
     24 	puts("test1");
     25 	memset(buf, '0', SIZ);
     26 	s = strncpy(buf, "abc", sizeof(buf));
     27 	assert(s == buf);
     28 	assert(!memcmp(s, (char[SIZ]) {"abc"}, sizeof(buf)));
     29 
     30 	puts("test2");
     31 	memset(buf, '0', SIZ);
     32 	s = strncpy(buf, "", sizeof(buf));
     33 	assert(s == buf);
     34 	assert(!memcmp(s, (char[SIZ]) {'\0'}, sizeof(buf)));
     35 
     36 	puts("test3");
     37 	memset(buf, '0', SIZ);
     38 	s = strncpy(buf, "", 1);
     39 	assert(s == buf);
     40 	assert(!memcmp(s,
     41 	               (char[SIZ]) {'\0', '0', '0', '0', '0', '0'},
     42 	               sizeof(buf)));
     43 
     44 	puts("done");
     45 
     46 	return 0;
     47 }