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.
15 * A variant of "Hello World" which reports various bits of its environment
16 * and returns an interesting exit status
31 int triangle_recurse(char* prog, char* num, int progress)
33 printf("\nBut not under WINS where I'll be told 0\n");
37 int triangle_parallel(char* prog, char* num)
39 printf("\nBut not under WINS where I'll be told 0\n");
45 int triangle_recurse(char* prog, char* num, int progress)
56 sprintf(cmd, "%s do_triangle %d", prog, n-1);
59 pid=popen3(cmd, "", 0, fids);
62 fprintf(stderr, "Executing %s, ", cmd);
63 perror("popen3 failed");
68 int n=waitpid(pid, &ret, WNOHANG);
81 int triangle_parallel(char* prog, char* num)
84 int pid, pid1, pid2, ret1, ret2, base, split;
86 int fids1[3], fids2[3];
92 basep=getenv("TRIANGLE_BASE");
97 /* we have to add up the numbers base..n inclusive
104 /* At least 3 numbers, so split it into subtasks for child processes
108 sprintf(cmd, "%s do_trianglep %d", prog, base+split);
109 pid1=popen3(cmd, "", 0, fids1);
112 fprintf(stderr, "Doing %d..%d of %d..%d", base+split, base, n, base);
113 perror("popen3 failed");
117 sprintf(cmd, "%d", base+split+1);
118 setenv("TRIANGLE_BASE", cmd, 1);
120 sprintf(cmd, "%s do_trianglep %d", prog, n);
121 pid2=popen3(cmd, "", 0, fids2);
124 fprintf(stderr, "Doing %d..%d of %d..%d", n, base+split+1, n, base);
125 perror("popen3 failed");
129 /* Now collect the results */
138 perror("waitpid failed");
152 printf("Unexpected pid %d (not %d or %d)\n", pid, pid1, pid2);
160 Usage: THELLOU [triangle[p] <n>]
162 @SYMTestCaseID SYSLIB-STDLIB-CT-1058
163 @SYMTestCaseDesc Tests for printing to standard output stream
164 @SYMTestPriority High
165 @SYMTestActions Prints some stuff (looks at args, getenv("USER"), cwd) to stdout and stderr.
166 Will compute triangle numbers by calling itself recursively, or parallel subdivison
167 @SYMTestExpectedResults Test must not fail
170 int main (int argc, char *argv[])
172 char *user=getenv("USER");
173 char cwd[MAXPATHLEN];
176 start_redirection_server();
178 if (argc==3 && strcmp(argv[1],"do_triangle")==0)
179 return triangle_recurse(argv[0], argv[2], 0);
181 if (argc==3 && strcmp(argv[1],"do_trianglep")==0)
182 return triangle_parallel(argv[0], argv[2]);
186 printf("Hello %s\n", user);
190 printf("Greetings.\n");
192 printf("I am process %d\n", getpid());
194 if (getcwd(cwd,sizeof(cwd))!=0)
195 printf("I am speaking to you from %s\n", cwd);
197 printf("I have %d arguments: ", argc);
198 for (i=0; i<argc; i++)
199 printf(">%s< ", argv[i]);
202 printf("In a few moments I shall say %c to stderr:\n", 'a'+argc);
205 fprintf(stderr, "%c\n", 'a'+argc);
208 if (argc==3 && strcmp(argv[1],"triangle")==0)
210 printf("For my next trick I shall compute the triangle of %s: ", argv[2]);
212 ret=triangle_recurse(argv[0], argv[2], 1);
213 printf("it's %d\n", ret);
216 if (argc==3 && strcmp(argv[1],"trianglep")==0)
218 printf("I shall now compute the triangle of %s using a parallel algorithm: ", argv[2]);
220 setenv("TRIANGLE_BASE", "1", 0);
221 ret=triangle_parallel(argv[0], argv[2]);
222 printf("it's %d\n", ret);
225 printf("Farewell...\nPress a key");