xref: /illumos-gate/usr/src/cmd/dtrace/test/tst/common/funcs/tst.substr.d (revision 6009dbc6db3fe0e2890761d3d562bb4c4b9bf0be)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #pragma D option quiet
30 #pragma D option strsize=32
31 
32 struct {
33 	int index;
34 	int length;
35 	int nolen;
36 	int alt;
37 } command[int];
38 
39 int i;
40 
41 BEGIN
42 {
43 	str = "foobarbazbop";
44 	str2 = "";
45 	altstr = "CRAIG: Positioned them, I don't ";
46 	altstr2 = "know... I'm fairly wide guy.";
47 
48 	command[i].index = 3;
49 	command[i].nolen = 1;
50 	i++;
51 
52 	command[i].index = 300;
53 	command[i].nolen = 1;
54 	i++;
55 
56 	command[i].index = -10;
57 	command[i].nolen = 1;
58 	i++;
59 
60 	command[i].index = 0;
61 	command[i].nolen = 1;
62 	i++;
63 
64 	command[i].index = 1;
65 	command[i].nolen = 1;
66 	i++;
67 
68 	command[i].index = strlen(str) - 1;
69 	command[i].nolen = 1;
70 	i++;
71 
72 	command[i].index = strlen(str);
73 	command[i].nolen = 1;
74 	i++;
75 
76 	command[i].index = strlen(str) + 1;
77 	command[i].nolen = 1;
78 	i++;
79 
80 	command[i].index = 8;
81 	command[i].length = 20;
82 	i++;
83 
84 	command[i].index = 4;
85 	command[i].length = 4;
86 	i++;
87 
88 	command[i].index = 5;
89 	command[i].length = strlen(str) - command[i].index + 1;
90 	i++;
91 
92 	command[i].index = 5;
93 	command[i].length = strlen(str) - command[i].index + 2;
94 	i++;
95 
96 	command[i].index = 400;
97 	command[i].length = 20;
98 	i++;
99 
100 	command[i].index = 400;
101 	command[i].length = 0;
102 	i++;
103 
104 	command[i].index = 400;
105 	command[i].length = -1;
106 	i++;
107 
108 	command[i].index = 3;
109 	command[i].length = 0;
110 	i++;
111 
112 	command[i].index = 3;
113 	command[i].length = -1;
114 	i++;
115 
116 	command[i].index = 3;
117 	command[i].length = -4;
118 	i++;
119 
120 	command[i].index = 3;
121 	command[i].length = -20;
122 	i++;
123 
124 	command[i].index = -10;
125 	command[i].length = -5;
126 	i++;
127 
128 	command[i].index = 0;
129 	command[i].length = 400;
130 	i++;
131 
132 	command[i].index = -1;
133 	command[i].length = 400;
134 	i++;
135 
136 	command[i].index = -1;
137 	command[i].length = 0;
138 	i++;
139 
140 	command[i].index = -1;
141 	command[i].length = -1;
142 	i++;
143 
144 	command[i].index = -2 * strlen(str);
145 	command[i].length = 2 * strlen(str);
146 	i++;
147 
148 	command[i].index = -2 * strlen(str);
149 	command[i].length = strlen(str);
150 	i++;
151 
152 	command[i].index = -2 * strlen(str);
153 	command[i].length = strlen(str) + 1;
154 	i++;
155 
156 	command[i].index = -1 * strlen(str);
157 	command[i].length = strlen(str);
158 	i++;
159 
160 	command[i].index = -1 * strlen(str);
161 	command[i].length = strlen(str) - 1;
162 	i++;
163 
164 	command[i].index = 100;
165 	command[i].length = 10;
166 	command[i].alt = 1;
167 	i++;
168 
169 	command[i].index = 100;
170 	command[i].nolen = 1;
171 	command[i].alt = 1;
172 	i++;
173 
174 	end = i;
175 	i = 0;
176 	printf("#!/usr/perl5/bin/perl\n\nBEGIN {\n");
177 
178 }
179 
180 tick-1ms
181 /i < end && command[i].nolen/
182 {
183 	this->str = command[i].alt ? altstr : str;
184 	this->str2 = command[i].alt ? altstr2 : str2;
185 	this->result = substr(command[i].alt ?
186 	    "CRAIG: Positioned them, I don't know... I'm fairly wide guy." :
187 	    str, command[i].index);
188 
189 	printf("\tif (substr(\"%s%s\", %d) ne \"%s\") {\n",
190 	    this->str, this->str2, command[i].index, this->result);
191 
192 	printf("\t\tprintf(\"perl => substr(\\\"%s%s\\\", %d) = ",
193 	    this->str, this->str2, command[i].index);
194 	printf("\\\"%%s\\\"\\n\",\n\t\t    substr(\"%s%s\", %d));\n",
195 	    this->str, this->str2, command[i].index);
196 	printf("\t\tprintf(\"   D => substr(\\\"%s%s\\\", %d) = ",
197 	    this->str, this->str2, command[i].index);
198 	printf("\\\"%%s\\\"\\n\",\n\t\t    \"%s\");\n", this->result);
199 	printf("\t\t$failed++;\n");
200 	printf("\t}\n\n");
201 }
202 
203 tick-1ms
204 /i < end && !command[i].nolen/
205 {
206 	this->str = command[i].alt ? altstr : str;
207 	this->str2 = command[i].alt ? altstr2 : str2;
208 	this->result = substr(command[i].alt ?
209 	    "CRAIG: Positioned them, I don't know... I'm fairly wide guy." :
210 	    str, command[i].index, command[i].length);
211 
212 	printf("\tif (substr(\"%s%s\", %d, %d) ne \"%s\") {\n",
213 	    this->str, this->str2, command[i].index, command[i].length,
214 	    this->result);
215 	printf("\t\tprintf(\"perl => substr(\\\"%s%s\\\", %d, %d) = ",
216 	    this->str, this->str2, command[i].index, command[i].length);
217 	printf("\\\"%%s\\\"\\n\",\n\t\t    substr(\"%s%s\", %d, %d));\n",
218 	    this->str, this->str2, command[i].index, command[i].length);
219 	printf("\t\tprintf(\"   D => substr(\\\"%s%s\\\", %d, %d) = ",
220 	    this->str, this->str2, command[i].index, command[i].length);
221 	printf("\\\"%%s\\\"\\n\",\n\t\t    \"%s\");\n", this->result);
222 	printf("\t\t$failed++;\n");
223 	printf("\t}\n\n");
224 }
225 
226 tick-1ms
227 /++i == end/
228 {
229 	printf("\texit($failed);\n}\n");
230 	exit(0);
231 }
232