⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 my_api.c

📁 unix 下用pro*c tuxedo 开发的东西
💻 C
字号:
/*
*	File name		: my_api.c
*	Purpose			: Some function called by Pcs_Startup.c
*	Author			: Mr. Zhujingkun
*	Date Created	: 2000,04,01
*	Version			: Version 1.0
*	Environment		: SCO UNIX 5.0.5
*	Portability		: UNIX Platform
*	Called Functions: Pcs_Startup.c
*/


#include "stdio.h"
#include "stdlib.h"
#include "my_api.h"
#include "Pcs_Startup.h"
#include "unistd.h"


//Check wether a file is exist
int MyExistFile( const char* sFile )
{
	FILE *hFileToOpen;
	hFileToOpen = fopen( sFile, "r" );
	if( hFileToOpen != (FILE*)NULL )
	{
		fclose( hFileToOpen );
		return(1);
	}
	else
		return(0);
}


//
//Read a line to a string from a file, \n is excluded.
//return 1 if sucessful, 0 otherwise
//Do not close the file.
//
int myapi_ReadALine( char* cLine, FILE* hTable )
{
	char cChar;
	int i=0;
	cLine[0] = '\0';
	while( (cChar = fgetc( hTable )) != '\n' )
	{
		if( cChar == EOF ) return(0);
		cLine[i++] = cChar;
		
		//truncated if superlong
		if( i >= MAX_LINE_LEN ) i=0;
		cLine[i] = '\0';
	}
	return(1);
}


//
//Get the Time_Interval specified in the file and return it
//
int GetTimeInterval(char* cType)
{
	FILE *hTimeInterval;
	char cLine[MAX_LINE_LEN+1], cCaption[100], cValue[21], cDescription[200];
	int iInterval, i;
	char* pLine;
	
	 
	hTimeInterval = fopen( CONFIG_FILE_FOR_TIMEINTERVAL, "r" );
	if( hTimeInterval == NULL )
	{
		sprintf( cDescription, "Pcs_Depart_Src_File: Cannot open file: %s,The file may not exist!", CONFIG_FILE_FOR_TIMEINTERVAL);
		ErrorLog( cDescription );
		return(DEFAULT_TIMEINTERVAL);  
	}
	
	while( myapi_ReadALine( cLine, hTimeInterval ) )
	{
		if( cLine[0] == '#' ) continue;
		if( cLine[0] == NULL ) continue;
		
		pLine = cLine;
		while( (*pLine == ' ') || (*pLine == '\t') ) pLine++;
		i=0;
		while( (*pLine != ' ') && (*pLine != '\t') && (*pLine != '\0') )
		{
			cCaption[i] = *pLine;
			i++;
			pLine++;
		}
		
		cCaption[i] = '\0';
		if( strcmp( cCaption, cType ) == 0 ) /*Match*/
		{
			while( (*pLine == ' ') || (*pLine == '\t') ) pLine++;
			if( *pLine == '\0' )
				return(DEFAULT_TIMEINTERVAL);
			i=0;
			while( (*pLine != ' ') && (*pLine != '\t') && (*pLine != '\0') )
			{
				cValue[i] = *pLine;
				i++;
				pLine++;
			}
			cValue[i] = '\0';
			iInterval = atoi(cValue);
			
			if(iInterval <= 0) 		
			{
				return(DEFAULT_TIMEINTERVAL);
			}
			else
				return(iInterval);
		}
		else
			continue;
	}
	fclose( hTimeInterval );
	return(DEFAULT_TIMEINTERVAL);
}
//
//Record the error message into errorlog file
//Need to truncate or delete as the size grow
//
void ErrorLog( const char* sErrorMessage )
{
	FILE* hErrorFile;
	char cDateTime[100];
	
	hErrorFile = fopen( ERROR_LOG_FILE, "a" );
	if( hErrorFile == NULL )
	{
		printf( "Critical! Cannot open ERROR_LOG_FILE for append or cannot create!\n");
		return;
	}
	fseek( hErrorFile, 0, SEEK_END );
	fputs( sErrorMessage, hErrorFile );
	fclose( hErrorFile );
	sprintf( cDateTime, "date >> %s\n", ERROR_LOG_FILE );
	system( cDateTime );
}


//
//To determine wether the string is a filename or file content
//Return 1 if the string contain no ','
//Return 0 otherwise
//
int myapi_IsFileName( char *cALine )
{
	char *pLine;
	pLine = cALine;
	while( *pLine != '\0' )
	{
		if( *pLine == ',' ) return (0);
		pLine++;
	}
	return(1);
}


//
//To generic a unique file name based on the file name given.
//First, add suffix "0",then add the suffix by number "1", until there
//is no such file. If 9999 is reached by suffix, "0" is assigned.
//
void myapi_FormUniqueName( char* cFileName )
{
	int i=0;
	FILE* hFileName;
	char cFileContent[100], *pTempFile, *pFileContent;
	
	strcpy( cFileContent, cFileName );
	
	while( MyExistFile( cFileName ) )
	{
		sprintf( cFileName, "%s%i",cFileContent, i++ );
		if( i > 9999 )
		{
			i=0;
			sprintf( cFileName, "%s0", cFileContent );
			return;
		}
	}
}


//
//Remove the pathname from a full name
//eg: /usr/bkfx/bin/Cc_Startup.x -> CcStartup.x
//
void GetArgv0( char* cArgv0, char* cPathName )
{
	char* pPath;
	pPath = cPathName;
	while( *pPath != '\0' )
	{
		strcpy( cArgv0, pPath );
		while( (*pPath != '/') && (*pPath != '\0') )
		{
			pPath++;
		}
		if( *pPath == '/' ) pPath++;
	}
}

/*
//Record the specified number in a file, one number per line
//The number is the PID of a process.
*/
void ProcIDLog(int iRet)
{
	FILE *hPIDList;
	char cPID[100];

	sprintf( cPID, "%i\n", iRet );
	hPIDList = fopen( STARTUP_PROCESS_ID_LIST, "a" );
	if( hPIDList == NULL )
	{
		ErrorLog( "Error! Cannot open STARTUP_PROCESS_ID_LIST for append or cannot create!\n");
		return;
	}
	fseek( hPIDList, 0, SEEK_END );
	fputs( cPID, hPIDList );
	fclose( hPIDList );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -