Update contrib.
2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
16 * A variant of "Hello World" which reports various bits of its environment
17 * and returns an interesting exit status
33 int triangle_recurse(wchar_t* prog, wchar_t* num, int progress)
35 printf("\nBut not under WINS where I'll be told 0\n");
39 int triangle_parallel(wchar_t* prog, wchar_t* num)
41 printf("\nBut not under WINS where I'll be told 0\n");
47 int triangle_recurse(wchar_t* prog, wchar_t* num, int progress)
57 wcstombs(tmpbuf, num, 50);
62 wcstombs(tmpbuf, prog, 50);
63 sprintf(cmd, "%s do_triangle %d", tmpbuf, n-1);
64 mbstowcs(wcmd, cmd, 100);
67 pid=wpopen3(wcmd, (wchar_t*)L"", 0, fids);
70 fprintf(stderr, "Executing %s, ", cmd);
71 perror("wpopen3 failed");
76 int n=waitpid(pid, &ret, WNOHANG);
89 int triangle_parallel(wchar_t* prog, wchar_t* num)
92 int pid, pid1, pid2, ret1, ret2, base, split;
95 int fids1[3], fids2[3];
99 wcstombs(tmpbuf, num, 50);
103 basep=getenv("TRIANGLE_BASE");
108 /* we have to add up the numbers base..n inclusive
115 /* At least 3 numbers, so split it into subtasks for child processes
119 wcstombs(tmpbuf, prog, 100);
120 sprintf(cmd, "%s do_trianglep %d", tmpbuf, base+split);
121 mbstowcs(wcmd, cmd, 100);
122 pid1=wpopen3(wcmd, (wchar_t*)L"", 0, fids1);
125 fprintf(stderr, "Doing %d..%d of %d..%d", base+split, base, n, base);
126 perror("popen3 failed");
130 sprintf(cmd, "%d", base+split+1);
131 mbstowcs(wcmd, cmd, 100);
132 wsetenv((wchar_t*)L"TRIANGLE_BASE", wcmd, 1);
134 sprintf(cmd, "%s do_trianglep %d", prog, n);
135 mbstowcs(wcmd, cmd, 100);
136 pid2=wpopen3(wcmd, (wchar_t*)L"", 0, fids2);
139 fprintf(stderr, "Doing %d..%d of %d..%d", n, base+split+1, n, base);
140 perror("wpopen3 failed");
144 /* Now collect the results */
153 perror("waitpid failed");
167 printf("Unexpected pid %d (not %d or %d)\n", pid, pid1, pid2);
175 Usage: THELLOU [triangle[p] <n>]
177 @SYMTestCaseID SYSLIB-STDLIB-CT-1096
178 @SYMTestCaseDesc Tests for printing to standard output stream
179 @SYMTestPriority High
180 @SYMTestActions Prints some stuff (looks at args, getenv("USER"), cwd) to stdout and stderr.
181 Will compute triangle numbers by calling itself recursively, or parallel subdivison
182 @SYMTestExpectedResults Test must not fail
185 int wmain (int argc, wchar_t *argv[])
187 wchar_t *user=wgetenv((wchar_t*)L"USER");
188 char cwd[MAXPATHLEN];
192 start_redirection_server();
194 if (argc==3 && wcscmp(argv[1],(wchar_t*)L"do_triangle")==0)
195 return triangle_recurse(argv[0], argv[2], 0);
197 if (argc==3 && wcscmp(argv[1],(wchar_t*)L"do_trianglep")==0)
198 return triangle_parallel(argv[0], argv[2]);
203 wcstombs(tmp,user,100);
204 printf("Hello %s\n", tmp);
208 printf("Greetings.\n");
210 printf("I am process %d\n", getpid());
212 if (getcwd(cwd,sizeof(cwd))!=0)
213 printf("I am speaking to you from %s\n", cwd);
215 printf("I have %d arguments: ", argc);
216 for (i=0; i<argc; i++)
218 wcstombs(tmpbuf, argv[i], sizeof(tmpbuf));
219 printf(">%s< ", tmpbuf);
223 printf("In a few moments I shall say %c to stderr:\n", 'a'+argc);
226 fprintf(stderr, "%c\n", 'a'+argc);
229 if (argc==3 && wcscmp(argv[1],(wchar_t*)L"triangle")==0)
231 printf("For my next trick I shall compute the triangle of %s: ", argv[2]);
233 ret=triangle_recurse(argv[0], argv[2], 1);
234 printf("it's %d\n", ret);
237 if (argc==3 && wcscmp(argv[1],(wchar_t*)L"trianglep")==0)
239 printf("I shall now compute the triangle of %s using a parallel algorithm: ", argv[2]);
241 wsetenv((wchar_t*)L"TRIANGLE_BASE", (wchar_t*)L"1", 0);
242 ret=triangle_parallel(argv[0], argv[2]);
243 printf("it's %d\n", ret);
246 printf("Farewell...\nPress a key");