xref: /illumos-gate/usr/src/cmd/dtrace/test/tst/common/funcs/tst.substr.d (revision fc910014e8a32a65612105835a10995f2c13d942)
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 D option quiet
28 #pragma D option strsize=32
29 
30 struct {
31 	int index;
32 	int length;
33 	int nolen;
34 	int alt;
35 } command[int];
36 
37 int i;
38 
39 BEGIN
40 {
41 	str = "foobarbazbop";
42 	str2 = "";
43 	altstr = "CRAIG: Positioned them, I don't ";
44 	altstr2 = "know... I'm fairly wide guy.";
45 
46 	command[i].index = 3;
47 	command[i].nolen = 1;
48 	i++;
49 
50 	command[i].index = 300;
51 	command[i].nolen = 1;
52 	i++;
53 
54 	command[i].index = -10;
55 	command[i].nolen = 1;
56 	i++;
57 
58 	command[i].index = 0;
59 	command[i].nolen = 1;
60 	i++;
61 
62 	command[i].index = 1;
63 	command[i].nolen = 1;
64 	i++;
65 
66 	command[i].index = strlen(str) - 1;
67 	command[i].nolen = 1;
68 	i++;
69 
70 	command[i].index = strlen(str);
71 	command[i].nolen = 1;
72 	i++;
73 
74 	command[i].index = strlen(str) + 1;
75 	command[i].nolen = 1;
76 	i++;
77 
78 	command[i].index = 8;
79 	command[i].length = 20;
80 	i++;
81 
82 	command[i].index = 4;
83 	command[i].length = 4;
84 	i++;
85 
86 	command[i].index = 5;
87 	command[i].length = strlen(str) - command[i].index + 1;
88 	i++;
89 
90 	command[i].index = 5;
91 	command[i].length = strlen(str) - command[i].index + 2;
92 	i++;
93 
94 	command[i].index = 400;
95 	command[i].length = 20;
96 	i++;
97 
98 	command[i].index = 400;
99 	command[i].length = 0;
100 	i++;
101 
102 	command[i].index = 400;
103 	command[i].length = -1;
104 	i++;
105 
106 	command[i].index = 3;
107 	command[i].length = 0;
108 	i++;
109 
110 	command[i].index = 3;
111 	command[i].length = -1;
112 	i++;
113 
114 	command[i].index = 3;
115 	command[i].length = -4;
116 	i++;
117 
118 	command[i].index = 3;
119 	command[i].length = -20;
120 	i++;
121 
122 	command[i].index = -10;
123 	command[i].length = -5;
124 	i++;
125 
126 	command[i].index = 0;
127 	command[i].length = 400;
128 	i++;
129 
130 	command[i].index = -1;
131 	command[i].length = 400;
132 	i++;
133 
134 	command[i].index = -1;
135 	command[i].length = 0;
136 	i++;
137 
138 	command[i].index = -1;
139 	command[i].length = -1;
140 	i++;
141 
142 	command[i].index = -2 * strlen(str);
143 	command[i].length = 2 * strlen(str);
144 	i++;
145 
146 	command[i].index = -2 * strlen(str);
147 	command[i].length = strlen(str);
148 	i++;
149 
150 	command[i].index = -2 * strlen(str);
151 	command[i].length = strlen(str) + 1;
152 	i++;
153 
154 	command[i].index = -1 * strlen(str);
155 	command[i].length = strlen(str);
156 	i++;
157 
158 	command[i].index = -1 * strlen(str);
159 	command[i].length = strlen(str) - 1;
160 	i++;
161 
162 	command[i].index = 100;
163 	command[i].length = 10;
164 	command[i].alt = 1;
165 	i++;
166 
167 	command[i].index = 100;
168 	command[i].nolen = 1;
169 	command[i].alt = 1;
170 	i++;
171 
172 	end = i;
173 	i = 0;
174 	printf("#!/usr/perl5/bin/perl\n\nBEGIN {\n");
175 
176 }
177 
178 tick-1ms
179 /i < end && command[i].nolen/
180 {
181 	this->str = command[i].alt ? altstr : str;
182 	this->str2 = command[i].alt ? altstr2 : str2;
183 	this->result = substr(command[i].alt ?
184 	    "CRAIG: Positioned them, I don't know... I'm fairly wide guy." :
185 	    str, command[i].index);
186 
187 	printf("\tif (substr(\"%s%s\", %d) ne \"%s\") {\n",
188 	    this->str, this->str2, command[i].index, this->result);
189 
190 	printf("\t\tprintf(\"perl => substr(\\\"%s%s\\\", %d) = ",
191 	    this->str, this->str2, command[i].index);
192 	printf("\\\"%%s\\\"\\n\",\n\t\t    substr(\"%s%s\", %d));\n",
193 	    this->str, this->str2, command[i].index);
194 	printf("\t\tprintf(\"   D => substr(\\\"%s%s\\\", %d) = ",
195 	    this->str, this->str2, command[i].index);
196 	printf("\\\"%%s\\\"\\n\",\n\t\t    \"%s\");\n", this->result);
197 	printf("\t\t$failed++;\n");
198 	printf("\t}\n\n");
199 }
200 
201 tick-1ms
202 /i < end && !command[i].nolen/
203 {
204 	this->str = command[i].alt ? altstr : str;
205 	this->str2 = command[i].alt ? altstr2 : str2;
206 	this->result = substr(command[i].alt ?
207 	    "CRAIG: Positioned them, I don't know... I'm fairly wide guy." :
208 	    str, command[i].index, command[i].length);
209 
210 	printf("\tif (substr(\"%s%s\", %d, %d) ne \"%s\") {\n",
211 	    this->str, this->str2, command[i].index, command[i].length,
212 	    this->result);
213 	printf("\t\tprintf(\"perl => substr(\\\"%s%s\\\", %d, %d) = ",
214 	    this->str, this->str2, command[i].index, command[i].length);
215 	printf("\\\"%%s\\\"\\n\",\n\t\t    substr(\"%s%s\", %d, %d));\n",
216 	    this->str, this->str2, command[i].index, command[i].length);
217 	printf("\t\tprintf(\"   D => substr(\\\"%s%s\\\", %d, %d) = ",
218 	    this->str, this->str2, command[i].index, command[i].length);
219 	printf("\\\"%%s\\\"\\n\",\n\t\t    \"%s\");\n", this->result);
220 	printf("\t\t$failed++;\n");
221 	printf("\t}\n\n");
222 }
223 
224 tick-1ms
225 /++i == end/
226 {
227 	printf("\texit($failed);\n}\n");
228 	exit(0);
229 }
230