sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
* THELLOU.C
|
sl@0
|
16 |
* A variant of "Hello World" which reports various bits of its environment
|
sl@0
|
17 |
* and returns an interesting exit status
|
sl@0
|
18 |
*
|
sl@0
|
19 |
*
|
sl@0
|
20 |
*/
|
sl@0
|
21 |
|
sl@0
|
22 |
|
sl@0
|
23 |
|
sl@0
|
24 |
#include <stdio.h>
|
sl@0
|
25 |
#include <stdlib.h>
|
sl@0
|
26 |
#include <unistd.h>
|
sl@0
|
27 |
#include <string.h>
|
sl@0
|
28 |
#include <sys/wait.h>
|
sl@0
|
29 |
#include "CTEST.H"
|
sl@0
|
30 |
|
sl@0
|
31 |
|
sl@0
|
32 |
#ifdef __WINS__
|
sl@0
|
33 |
int triangle_recurse(wchar_t* prog, wchar_t* num, int progress)
|
sl@0
|
34 |
{
|
sl@0
|
35 |
printf("\nBut not under WINS where I'll be told 0\n");
|
sl@0
|
36 |
return 0;
|
sl@0
|
37 |
}
|
sl@0
|
38 |
|
sl@0
|
39 |
int triangle_parallel(wchar_t* prog, wchar_t* num)
|
sl@0
|
40 |
{
|
sl@0
|
41 |
printf("\nBut not under WINS where I'll be told 0\n");
|
sl@0
|
42 |
return 0;
|
sl@0
|
43 |
}
|
sl@0
|
44 |
|
sl@0
|
45 |
#else
|
sl@0
|
46 |
|
sl@0
|
47 |
int triangle_recurse(wchar_t* prog, wchar_t* num, int progress)
|
sl@0
|
48 |
{
|
sl@0
|
49 |
int n;
|
sl@0
|
50 |
int pid, ret;
|
sl@0
|
51 |
char cmd[100];
|
sl@0
|
52 |
wchar_t wcmd[100];
|
sl@0
|
53 |
int fids[3];
|
sl@0
|
54 |
char tmpbuf[100];
|
sl@0
|
55 |
|
sl@0
|
56 |
|
sl@0
|
57 |
wcstombs(tmpbuf, num, 50);
|
sl@0
|
58 |
n = atol(tmpbuf);
|
sl@0
|
59 |
if (n<1)
|
sl@0
|
60 |
return 0;
|
sl@0
|
61 |
|
sl@0
|
62 |
wcstombs(tmpbuf, prog, 50);
|
sl@0
|
63 |
sprintf(cmd, "%s do_triangle %d", tmpbuf, n-1);
|
sl@0
|
64 |
mbstowcs(wcmd, cmd, 100);
|
sl@0
|
65 |
if (progress)
|
sl@0
|
66 |
{
|
sl@0
|
67 |
pid=wpopen3(wcmd, (wchar_t*)L"", 0, fids);
|
sl@0
|
68 |
if (pid<0)
|
sl@0
|
69 |
{
|
sl@0
|
70 |
fprintf(stderr, "Executing %s, ", cmd);
|
sl@0
|
71 |
perror("wpopen3 failed");
|
sl@0
|
72 |
return -1999999;
|
sl@0
|
73 |
}
|
sl@0
|
74 |
while (1)
|
sl@0
|
75 |
{
|
sl@0
|
76 |
int n=waitpid(pid, &ret, WNOHANG);
|
sl@0
|
77 |
if (n==pid)
|
sl@0
|
78 |
break;
|
sl@0
|
79 |
printf(".");
|
sl@0
|
80 |
fflush(stdout);
|
sl@0
|
81 |
sleep(1);
|
sl@0
|
82 |
}
|
sl@0
|
83 |
}
|
sl@0
|
84 |
else
|
sl@0
|
85 |
ret=wsystem(wcmd);
|
sl@0
|
86 |
return n+ret;
|
sl@0
|
87 |
}
|
sl@0
|
88 |
|
sl@0
|
89 |
int triangle_parallel(wchar_t* prog, wchar_t* num)
|
sl@0
|
90 |
{
|
sl@0
|
91 |
int n;
|
sl@0
|
92 |
int pid, pid1, pid2, ret1, ret2, base, split;
|
sl@0
|
93 |
char cmd[100];
|
sl@0
|
94 |
wchar_t wcmd[100];
|
sl@0
|
95 |
int fids1[3], fids2[3];
|
sl@0
|
96 |
char* basep;
|
sl@0
|
97 |
char tmpbuf[100];
|
sl@0
|
98 |
|
sl@0
|
99 |
wcstombs(tmpbuf, num, 50);
|
sl@0
|
100 |
n = atol(tmpbuf);
|
sl@0
|
101 |
if (n<1)
|
sl@0
|
102 |
return 0;
|
sl@0
|
103 |
basep=getenv("TRIANGLE_BASE");
|
sl@0
|
104 |
if (basep==0)
|
sl@0
|
105 |
return 0;
|
sl@0
|
106 |
base=atol(basep);
|
sl@0
|
107 |
|
sl@0
|
108 |
/* we have to add up the numbers base..n inclusive
|
sl@0
|
109 |
*/
|
sl@0
|
110 |
if (base==n)
|
sl@0
|
111 |
return n;
|
sl@0
|
112 |
if (base+1==n)
|
sl@0
|
113 |
return base+n;
|
sl@0
|
114 |
|
sl@0
|
115 |
/* At least 3 numbers, so split it into subtasks for child processes
|
sl@0
|
116 |
*/
|
sl@0
|
117 |
split = (n-base)/2;
|
sl@0
|
118 |
|
sl@0
|
119 |
wcstombs(tmpbuf, prog, 100);
|
sl@0
|
120 |
sprintf(cmd, "%s do_trianglep %d", tmpbuf, base+split);
|
sl@0
|
121 |
mbstowcs(wcmd, cmd, 100);
|
sl@0
|
122 |
pid1=wpopen3(wcmd, (wchar_t*)L"", 0, fids1);
|
sl@0
|
123 |
if (pid1<0)
|
sl@0
|
124 |
{
|
sl@0
|
125 |
fprintf(stderr, "Doing %d..%d of %d..%d", base+split, base, n, base);
|
sl@0
|
126 |
perror("popen3 failed");
|
sl@0
|
127 |
return -1999999;
|
sl@0
|
128 |
}
|
sl@0
|
129 |
|
sl@0
|
130 |
sprintf(cmd, "%d", base+split+1);
|
sl@0
|
131 |
mbstowcs(wcmd, cmd, 100);
|
sl@0
|
132 |
wsetenv((wchar_t*)L"TRIANGLE_BASE", wcmd, 1);
|
sl@0
|
133 |
|
sl@0
|
134 |
sprintf(cmd, "%s do_trianglep %d", prog, n);
|
sl@0
|
135 |
mbstowcs(wcmd, cmd, 100);
|
sl@0
|
136 |
pid2=wpopen3(wcmd, (wchar_t*)L"", 0, fids2);
|
sl@0
|
137 |
if (pid2<0)
|
sl@0
|
138 |
{
|
sl@0
|
139 |
fprintf(stderr, "Doing %d..%d of %d..%d", n, base+split+1, n, base);
|
sl@0
|
140 |
perror("wpopen3 failed");
|
sl@0
|
141 |
return -1999999;
|
sl@0
|
142 |
}
|
sl@0
|
143 |
|
sl@0
|
144 |
/* Now collect the results */
|
sl@0
|
145 |
ret1=-10000;
|
sl@0
|
146 |
ret2=-10000;
|
sl@0
|
147 |
for (n=0; n<2; n++)
|
sl@0
|
148 |
{
|
sl@0
|
149 |
int ret=-3999999;
|
sl@0
|
150 |
pid=wait(&ret);
|
sl@0
|
151 |
if (pid<0)
|
sl@0
|
152 |
{
|
sl@0
|
153 |
perror("waitpid failed");
|
sl@0
|
154 |
return -2999999;
|
sl@0
|
155 |
}
|
sl@0
|
156 |
if (pid==pid1)
|
sl@0
|
157 |
{
|
sl@0
|
158 |
pid1=-1;
|
sl@0
|
159 |
ret1=ret;
|
sl@0
|
160 |
}
|
sl@0
|
161 |
else if (pid==pid2)
|
sl@0
|
162 |
{
|
sl@0
|
163 |
pid2=-1;
|
sl@0
|
164 |
ret2=ret;
|
sl@0
|
165 |
}
|
sl@0
|
166 |
else
|
sl@0
|
167 |
printf("Unexpected pid %d (not %d or %d)\n", pid, pid1, pid2);
|
sl@0
|
168 |
}
|
sl@0
|
169 |
return ret1+ret2;
|
sl@0
|
170 |
}
|
sl@0
|
171 |
|
sl@0
|
172 |
#endif
|
sl@0
|
173 |
|
sl@0
|
174 |
/**
|
sl@0
|
175 |
Usage: THELLOU [triangle[p] <n>]
|
sl@0
|
176 |
|
sl@0
|
177 |
@SYMTestCaseID SYSLIB-STDLIB-CT-1096
|
sl@0
|
178 |
@SYMTestCaseDesc Tests for printing to standard output stream
|
sl@0
|
179 |
@SYMTestPriority High
|
sl@0
|
180 |
@SYMTestActions Prints some stuff (looks at args, getenv("USER"), cwd) to stdout and stderr.
|
sl@0
|
181 |
Will compute triangle numbers by calling itself recursively, or parallel subdivison
|
sl@0
|
182 |
@SYMTestExpectedResults Test must not fail
|
sl@0
|
183 |
@SYMREQ REQ0000
|
sl@0
|
184 |
*/
|
sl@0
|
185 |
int wmain (int argc, wchar_t *argv[])
|
sl@0
|
186 |
{
|
sl@0
|
187 |
wchar_t *user=wgetenv((wchar_t*)L"USER");
|
sl@0
|
188 |
char cwd[MAXPATHLEN];
|
sl@0
|
189 |
int ret=3, i;
|
sl@0
|
190 |
char tmpbuf[100];
|
sl@0
|
191 |
|
sl@0
|
192 |
start_redirection_server();
|
sl@0
|
193 |
|
sl@0
|
194 |
if (argc==3 && wcscmp(argv[1],(wchar_t*)L"do_triangle")==0)
|
sl@0
|
195 |
return triangle_recurse(argv[0], argv[2], 0);
|
sl@0
|
196 |
|
sl@0
|
197 |
if (argc==3 && wcscmp(argv[1],(wchar_t*)L"do_trianglep")==0)
|
sl@0
|
198 |
return triangle_parallel(argv[0], argv[2]);
|
sl@0
|
199 |
|
sl@0
|
200 |
if (user)
|
sl@0
|
201 |
{
|
sl@0
|
202 |
char tmp[100];
|
sl@0
|
203 |
wcstombs(tmp,user,100);
|
sl@0
|
204 |
printf("Hello %s\n", tmp);
|
sl@0
|
205 |
ret=4;
|
sl@0
|
206 |
}
|
sl@0
|
207 |
else
|
sl@0
|
208 |
printf("Greetings.\n");
|
sl@0
|
209 |
|
sl@0
|
210 |
printf("I am process %d\n", getpid());
|
sl@0
|
211 |
|
sl@0
|
212 |
if (getcwd(cwd,sizeof(cwd))!=0)
|
sl@0
|
213 |
printf("I am speaking to you from %s\n", cwd);
|
sl@0
|
214 |
|
sl@0
|
215 |
printf("I have %d arguments: ", argc);
|
sl@0
|
216 |
for (i=0; i<argc; i++)
|
sl@0
|
217 |
{
|
sl@0
|
218 |
wcstombs(tmpbuf, argv[i], sizeof(tmpbuf));
|
sl@0
|
219 |
printf(">%s< ", tmpbuf);
|
sl@0
|
220 |
}
|
sl@0
|
221 |
printf("\r\n");
|
sl@0
|
222 |
|
sl@0
|
223 |
printf("In a few moments I shall say %c to stderr:\n", 'a'+argc);
|
sl@0
|
224 |
fflush(stdout);
|
sl@0
|
225 |
|
sl@0
|
226 |
fprintf(stderr, "%c\n", 'a'+argc);
|
sl@0
|
227 |
fflush(stderr);
|
sl@0
|
228 |
|
sl@0
|
229 |
if (argc==3 && wcscmp(argv[1],(wchar_t*)L"triangle")==0)
|
sl@0
|
230 |
{
|
sl@0
|
231 |
printf("For my next trick I shall compute the triangle of %s: ", argv[2]);
|
sl@0
|
232 |
fflush(stdout);
|
sl@0
|
233 |
ret=triangle_recurse(argv[0], argv[2], 1);
|
sl@0
|
234 |
printf("it's %d\n", ret);
|
sl@0
|
235 |
}
|
sl@0
|
236 |
|
sl@0
|
237 |
if (argc==3 && wcscmp(argv[1],(wchar_t*)L"trianglep")==0)
|
sl@0
|
238 |
{
|
sl@0
|
239 |
printf("I shall now compute the triangle of %s using a parallel algorithm: ", argv[2]);
|
sl@0
|
240 |
fflush(stdout);
|
sl@0
|
241 |
wsetenv((wchar_t*)L"TRIANGLE_BASE", (wchar_t*)L"1", 0);
|
sl@0
|
242 |
ret=triangle_parallel(argv[0], argv[2]);
|
sl@0
|
243 |
printf("it's %d\n", ret);
|
sl@0
|
244 |
}
|
sl@0
|
245 |
|
sl@0
|
246 |
printf("Farewell...\nPress a key");
|
sl@0
|
247 |
getchar();
|
sl@0
|
248 |
return ret;
|
sl@0
|
249 |
}
|