1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <Strn.h> 5 6 int main(int argc, char **argv) 7 { 8 char a[8]; 9 char pad1[32]; 10 char *b; 11 char c[37]; 12 char pad2[23]; 13 int i; 14 int len1, len2; 15 16 b = Strncpy(a, "hello", sizeof(a)); 17 b = Strncat(b, "world", sizeof(a)); 18 printf("1: result=[%s] should be=[%s]\n", 19 b, 20 "hellowo" 21 ); 22 23 for (i=0; i<sizeof(c); i++) 24 c[i] = 'X'; 25 b = Strncpy(c, "testing", sizeof(c) - 2); 26 #if (STRN_ZERO_PAD == 1) 27 for (i=7; i<sizeof(c) - 2; i++) { 28 if (c[i] != '\0') { 29 printf("2: did not clear to end of buffer\n"); 30 break; 31 } 32 } 33 #endif 34 for (i=sizeof(c) - 2; i<sizeof(c); i++) { 35 if (c[i] != 'X') { 36 printf("2: overwrote buffer\n"); 37 break; 38 } 39 } 40 41 for (i=0; i<sizeof(c); i++) 42 c[i] = 'X'; 43 b = Strncpy(c, "testing", sizeof(c) - 2); 44 b = Strncat(b, " still", sizeof(c) - 2); 45 #if (STRN_ZERO_PAD == 1) 46 for (i=13; i<sizeof(c) - 2; i++) { 47 if (c[i] != '\0') { 48 printf("3: did not clear to end of buffer\n"); 49 break; 50 } 51 } 52 #endif 53 for (i=sizeof(c) - 2; i<sizeof(c); i++) { 54 if (c[i] != 'X') { 55 printf("3: overwrote buffer\n"); 56 break; 57 } 58 } 59 60 /*--------------*/ 61 62 b = Strnpcpy(a, "hello", sizeof(a)); 63 len1 = (int) (b - a); 64 b = Strnpcat(a, "world", sizeof(a)); 65 len2 = (int) (b - a); 66 printf("4: result=[%s] should be=[%s] len1=%d len2=%d\n", 67 a, 68 "hellowo", 69 len1, 70 len2 71 ); 72 73 for (i=0; i<sizeof(c); i++) 74 c[i] = 'X'; 75 b = Strnpcpy(c, "testing", sizeof(c) - 2); 76 #if (STRNP_ZERO_PAD == 1) 77 for (i=7; i<sizeof(c) - 2; i++) { 78 if (c[i] != '\0') { 79 printf("5: did not clear to end of buffer\n"); 80 break; 81 } 82 } 83 #endif 84 for (i=sizeof(c) - 2; i<sizeof(c); i++) { 85 if (c[i] != 'X') { 86 printf("5: overwrote buffer\n"); 87 break; 88 } 89 } 90 91 for (i=0; i<sizeof(c); i++) 92 c[i] = 'X'; 93 b = Strnpcpy(c, "testing", sizeof(c) - 2); 94 b = Strnpcat(c, " still", sizeof(c) - 2); 95 #if (STRNP_ZERO_PAD == 1) 96 for (i=13; i<sizeof(c) - 2; i++) { 97 if (c[i] != '\0') { 98 printf("6: did not clear to end of buffer\n"); 99 break; 100 } 101 } 102 #endif 103 for (i=sizeof(c) - 2; i<sizeof(c); i++) { 104 if (c[i] != 'X') { 105 printf("6: overwrote buffer\n"); 106 break; 107 } 108 } 109 110 /*--------------*/ 111 { 112 char *str; 113 114 str = NULL; 115 if (Dynscat(&str, "this is a test", 0) == NULL) { 116 printf("7a: fail\n"); 117 } else if (strcmp(str, "this is a test") != 0) { 118 printf("7b: fail\n"); 119 } 120 free(str); 121 122 str = NULL; 123 if (Dynscat(&str, "this is a test", 0) == NULL) { 124 printf("7c: fail\n"); 125 } else if (strcmp(str, "this is a test") != 0) { 126 printf("7d: fail\n"); 127 } else if (Dynscat(&str, " ", "", "and", " ", "so is this", 0) == NULL) { 128 printf("7e: fail\n"); 129 } else if (strcmp(str, "this is a test and so is this") != 0) { 130 printf("7f: fail\n"); 131 } 132 free(str); 133 } 134 exit(0); 135 } 136