os/ossrv/genericopenlibs/cstdlib/TSTLIB/TWHELLOU.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/TWHELLOU.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,249 @@
     1.4 +/*
     1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +* THELLOU.C
    1.19 +* A variant of "Hello World" which reports various bits of its environment
    1.20 +* and returns an interesting exit status
    1.21 +* 
    1.22 +*
    1.23 +*/
    1.24 +
    1.25 +
    1.26 +
    1.27 +#include <stdio.h>
    1.28 +#include <stdlib.h>
    1.29 +#include <unistd.h>
    1.30 +#include <string.h>
    1.31 +#include <sys/wait.h>
    1.32 +#include "CTEST.H"
    1.33 +
    1.34 +
    1.35 +#ifdef __WINS__
    1.36 +int triangle_recurse(wchar_t* prog, wchar_t* num, int progress)
    1.37 +	{
    1.38 +	printf("\nBut not under WINS where I'll be told 0\n");
    1.39 +	return 0;
    1.40 +	}
    1.41 +
    1.42 +int triangle_parallel(wchar_t* prog, wchar_t* num)
    1.43 +	{
    1.44 +	printf("\nBut not under WINS where I'll be told 0\n");
    1.45 +	return 0;
    1.46 +	}
    1.47 +
    1.48 +#else
    1.49 +
    1.50 +int triangle_recurse(wchar_t* prog, wchar_t* num, int progress)
    1.51 +	{
    1.52 +	int n;
    1.53 +	int pid, ret;
    1.54 +	char cmd[100];
    1.55 +	wchar_t wcmd[100];
    1.56 +	int fids[3];
    1.57 +	char tmpbuf[100];
    1.58 +
    1.59 +
    1.60 +	wcstombs(tmpbuf, num, 50);
    1.61 +	n = atol(tmpbuf);
    1.62 +	if (n<1)
    1.63 +		return 0;
    1.64 +
    1.65 +	wcstombs(tmpbuf, prog, 50);
    1.66 +	sprintf(cmd, "%s do_triangle %d", tmpbuf, n-1);
    1.67 +	mbstowcs(wcmd, cmd, 100);
    1.68 +	if (progress)
    1.69 +		{
    1.70 +		pid=wpopen3(wcmd, (wchar_t*)L"", 0, fids);
    1.71 +		if (pid<0)
    1.72 +			{
    1.73 +			fprintf(stderr, "Executing %s, ", cmd);
    1.74 +			perror("wpopen3 failed");
    1.75 +			return -1999999;
    1.76 +			}
    1.77 +		while (1)
    1.78 +			{
    1.79 +			int n=waitpid(pid, &ret, WNOHANG);
    1.80 +			if (n==pid)
    1.81 +				break;
    1.82 +			printf(".");
    1.83 +			fflush(stdout);
    1.84 +			sleep(1);
    1.85 +			}
    1.86 +		}
    1.87 +	else
    1.88 +		ret=wsystem(wcmd);
    1.89 +	return n+ret;
    1.90 +	}
    1.91 +
    1.92 +int triangle_parallel(wchar_t* prog, wchar_t* num)
    1.93 +	{
    1.94 +	int n;
    1.95 +	int pid, pid1, pid2, ret1, ret2, base, split;
    1.96 +	char cmd[100];
    1.97 +	wchar_t wcmd[100];
    1.98 +	int fids1[3], fids2[3];
    1.99 +	char* basep;
   1.100 +	char tmpbuf[100];
   1.101 +
   1.102 +	wcstombs(tmpbuf, num, 50);
   1.103 +	n = atol(tmpbuf);
   1.104 +	if (n<1)
   1.105 +		return 0;
   1.106 +	basep=getenv("TRIANGLE_BASE");
   1.107 +	if (basep==0)
   1.108 +		return 0;
   1.109 +	base=atol(basep);
   1.110 +
   1.111 +	/* we have to add up the numbers base..n inclusive
   1.112 +	 */
   1.113 +	if (base==n)
   1.114 +		return n;
   1.115 +	if (base+1==n)
   1.116 +		return base+n;
   1.117 +
   1.118 +	/* At least 3 numbers, so split it into subtasks for child processes
   1.119 +	 */
   1.120 +	split = (n-base)/2;
   1.121 +
   1.122 +	wcstombs(tmpbuf, prog, 100);
   1.123 +	sprintf(cmd, "%s do_trianglep %d", tmpbuf, base+split);
   1.124 +	mbstowcs(wcmd, cmd, 100);
   1.125 +	pid1=wpopen3(wcmd, (wchar_t*)L"", 0, fids1);
   1.126 +	if (pid1<0)
   1.127 +		{
   1.128 +		fprintf(stderr, "Doing %d..%d of %d..%d", base+split, base, n, base);
   1.129 +		perror("popen3 failed");
   1.130 +		return -1999999;
   1.131 +		}
   1.132 +	
   1.133 +	sprintf(cmd, "%d", base+split+1);
   1.134 +	mbstowcs(wcmd, cmd, 100);
   1.135 +	wsetenv((wchar_t*)L"TRIANGLE_BASE", wcmd, 1);
   1.136 +
   1.137 +	sprintf(cmd, "%s do_trianglep %d", prog, n);
   1.138 +	mbstowcs(wcmd, cmd, 100);
   1.139 +	pid2=wpopen3(wcmd, (wchar_t*)L"", 0, fids2);
   1.140 +	if (pid2<0)
   1.141 +		{
   1.142 +		fprintf(stderr, "Doing %d..%d of %d..%d", n, base+split+1, n, base);
   1.143 +		perror("wpopen3 failed");
   1.144 +		return -1999999;
   1.145 +		}
   1.146 +	
   1.147 +	/* Now collect the results */
   1.148 +	ret1=-10000;
   1.149 +	ret2=-10000;
   1.150 +	for (n=0; n<2; n++)
   1.151 +		{
   1.152 +		int ret=-3999999;
   1.153 +		pid=wait(&ret);
   1.154 +		if (pid<0)
   1.155 +			{
   1.156 +			perror("waitpid failed");
   1.157 +			return -2999999;
   1.158 +			}
   1.159 +		if (pid==pid1)
   1.160 +			{
   1.161 +			pid1=-1;
   1.162 +			ret1=ret;
   1.163 +			}
   1.164 +		else if (pid==pid2)
   1.165 +			{
   1.166 +			pid2=-1;
   1.167 +			ret2=ret;
   1.168 +			}
   1.169 +		else
   1.170 +			printf("Unexpected pid %d (not %d or %d)\n", pid, pid1, pid2);
   1.171 +		}
   1.172 +	return ret1+ret2;
   1.173 +	}
   1.174 +
   1.175 +#endif
   1.176 +
   1.177 +/**
   1.178 +Usage:  THELLOU [triangle[p] <n>]
   1.179 +
   1.180 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1096
   1.181 +@SYMTestCaseDesc	    Tests for printing to standard output stream
   1.182 +@SYMTestPriority 	    High
   1.183 +@SYMTestActions  	    Prints some stuff (looks at args, getenv("USER"), cwd) to stdout and stderr.
   1.184 +						Will compute triangle numbers by calling itself recursively, or parallel subdivison
   1.185 +@SYMTestExpectedResults Test must not fail
   1.186 +@SYMREQ                 REQ0000
   1.187 +*/		
   1.188 +int wmain (int argc, wchar_t *argv[])
   1.189 +	{
   1.190 +	wchar_t *user=wgetenv((wchar_t*)L"USER");
   1.191 +	char cwd[MAXPATHLEN];
   1.192 +	int ret=3, i;
   1.193 +	char tmpbuf[100];
   1.194 +
   1.195 +	start_redirection_server();
   1.196 +	
   1.197 +	if (argc==3 && wcscmp(argv[1],(wchar_t*)L"do_triangle")==0)
   1.198 +		return triangle_recurse(argv[0], argv[2], 0);
   1.199 +
   1.200 +	if (argc==3 && wcscmp(argv[1],(wchar_t*)L"do_trianglep")==0)
   1.201 +		return triangle_parallel(argv[0], argv[2]);
   1.202 +
   1.203 +	if (user)
   1.204 +		{
   1.205 +		char tmp[100];
   1.206 +		wcstombs(tmp,user,100);
   1.207 +		printf("Hello %s\n", tmp);
   1.208 +		ret=4;
   1.209 +		}
   1.210 +	else
   1.211 +		printf("Greetings.\n");
   1.212 +
   1.213 +	printf("I am process %d\n", getpid());
   1.214 +
   1.215 +	if (getcwd(cwd,sizeof(cwd))!=0)
   1.216 +		printf("I am speaking to you from %s\n", cwd);
   1.217 +
   1.218 +	printf("I have %d arguments: ", argc);
   1.219 +	for (i=0; i<argc; i++)
   1.220 +		{
   1.221 +		wcstombs(tmpbuf, argv[i], sizeof(tmpbuf));
   1.222 +		printf(">%s< ", tmpbuf);
   1.223 +		}
   1.224 +	printf("\r\n");
   1.225 +
   1.226 +	printf("In a few moments I shall say %c to stderr:\n", 'a'+argc);
   1.227 +	fflush(stdout);
   1.228 +
   1.229 +	fprintf(stderr, "%c\n", 'a'+argc);
   1.230 +	fflush(stderr);
   1.231 +
   1.232 +	if (argc==3 && wcscmp(argv[1],(wchar_t*)L"triangle")==0)
   1.233 +		{
   1.234 +		printf("For my next trick I shall compute the triangle of %s: ", argv[2]);
   1.235 +		fflush(stdout);
   1.236 +		ret=triangle_recurse(argv[0], argv[2], 1);
   1.237 +		printf("it's %d\n", ret);
   1.238 +		}
   1.239 +
   1.240 +	if (argc==3 && wcscmp(argv[1],(wchar_t*)L"trianglep")==0)
   1.241 +		{
   1.242 +		printf("I shall now compute the triangle of %s using a parallel algorithm: ", argv[2]);
   1.243 +		fflush(stdout);
   1.244 +		wsetenv((wchar_t*)L"TRIANGLE_BASE", (wchar_t*)L"1", 0);
   1.245 +		ret=triangle_parallel(argv[0], argv[2]);
   1.246 +		printf("it's %d\n", ret);
   1.247 +		}
   1.248 +
   1.249 +	printf("Farewell...\nPress a key");
   1.250 +	getchar();
   1.251 +	return ret;
   1.252 +	}