.ad 8
.bm 8
.fm 4
.bt $Copyright (c) 1995-2005 SAP AG$$Page %$
.tm 12
.hm 6
.hs 3
.tt 1 $SQL$Project Datensicherung$vox12c$
.tt 2 $$$
.tt 3 $$BACKUP$$1998-03-10$
***********************************************************
.nf

.nf

.nf

    ========== licence begin  GPL
    Copyright (c) 1995-2005 SAP AG

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    ========== licence end
.fo


.fo


.fo
.nf
.sp
Module  :      BACKUP 
=========
.sp
Purpose :
.CM *-END-* purpose -------------------------------------
.sp
.cp 3
Define  :

.CM *-END-* define --------------------------------------
.sp;.cp 3
Use     :

.CM *-END-* use -----------------------------------------
.sp;.cp 3
Synonym :

.CM *-END-* synonym -------------------------------------
.sp;.cp 3
Author  : 
.sp
.cp 3
Created : 
.sp
.cp 3
Version :
.sp
.cp 3
Release :  6.2    Date : 1998-03-10
.br
.sp
***********************************************************
.sp
.cp 10
.fo
.oc _/1
Specification:

.CM *-END-* specification -------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.fo
.oc _/1
Description:

.CM *-END-* description ---------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.nf
.oc _/1
Structure:

.CM *-END-* structure -----------------------------------
.sp 2
**********************************************************
.sp
.cp 10
.nf
.oc _/1
.CM -lll-
Code    :
/* PRETTY */

/*****************************************************************************************/

static char	m_szHostName[LINE_LEN+1] = { 0, };

/*****************************************************************************************/

int vox12_Init ( )
{
	return 0;
}

/*****************************************************************************************/

BOOL vox12_CheckFile ( char *filename )
{
	if ( access(filename,0) == -1 )
		return FALSE;

	return TRUE;

} /* CheckFiles */

/*****************************************************************************************/

BOOL vox12_CheckFiles ( unsigned num, ... )
{
	unsigned	i;
	va_list		files;

	va_start(files,num);

	for ( i = 0; i < num; i++ )
	{
		char	*pszFileName = va_arg(files,char*);
	
		if ( access(pszFileName,0) == -1 )
			return FALSE;
	}

	va_end(files);

	return TRUE;

} /* CheckFiles */

/*****************************************************************************************/

BOOL vox12_CheckDir ( char *dirname )
{
	struct	_stat	s_stat;
	
	if ( _stat(dirname,&s_stat) == -1 )
		return FALSE;

	if ( (s_stat.st_mode & _S_IFDIR) == 0 )
		return FALSE;

	return TRUE;
}

/*****************************************************************************************/

BOOL vox12_CheckDirs ( unsigned num, ... )
{
	unsigned	i;
	va_list		files;

	va_start(files,num);

	for ( i = 0; i < num; i++ )
	{
		char	*pszDirName = va_arg(files,char*);

		if ( vox12_CheckDir(pszDirName) == FALSE )
			return FALSE;
	}

	va_end(files);

	return TRUE;

} /* CheckDirs */

/*****************************************************************************************/

char *vox12_BaseName ( char *filename )
{
	char	*p;
	char	*pStr = (p = strrchr(filename,'\\')) == NULL ? strrchr(filename,'/') : p;

	if ( pStr == NULL )
		return filename;
	else
		return pStr + 1;
}

/*****************************************************************************************/

char *vox12_DirName ( char *filename )
{
int MissingCode;
//REM DirName noch nicht impl.	
	return filename;
}

/*****************************************************************************************/

char *vox12_GetTempDir()
{
	char	*pszTmpDir;

	if ( (pszTmpDir = vox12_GetEnv("TMP")) == NULL )
	{
		if ( (pszTmpDir = vox12_GetEnv("TEMP")) == NULL )
			pszTmpDir = "./";			
	}		

	return pszTmpDir;
}

/*****************************************************************************************/

int vox12_ChangeDir ( char *dir ) 
{
	if ( chdir(dir) == -1 )
	{
		iErrorCode = GetLastError();
		sprintf(szErrorMsg,"chdir(%d) failed, rc=%d",dir,iErrorCode);
		return -1;
	}

	return 0;
}

/*****************************************************************************************/

TPID vox12_ExecCommand ( char *cmd )
{
	STARTUPINFO				StartupInfo;
	PROCESS_INFORMATION		ProcessInfo;
	int						iRC = 0;

	BOOL					bBackGround;
	char					*p;
int MissingCode;

	// Process will run in background ?
//REM bessere Abfrage nach "im Hintergrund laufen"
	if ( (p = strstr(cmd,"& ")) != NULL )
	{
		*p = ' ';
		bBackGround = TRUE;
	}
	else if ( (p = strstr(cmd," &")) != NULL )
	{
		*(p+1) = ' ';
		bBackGround = TRUE;
	}
	else
		bBackGround = FALSE;
			

	memset(&StartupInfo,0,sizeof(STARTUPINFO));
	StartupInfo.cb = sizeof(STARTUPINFO);
	StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
	StartupInfo.wShowWindow = SW_HIDE;

	if ( CreateProcess( NULL,
						cmd,
						NULL,
						NULL,
						TRUE,
						0, 			// CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP,
						NULL,
						NULL,
						&StartupInfo,
						&ProcessInfo ) == FALSE )
	{
		iErrorCode = GetLastError();
		sprintf(szErrorMsg,"CreateProcess(%s) failed, rc=%d",cmd,iErrorCode);
		return TPID_ERROR;
	}

	if ( bBackGround == FALSE )
	{
		if ( WaitForSingleObject(ProcessInfo.hProcess,INFINITE) == WAIT_FAILED )
		{
			iErrorCode = GetLastError();
			sprintf(szErrorMsg,"WaitForSingleObject(%d) failed, rc=%d",
					ProcessInfo.hProcess,iErrorCode);
			return TPID_ERROR;
		}

		if ( GetExitCodeProcess(ProcessInfo.hProcess, &iRC) == FALSE )
		{
			iErrorCode = GetLastError();
			sprintf(szErrorMsg,"GetExitCodeProcess() failed, rc=%d",iErrorCode);
			return TPID_ERROR;
		}
	}

	iErrorCode = iRC;
	strcpy(szErrorMsg,"");
	
	return (TPID)ProcessInfo.hProcess;
}

/*****************************************************************************************/

TPID vox12_VExecCommand ( char *format, ... )
{
	char		szCommandLine[255];
	va_list		args;

	va_start(args,format);
	vsprintf(szCommandLine,format,args);
	va_end(args);

	return vox12_ExecCommand(szCommandLine);
}

/*****************************************************************************************/

int vox12_WaitForProcess ( TPID pid )
{
	int	iRC = 0;

	if ( WaitForSingleObject((HANDLE)pid,INFINITE) == WAIT_FAILED )
	{
		iErrorCode = GetLastError();
		sprintf(szErrorMsg,"WaitForSingleObject(%d) failed, rc=%d",pid,iErrorCode);
		return -1;
	}

	if ( GetExitCodeProcess((HANDLE)pid, &iRC) == FALSE )
	{
		iErrorCode = GetLastError();
		sprintf(szErrorMsg,"GetExitCodeProcess() failed, rc=%d",iErrorCode);
		return -1;
	}

	iErrorCode = iRC;
	return 0;
}

/*****************************************************************************************/

int vox12_KillProcess ( TPID pid )
{
	HANDLE	hProcHandle;

	if ( (hProcHandle = OpenProcess(PROCESS_TERMINATE,FALSE,pid)) == NULL )
	{
		iErrorCode = GetLastError();
		sprintf(szErrorMsg,"OpenProcess() failed, rc=%d",iErrorCode);
		return -1;
	}

	if ( TerminateProcess(hProcHandle,1) == FALSE )
	{
		iErrorCode = GetLastError();
		sprintf(szErrorMsg,"TerminateProcess() failed, rc=%d",iErrorCode);
		return -1;
	}

	return 0;
}

/*****************************************************************************************/

TPID vox12_GetPID ( void )
{
#if defined(WIN32)
	return GetCurrentProcessId();
#elif defined(OS2)
#else
#endif
}

/*****************************************************************************************/

char *vox12_GetHostName ()
{
int MissingCode;
	char	szBuffer[MAX_PATH+1];
	char	szTempFile[MAX_PATH+1];
			
	FILE	*fp = NULL;
	int		PID;

	if ( strlen(m_szHostName) != 0 )
		return m_szHostName;

	if ( tmpnam(szTempFile) == NULL )
	{
		iErrorCode = GetLastError();
		sprintf(szErrorMsg,"tmpnam() failed, rc=%d",iErrorCode);
		return NULL;
	}
		
// Was mach' ich ohne TCP/IP (d.h. ohne hostname)
	sprintf(szBuffer,"cmd /c hostname > %s",szTempFile);

	if ( (PID = vox12_ExecCommand(szBuffer)) == -1 )
	{
		iErrorCode = vox11_GetErrorCode();
		sprintf(szErrorMsg,"ExecCommand(%s) failed, rc=%d",szBuffer,iErrorCode);
		return NULL;
	}

	if ( vox11_GetErrorCode() != 0 )
	{
		iErrorCode = vox11_GetErrorCode();
		sprintf(szErrorMsg,"Program 'hostname' returned %d",iErrorCode);
		return NULL;
	}

	if ( (fp = fopen(szTempFile,"r")) == NULL )
	{
		iErrorCode = GetLastError();
		sprintf(szErrorMsg,"fopen(%s) failed, rc=%d",szTempFile,iErrorCode);
		remove(szTempFile);
		return NULL;
	}
	
	// Scan first line
	if ( fscanf(fp,"%s",szBuffer) == 0 )
	{
		iErrorCode = 2;
		sprintf(szErrorMsg,"fscanf() failed");
		remove(szTempFile);
		return NULL;
	}
	
	fclose(fp);
	remove(szTempFile);

	strcpy(m_szHostName,szBuffer);

	return m_szHostName;
}

/*****************************************************************************************/

char *vox12_GetOSName()
{
#ifdef WIN32
	OSVERSIONINFO	os;

	os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	GetVersionEx(&os);

	switch ( os.dwPlatformId )
	{
		case VER_PLATFORM_WIN32s:			return "Windows31";
//		case VER_PLATFORM_WIN32_WINDOWS:	return "Windows95";
		case VER_PLATFORM_WIN32_NT:			return "WindowsNT";
		default:							return "???";
	}

#else

#endif
}

/*****************************************************************************************/

char *vox12_GetEnv ( char *var )
{
#if defined(WIN32)
/*REM Umgebung aus der Registry
	static char 	szBuffer[LINE_LEN+1] = { 0, };
	HANDLE 			hEnvironmentKey;
    DWORD       	Type;
	unsigned long	Size = LINE_LEN;
	
    if ( RegOpenKey(HKEY_LOCAL_MACHINE,
				       "SYSTEM\\"
				       "CurrentControlSet\\"
				       "Control\\"
				       "Session Manager\\"
				       "Environment",
				       &hEnvironmentKey) != 0 )
	{
		sprintf(szErrorMsg,"RegOpenKey() failed");
		iErrorCode = GetLastError();
		return NULL;
	}

    if ( RegQueryValueEx(hEnvironmentKey, var, NULL, &Type, (UCHAR*)szBuffer, &Size) != 0 )
    {
		sprintf(szErrorMsg,"RegQueryValueEx() failed");
		iErrorCode = GetLastError();
		RegCloseKey(hEnvironmentKey);
    	return NULL;
	}

    if (Size >= LINE_LEN) 
	{
		sprintf(szErrorMsg,"Environmentbuffer too short");
		iErrorCode = ERROR_NOT_ENOUGH_MEMORY;
		RegCloseKey(hEnvironmentKey);
    	return NULL;
	}

	RegCloseKey(hEnvironmentKey);
	sprintf(szErrorMsg,"No error");
	iErrorCode = ERROR_SUCCESS;
	return szBuffer;
*/

	return getenv(var);

#elif defined(OS2)
#else
#endif
}

/*****************************************************************************************/

int vox12_RemoveFile ( char *filename )
{
	int	iRC = 0;

	if ( remove(filename)  != 0 )
		return iRC;

	return 0;
}

/*****************************************************************************************/
/*
 =============================== END ========================================
*/

.CM *-END-* code ----------------------------------------
.SP 2
***********************************************************
*-PRETTY-*  statements    :         38
*-PRETTY-*  lines of code :         38        PRETTY  1.07
*-PRETTY-*  lines in file :        141         1989-01-20
.PA
