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

📄 myos.c

📁 可以在linux下使用的ddns的测试程式
💻 C
字号:
#include "ConstDef.h"
//#include "Common.h"
#include "MyOs.h"
#include <semaphore.h>
//****** Semaphore ******// 

void MySemInit(MY_SEM *pSem, Uint32 InitValue)
{
#ifdef _WIN
    pSem->h=CreateSemaphore(NULL, InitValue, 0x0FFFFFFF, NULL);
    assert(pSem->h!=NULL);
#elif defined( _LINUX)
	sem_init((sem_t *)&pSem->sem, (int)0, (unsigned int)InitValue);
	//a(0, (int)0, (unsigned int)0);
#endif
}

void MySemDestroy(MY_SEM *pSem)
{
#ifdef _WIN
    CloseHandle(pSem->h);
#elif defined( _LINUX)
sem_destroy(&pSem->sem);
#endif
}

int MySemWait(MY_SEM *pSem)
{
#ifdef _WIN
    if(WaitForSingleObject(pSem->h, INFINITE)!=WAIT_OBJECT_0)
    {
        return -1;
    }
#elif defined( _LINUX)
    sem_wait(&pSem->sem);
#endif
    return 0;
}        

void MySemPost(MY_SEM *pSem)
{
#ifdef _WIN
    ReleaseSemaphore(pSem->h,1,NULL);
#elif defined( _LINUX)
    sem_post(&pSem->sem);
#endif
}    
//*******  end *******//

void MyCloseSocket(MY_SOCKET *pSocketFd)
{
	if(*pSocketFd!=INVALID_SOCK){
	    MY_SOCKET TmpFd=*pSocketFd;
    	*pSocketFd=INVALID_SOCK;
	    close(TmpFd);
	}
}

int MyThreadCreate(MY_THREAD *pThread, MY_THREAD_FUNC_TYPE start_routine, void *para)
{
#ifdef _WIN
    if((*pThread=CreateThread(NULL, 0, start_routine, para, 0, 0))==NULL)
        return -1;
//    if((*pThread=CreateThread(NULL, 0, start_routine, para, CREATE_SUSPENDED, 0))==NULL)
//        return -1;
//    if(!SetThreadPriority(*pThread, THREAD_PRIORITY_ABOVE_NORMAL)) //for test
//        return -1;
//    if(ResumeThread(*pThread)==0xFFFFFFFF)  //for test
//        return -1;
#elif defined( _LINUX)
    if(pthread_create(pThread, NULL, start_routine, para)!=0)
	{	printf("creat pthread error\n");
                return -1;
    	}
#endif
  printf("creat pthread  is ok\n");
    return 0;
}            

int InitNetwork(void)
{
#ifdef _WIN
    WSADATA wsa_data;
	if(WSAStartup(MAKEWORD( 2, 2 ),&wsa_data)!=0)
    {
        return -1;
    }
    return 0;
#elif defined( _LINUX)
    return 0;
#endif
}

void UnInitNetwork(void)
{
#ifdef _WIN
	WSACleanup();
#elif defined( _LINUX)
#endif
}

void MySleep(Uint32 i)
{
#ifdef _WIN
	Sleep(i);
#elif defined( _LINUX)
        sleep(i);
#endif
}    

void MyCriticalInit(MY_CRITICAL *pCritical)
{
#ifdef _WIN
    InitializeCriticalSection(pCritical);
#elif defined( _LINUX)
    pthread_mutex_init(pCritical, NULL);
#endif
}    

void MyCriticalEntry(MY_CRITICAL *pCritical)
{
#ifdef _WIN
    EnterCriticalSection(pCritical);
#elif defined( _LINUX)
    pthread_mutex_lock(pCritical);
#endif
}        

void MyCriticalLeave(MY_CRITICAL *pCritical)
{
#ifdef _WIN
    LeaveCriticalSection(pCritical);
#elif defined( _LINUX)
    pthread_mutex_unlock(pCritical);
#endif
}
 
void MyCriticalDestroy(MY_CRITICAL *pCritical)
{
#ifdef _WIN
    DeleteCriticalSection(pCritical);
#elif defined( _LINUX)
    
#endif
}        

Uint32 MyGetSysTick(void)
{
#ifdef _WIN
    return GetTickCount();
#elif defined( _LINUX)
#endif
}

/*
void MyClose(void **fd)
{
    int tmp=(int)(*fd);
    fd=NULL;
    close(tmp);
} */

int MyIsDirExists(const char *Dir)
{
#ifdef _WIN
    int Code;
    Code=GetFileAttributes(Dir);
    return (Code!=0xFFFFFFFF&&(Code&FILE_ATTRIBUTE_DIRECTORY)!=0);
#elif defined( _LINUX)
    return 1;//edit 07/08/10
#endif
} 

void MyExtractFilePath(Uint8 *Dest, Uint8 *FileName)
{
    char *pDirSep;
    pDirSep=strrchr((char*)FileName, (int)DIR_SEPARATOR[0]);
    if(pDirSep==NULL)
       // Dest[0]=NULL;修改于20070810
       Dest = NULL;
    else
    {
        strncpy(Dest, FileName,((unsigned char*)pDirSep-FileName+1));
        Dest[((unsigned char*)pDirSep-FileName+1)]=0;
    }
}


void MyExtractFileName( Uint8 *Dest, Uint8 *FileName, int bWithExt )
{
    char *pDot, *pDirSep;
    pDirSep = strrchr( ( char* )FileName, DIR_SEPARATOR[ 0 ] );

    if ( pDirSep == NULL )
    {
        Dest[ 0 ] = 0;
    }
    else
    {
        strcpy( Dest, &pDirSep[ 1 ] );
        if ( !bWithExt )
        {
            pDot = strrchr( ( char* )Dest, '.' );
            if ( pDot != NULL )
            {
                *pDot = 0;
            }
        }
    }
}


void MyExtractFileExt(Uint8 *Dest, Uint8 *FileName)
{
    char *pDotSep;    
    pDotSep=strrchr((char*)FileName, (int)'.');
    if(pDotSep==NULL)
       // Dest[0]=NULL;
       Dest=NULL;//修改于20070810
       else
    {
        strcpy(Dest, pDotSep);
    }
}

#if 0
int MyFileExists(char *filepathName)
{
    struct ffblk ffblk;
    int ret=0;
    if(findfirst(filepathName, &ffblk, 0)==0)
    {
        ret=1;
        findclose(&ffblk);
    }
    return ret;
}

Uint32 MyGetFileSize(Uint8 *filepathname)
{   
    struct ffblk ffblk;
    Uint32 len=0;
    if(findfirst(filepathname, &ffblk, 0)==0)
    {
        len=ffblk.ff_fsize;
        findclose(&ffblk);
    }
    return len;
}

int MyGetFileDateTime(Uint8 *filepathname, Uint16 *pdate, Uint16 *ptime)
{
    struct ffblk ffblk;
    if(findfirst(filepathname, &ffblk, 0)!=0)
        return -1;
    else
    {
        *pdate=ffblk.ff_fdate;
        *ptime=ffblk.ff_ftime;
    }
    return 0;
}


#endif

⌨️ 快捷键说明

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