glib/glibbackend/src/spawn.cpp
changeset 0 e4d67989cc36
child 72 403e7f6ed6c5
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 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 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <e32std.h>
       
    21 #include <e32base.h>
       
    22 #include <e32cons.h>
       
    23 #include <utf.h>
       
    24 #include <errno.h>
       
    25 #include <wchar.h>
       
    26 #include <stdlib.h>
       
    27 #include <string.h>
       
    28 #include <pdrstore.h>
       
    29 #include "glibbackend.h"
       
    30 
       
    31 #define MAX_COMMAND_LINE_LENGTH 256
       
    32 
       
    33 extern "C"
       
    34 {
       
    35 	
       
    36 EXPORT_C int spawnv(int mode,const char * path,const char **argv)
       
    37 {
       
    38 	TText16 iPath16[KMaxFileName];
       
    39 	char replacePath[KMaxFileName];
       
    40 	
       
    41 	/* replace / with // */
       
    42 	strcpy(replacePath,path);
       
    43 	char *temp = strchr(replacePath,'/');
       
    44 	while (temp)
       
    45 	{
       
    46 		*temp = '\\';
       
    47 		temp = strchr(replacePath,'/');
       
    48 	} // end while
       
    49 	
       
    50 	// convert narrow char to wide char
       
    51 	if(mbstowcs((wchar_t *)iPath16, replacePath, strlen(replacePath)+1) == (size_t)(-1))
       
    52 	{
       
    53 			return -1;
       
    54 	}
       
    55 	
       
    56 	TBuf16<KMaxFileName> iCmdName16;
       
    57 	
       
    58 	iCmdName16.FillZ(KMaxFileName);
       
    59 	
       
    60 	iCmdName16.Copy(iPath16);
       
    61 	
       
    62 	TInt i;
       
    63 	
       
    64 	TInt iVal = 0;
       
    65 	
       
    66 	while(argv[iVal])
       
    67 	{
       
    68 		iVal++;
       
    69 	}	//end while
       
    70 		
       
    71 	
       
    72 	TBuf16<MAX_COMMAND_LINE_LENGTH> iArgv16;
       
    73 	
       
    74 	iArgv16.FillZ(MAX_COMMAND_LINE_LENGTH);
       
    75 	
       
    76 	iArgv16.SetLength(0);
       
    77 	
       
    78 	for(i = 0; i < iVal ; i++)
       
    79 	{
       
    80 		TText16 temp[MAX_COMMAND_LINE_LENGTH];
       
    81 		
       
    82 		if(mbstowcs((wchar_t *)temp, argv[i], strlen(argv[i])+1) == (size_t)(-1))
       
    83 		{
       
    84 				return -1;
       
    85 		}
       
    86 		
       
    87 		TPtrC16 iTemp(temp,strlen(argv[i]));
       
    88 		
       
    89 		iArgv16.Append(iTemp);
       
    90 		
       
    91 		if(i != iVal-1)
       
    92 			iArgv16.Append(L' ');
       
    93 		
       
    94 	} // end for
       
    95 	
       
    96 	RProcess iProcess;
       
    97 	TRequestStatus iStatus;
       
    98 	TInt iRetVal = iProcess.Create(iCmdName16,iArgv16);
       
    99 		
       
   100 	if(iRetVal != KErrNone)
       
   101 	{
       
   102 		switch(iRetVal)
       
   103 		{
       
   104 			case KErrNotFound : 	errno = ENOENT;
       
   105 									break;
       
   106 			case KErrNoMemory : 	errno = ENOMEM ;
       
   107 									break;
       
   108 			case KErrNotSupported : errno = ENOEXEC;
       
   109 									break;
       
   110 			case KErrBadName : 		errno = ENAMETOOLONG;
       
   111 									break;
       
   112 			default:   	 	 		break;
       
   113 		}
       
   114 		
       
   115 		return -1;
       
   116 	}
       
   117 	
       
   118 	if(mode == P_WAIT)
       
   119 	{
       
   120 		iProcess.Logon(iStatus);
       
   121 		iProcess.Resume();
       
   122  		User::WaitForRequest(iStatus);
       
   123  		iProcess.Close();
       
   124  		return iStatus.Int();
       
   125 	}
       
   126 	else
       
   127 	{
       
   128 		iProcess.Resume();	
       
   129 		return iProcess.Id();	
       
   130 	}
       
   131 }
       
   132 
       
   133 EXPORT_C int spawnvp(int mode,const char * path,const char **argv)
       
   134 {
       
   135 	return spawnv(mode,path,argv);
       
   136 }
       
   137 
       
   138 
       
   139 static int ValidateHandle(HANDLE handle)
       
   140 {
       
   141 	RProcess iProcess;
       
   142 	if( iProcess.Open(handle) < 0)
       
   143 		return 0;
       
   144 	
       
   145 	THandleInfo handleinfo;
       
   146 	iProcess.HandleInfo(&handleinfo);
       
   147 	
       
   148 	if(handleinfo.iNumOpenInProcess ==0 && handleinfo.iNumOpenInThread == 0 && 
       
   149 	 handleinfo.iNumProcesses == 0 && handleinfo.iNumThreads == 0)
       
   150 		return 0;
       
   151 	else
       
   152 		return 1;
       
   153 }
       
   154 
       
   155 EXPORT_C BOOL CloseHandle(HANDLE handle)
       
   156 {
       
   157 	RProcess iProcess;
       
   158 	if( iProcess.Open(handle) < 0)
       
   159 		return 0;
       
   160 	
       
   161 	if(ValidateHandle(handle))
       
   162 		iProcess.Close();
       
   163 	
       
   164 	return 1;
       
   165 }
       
   166 
       
   167 EXPORT_C HANDLE GetCurrentProcess()
       
   168 {
       
   169 	return RProcess().Id();
       
   170 }
       
   171 
       
   172 BOOL DuplicateHandle(HANDLE hSourceProcessHandle,HANDLE hSourceHandle,HANDLE hTargetProcessHandle,LPHANDLE lpTargetHandle,DWORD dwDesiredAccess,BOOL bInheritHandle,DWORD dwOptions)
       
   173 {
       
   174 	return 0;
       
   175 }
       
   176 
       
   177 EXPORT_C int spawnve(int mode,const char * path,const char **argv,const char **envp)
       
   178 {
       
   179 	return spawnv(mode,path,argv);
       
   180 }
       
   181 
       
   182 EXPORT_C int spawnvpe(int mode,const char * path,const char **argv,const char **envp)
       
   183 {
       
   184 	return spawnv(mode,path,argv);
       
   185 }
       
   186 
       
   187 
       
   188 } /* close extern "C"*/