xref: /titanic_41/usr/src/cmd/sendmail/libsm/t-strl.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *	All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
6*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
7*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
12*7c478bd9Sstevel@tonic-gate 
13*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
14*7c478bd9Sstevel@tonic-gate SM_IDSTR(id, "@(#)$Id: t-strl.c,v 1.13 2001/08/27 23:00:05 gshapiro Exp $")
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
17*7c478bd9Sstevel@tonic-gate #include <stdio.h>
18*7c478bd9Sstevel@tonic-gate #include <sm/heap.h>
19*7c478bd9Sstevel@tonic-gate #include <sm/string.h>
20*7c478bd9Sstevel@tonic-gate #include <sm/test.h>
21*7c478bd9Sstevel@tonic-gate 
22*7c478bd9Sstevel@tonic-gate #define MAXL	16
23*7c478bd9Sstevel@tonic-gate #define N	5
24*7c478bd9Sstevel@tonic-gate #define SIZE	128
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate int
27*7c478bd9Sstevel@tonic-gate main(argc, argv)
28*7c478bd9Sstevel@tonic-gate 	int argc;
29*7c478bd9Sstevel@tonic-gate 	char *argv[];
30*7c478bd9Sstevel@tonic-gate {
31*7c478bd9Sstevel@tonic-gate 	char *s1, *s2, *s3;
32*7c478bd9Sstevel@tonic-gate 	int one, two, k;
33*7c478bd9Sstevel@tonic-gate 	char src1[N][SIZE], dst1[SIZE], dst2[SIZE];
34*7c478bd9Sstevel@tonic-gate 	char *r;
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate 	sm_test_begin(argc, argv, "test strl* string functions");
37*7c478bd9Sstevel@tonic-gate 	s1 = "abc";
38*7c478bd9Sstevel@tonic-gate 	s2 = "123";
39*7c478bd9Sstevel@tonic-gate 	s3 = sm_malloc_x(MAXL);
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
42*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(s1, s3) == 0);
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcat(s3, s2, 8) == 6);
45*7c478bd9Sstevel@tonic-gate 	r ="abc123";
46*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(s3, r) == 0);
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcpy(s3, s1, 2) == 3);
49*7c478bd9Sstevel@tonic-gate 	r = "a";
50*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(s3, r) == 0);
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcat(s3, s2, 3) == 4);
53*7c478bd9Sstevel@tonic-gate 	r = "a1";
54*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(s3, r) == 0);
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
57*7c478bd9Sstevel@tonic-gate 	r = ":";
58*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcat2(s3, r, s2, MAXL) == 7);
59*7c478bd9Sstevel@tonic-gate 	r = "abc:123";
60*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(s3, r) == 0);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
63*7c478bd9Sstevel@tonic-gate 	r = ":";
64*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcat2(s3, r, s2, 6) == 7);
65*7c478bd9Sstevel@tonic-gate 	r = "abc:1";
66*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(s3, r) == 0);
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
69*7c478bd9Sstevel@tonic-gate 	r = ":";
70*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcat2(s3, r, s2, 2) == 7);
71*7c478bd9Sstevel@tonic-gate 	r = "abc";
72*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(s3, r) == 0);
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
75*7c478bd9Sstevel@tonic-gate 	r = ":";
76*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcat2(s3, r, s2, 4) == 7);
77*7c478bd9Sstevel@tonic-gate 	r = "abc";
78*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(s3, r) == 0);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
81*7c478bd9Sstevel@tonic-gate 	r = ":";
82*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcat2(s3, r, s2, 5) == 7);
83*7c478bd9Sstevel@tonic-gate 	r = "abc:";
84*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(s3, r) == 0);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
87*7c478bd9Sstevel@tonic-gate 	r = ":";
88*7c478bd9Sstevel@tonic-gate 	SM_TEST(sm_strlcat2(s3, r, s2, 6) == 7);
89*7c478bd9Sstevel@tonic-gate 	r = "abc:1";
90*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(s3, r) == 0);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	for (k = 0; k < N; k++)
93*7c478bd9Sstevel@tonic-gate 	{
94*7c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(src1[k], "abcdef", sizeof src1);
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	one = sm_strlcpyn(dst1, sizeof dst1, 3, src1[0], "/", src1[1]);
98*7c478bd9Sstevel@tonic-gate 	two = sm_snprintf(dst2, sizeof dst2, "%s/%s", src1[0], src1[1]);
99*7c478bd9Sstevel@tonic-gate 	SM_TEST(one == two);
100*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(dst1, dst2) == 0);
101*7c478bd9Sstevel@tonic-gate 	one = sm_strlcpyn(dst1, 10, 3, src1[0], "/", src1[1]);
102*7c478bd9Sstevel@tonic-gate 	two = sm_snprintf(dst2, 10, "%s/%s", src1[0], src1[1]);
103*7c478bd9Sstevel@tonic-gate 	SM_TEST(one == two);
104*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(dst1, dst2) == 0);
105*7c478bd9Sstevel@tonic-gate 	one = sm_strlcpyn(dst1, 5, 3, src1[0], "/", src1[1]);
106*7c478bd9Sstevel@tonic-gate 	two = sm_snprintf(dst2, 5, "%s/%s", src1[0], src1[1]);
107*7c478bd9Sstevel@tonic-gate 	SM_TEST(one == two);
108*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(dst1, dst2) == 0);
109*7c478bd9Sstevel@tonic-gate 	one = sm_strlcpyn(dst1, 0, 3, src1[0], "/", src1[1]);
110*7c478bd9Sstevel@tonic-gate 	two = sm_snprintf(dst2, 0, "%s/%s", src1[0], src1[1]);
111*7c478bd9Sstevel@tonic-gate 	SM_TEST(one == two);
112*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(dst1, dst2) == 0);
113*7c478bd9Sstevel@tonic-gate 	one = sm_strlcpyn(dst1, sizeof dst1, 5, src1[0], "/", src1[1], "/", src1[2]);
114*7c478bd9Sstevel@tonic-gate 	two = sm_snprintf(dst2, sizeof dst2, "%s/%s/%s", src1[0], src1[1], src1[2]);
115*7c478bd9Sstevel@tonic-gate 	SM_TEST(one == two);
116*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(dst1, dst2) == 0);
117*7c478bd9Sstevel@tonic-gate 	one = sm_strlcpyn(dst1, 15, 5, src1[0], "/", src1[1], "/", src1[2]);
118*7c478bd9Sstevel@tonic-gate 	two = sm_snprintf(dst2, 15, "%s/%s/%s", src1[0], src1[1], src1[2]);
119*7c478bd9Sstevel@tonic-gate 	SM_TEST(one == two);
120*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(dst1, dst2) == 0);
121*7c478bd9Sstevel@tonic-gate 	one = sm_strlcpyn(dst1, 20, 5, src1[0], "/", src1[1], "/", src1[2]);
122*7c478bd9Sstevel@tonic-gate 	two = sm_snprintf(dst2, 20, "%s/%s/%s", src1[0], src1[1], src1[2]);
123*7c478bd9Sstevel@tonic-gate 	SM_TEST(one == two);
124*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(dst1, dst2) == 0);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	one = sm_strlcpyn(dst1, sizeof dst1, 0);
127*7c478bd9Sstevel@tonic-gate 	SM_TEST(one == 0);
128*7c478bd9Sstevel@tonic-gate 	r = "";
129*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(dst1, r) == 0);
130*7c478bd9Sstevel@tonic-gate 	one = sm_strlcpyn(dst1, 20, 1, src1[0]);
131*7c478bd9Sstevel@tonic-gate 	two = sm_snprintf(dst2, 20, "%s", src1[0]);
132*7c478bd9Sstevel@tonic-gate 	SM_TEST(one == two);
133*7c478bd9Sstevel@tonic-gate 	one = sm_strlcpyn(dst1, 2, 1, src1[0]);
134*7c478bd9Sstevel@tonic-gate 	two = sm_snprintf(dst2, 2, "%s", src1[0]);
135*7c478bd9Sstevel@tonic-gate 	SM_TEST(one == two);
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	return sm_test_end();
138*7c478bd9Sstevel@tonic-gate }
139