📄 common.c
字号:
#include "common.h"
int SetNonblock(int fd)
{
int flags;
if((flags=fcntl(fd,F_GETFL,0))==-1) {
CommonShow("SetNonBlock Get error!");
return(-abs(errno));
}
flags|=O_NONBLOCK;
if(fcntl(fd,F_SETFL,flags)==-1) {
CommonShow("SetNonBlock Set error!");
return(-abs(errno));
}
return(0);
}
/*******************************************************************************/
int SetBlock(int fd)
{
int flags;
if((flags=fcntl(fd,F_GETFL,0))==-1) {
CommonShow("SetBlock Get error!");
return(-abs(errno));
}
flags&=~O_NONBLOCK;
if(fcntl(fd,F_SETFL,flags)==-1) {
CommonShow("SetBlock Set error!");
return(-abs(errno));
}
return(0);
}
/**/
int Readn(int Fd,char *Buf,int n)
{
int m;
int ReadNum;
char *p;
p=Buf;
m=n;
ReadnAgain:
if((ReadNum=read(Fd,p,m))<0)
{
if(errno==EINTR || errno==EAGAIN)
goto ReadnAgain;
else if(errno==ECONNRESET)
return(0);
CommonShow("Readn");
return(-abs(errno));
}
else if (ReadNum==0)
return(0);
else {
p+=ReadNum;
m-=ReadNum;
if(m==0)
{
*p=0;
return(n);
}
else if(m>0) {
goto ReadnAgain;
}
else {
CommonShow("Unexpected fatal error");
*p=0;
return(-10000);
}
}
}
/*将十六进制转化为十进制*/
int HexToInt(char *h,int n)
{
int s;
int i;
int a[10];
if(n==0) n=strlen(h);
for(i=0;i<n;i++)
if((h[i]>='0')&&(h[i]<='9'))
a[i]=h[i]-48;
else if((h[i]>='A')&&(h[i]<='F'))
a[i]=h[i]-55;
else if((h[i]>='a')&&(h[i]<='f'))
a[i]=h[i]-87;
else if (h[i]==' ')
a[i]=0;
else
{
CommonShow("Error,The string is not a hex string:HexToDeci");
return(-10000);
}
s=a[0];
for(i=1;i<n;i++)
s=s*16+a[i];
return(s);
}
int IntToHex(char *bufback, int iValue)
{
sprintf(bufback, "%4X", iValue);
}
/*取系统时间*/
void SysTime(int *year,int *mon,int *day,int *hour,int *min,int *sec)
{
struct tm *tt;
time_t secs;
time(&secs);
tt=localtime(&secs);
*year=tt->tm_year+1900;
*mon=tt->tm_mon+1;
*day=tt->tm_mday;
*hour=tt->tm_hour;
*min=tt->tm_min;
*sec=tt->tm_sec;
}
/*************************************************************/
/*取系统时间*/
void GetSysTime(char *t)
{
int year,mon,day,hour,min,sec;
SysTime(&year,&mon,&day,&hour,&min,&sec);
sprintf(t,"%04d%02d%02d%02d%02d%02d",year,mon,day,hour,min,sec);
}
/******************************************************************************/
/*
void CommonShow(char *w)
{
char message[200];
GetSysTime(message);
sprintf(message+14," :%8d: %s\n",getpid(),w);
printf("%s", message);
} */
/************************************************************/
/*
void SubShow(int tid, char *w)
{
char message[200];
GetSysTime(message);
sprintf(message+14," :%8d: %s\n",tid,w);
printf("%s", message);
} */
/*************************************************/
/** 解密函数 **/
/*************************************************/
void decodekey(char *source,char *dest)
{
char *s;
char highchar[4] = {0x41,0x01,0x40,0x00};
int i,len,cnt,li_loop;
char cc,c1,c2,c3,c4;
len = strlen(source);
cnt = len / 4;
for (li_loop = 0;li_loop < cnt;li_loop ++)
{
s = source;
cc = c1 = c2 = 0x00;
for(i=3;i>=0;)
{
c2 <<= 2;
cc = s[li_loop*4 + i];
c4 = cc & 0x41;
switch (c4)
{
case 65:
c1 = 0;
break;
case 1:
c1 = 1;
break;
case 0x40:
c1 = 0x02;
break;
case 0:
c1 = 0x03;
break;
default:
break;
}
c2 = c1 | c2;
i--;
}
c3 = ~c2;
dest[li_loop] = c3;
dest[li_loop + 1] = 0x00;
}
}
/*************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -