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