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

📄 etc.c

📁 UNIX/LINUX平台下面SMS网管原代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * FILE : etc.c */#include <time.h>#include <malloc.h>#include <math.h>#include <stdlib.h>#include <string.h>#include <sys/time.h>#include <sys/resource.h>#include "etc.h"#define SPLIT		"#"	/* 补充字符*/#define CONST_MOD	10	/*模常量*/int  ig_CharLen;char cg_Buffer[1024],cg_Buffer1[1024];/* Global function */char PUB_ProCifCusNoVer(char *epcBuf);/* Local function */static void Trim_SupChar();static int Do_Comp_Val(int n);static char CustomNo_Ver_Pro();/*字符值对照*/static int char_map[][10]={        {         '0','1','2','3','4','5','6','7','8','9'        },        {          0,1,2,3,4,5,6,7,8,9        }};extern int errno;void PureNumber(char *purenum, char *mixnum);/* * This function locate the specified section in the etc file. */static int etc_LocateSection(FILE* fp, char* pSection, FILE* bak_fp){    char szBuff[ETC_MAXLINE + 1];    char* current;    char* tail;    while (TRUE) {        if (!fgets(szBuff, ETC_MAXLINE, fp)) {            if (feof (fp))                return ETC_SECTIONNOTFOUND;            else                return ETC_FILEIOFAILED;        }        else if (bak_fp && fputs (szBuff, bak_fp) == EOF)            return ETC_FILEIOFAILED;                current = szBuff;        while (*current == ' ' ||  *current == '\t') current++;         if (*current == ';' || *current == '#') continue;        if (*current++ == '[')             while (*current == ' ' ||  *current == '\t') current ++;        else            continue;        /*Locate the tail.*/        tail = current;        while (*tail != ']' && *tail != '\n' &&              *tail != ';' && *tail != '#' && *tail != '\0')              tail++;        *tail = '\0';        while (*tail == ' ' || *tail == '\t') {            *tail = '\0';            tail--;         }                    if (strcmp (current, pSection) == 0)            return ETC_OK;     }}/*  *This function locate the specified key in the etc file. */static int etc_LocateKeyValue(FILE* fp, char* pKey,                                int bCurSection, char* pValue, int iLen,                               FILE* bak_fp, char* nextSection){    char szBuff[ETC_MAXLINE + 1 + 1];    char* current;    char* tail;    char* value;    while (TRUE) {        if (!fgets(szBuff, ETC_MAXLINE, fp))            return ETC_FILEIOFAILED;        if (szBuff [strlen (szBuff) - 1] == '\n')            szBuff [strlen (szBuff) - 1] = '\0';        current = szBuff;        while (*current == ' ' ||  *current == '\t') current++;         if (*current == ';' || *current == '#') continue;        if (*current == '[') {            if (bCurSection) {                strcpy (nextSection, current);                return ETC_KEYNOTFOUND;             }            else                continue;        }        tail = current;        while (*tail != '=' && *tail != '\n' &&              *tail != ';' && *tail != '#' && *tail != '\0')              tail++;        value = tail + 1;        if (*tail != '=')            *value = '\0';         *tail-- = '\0';        while (*tail == ' ' || *tail == '\t') {            *tail = '\0';            tail--;         }                    if (strcmp (current, pKey) == 0) {            tail = value;            while (*tail != '\n' && *tail != '\0') tail++;            *tail = '\0';             if (pValue)                strncpy (pValue, value, iLen);            return ETC_OK;         }        else if (bak_fp && *current != '\0')            fprintf (bak_fp, "%s=%s\n", current, value);    }}/* Function: GetValueFromEtcFile(char* pEtcFile, char* pSection, *                               char* pKey, char* pValue, int iLen); * Parameter: *     pEtcFile: etc file path name. *     pSection: Section name. *     pKey:     Key name. *     pValue:   The buffer will store the value of the key. *     iLen:     The max length of value string. * Return: *     int               	  meaning *     ETC_FILENOTFOUND           The etc file not found.  *     ETC_SECTIONNOTFOUND        The section is not found.  *     ETC_EKYNOTFOUND            The Key is not found. *     ETC_OK            	  OK. */int GetValueFromEtcFile(char* pEtcFile, char* pSection,                               char* pKey, char* pValue, int iLen){    FILE* fp;    char tempSection [ETC_MAXLINE + 2];    if (!(fp = fopen(pEtcFile, "r")))         return ETC_FILENOTFOUND;    if (pSection)         if (etc_LocateSection (fp, pSection, NULL) != ETC_OK) {             fclose (fp);             return ETC_SECTIONNOTFOUND;         }    if (etc_LocateKeyValue (fp, pKey, pSection != NULL,                 pValue, iLen, NULL, tempSection) != ETC_OK) {         fclose (fp);         return ETC_KEYNOTFOUND;    }    fclose (fp);    return ETC_OK;}/* Function: GetIntValueFromEtcFile(char* pEtcFile, char* pSection, *                               char* pKey); * Parameter: *     pEtcFile: etc file path name. *     pSection: Section name. *     pKey:     Key name. * Return: *     int                      	meaning *     ETC_FILENOTFOUND             The etc file not found.  *     ETC_SECTIONNOTFOUND          The section is not found.  *     ETC_EKYNOTFOUND              The Key is not found. *     ETC_OK                       OK. */int GetIntValueFromEtcFile(char* pEtcFile, char* pSection,                               char* pKey, int* value){    int ret;    char szBuff [51];    ret = GetValueFromEtcFile (pEtcFile, pSection, pKey, szBuff, 50);    if (ret < 0)        return ret;    *value = strtol (szBuff, NULL, 0);    if ((*value == LONG_MIN || *value == LONG_MAX) && errno == ERANGE)        return ETC_INTCONV;    return ETC_OK;}static int etc_CopyAndLocate (FILE* etc_fp, FILE* tmp_fp,                 char* pSection, char* pKey, char* tempSection){    if (pSection && etc_LocateSection (etc_fp, pSection, tmp_fp) != ETC_OK)        return ETC_SECTIONNOTFOUND;    if (etc_LocateKeyValue (etc_fp, pKey, pSection != NULL,                 NULL, 0, tmp_fp, tempSection) != ETC_OK)        return ETC_KEYNOTFOUND;    return ETC_OK;}static int etc_FileCopy (FILE* sf, FILE* df){    char line [ETC_MAXLINE + 1];        while (fgets (line, ETC_MAXLINE + 1, sf) != NULL)        if (fputs (line, df) == EOF) {            return ETC_FILEIOFAILED;        }    return ETC_OK;}/* Function: SetValueToEtcFile(char* pEtcFile, char* pSection, *                               char* pKey, char* pValue); * Parameter: *     pEtcFile: etc file path name. *     pSection: Section name. *     pKey:     Key name. *     pValue:   Value. * Return: *     int                      meaning *     ETC_FILENOTFOUND         The etc file not found. *     ETC_TMPFILEFAILED        Create tmp file failure. *     ETC_OK                   OK. */int SetValueToEtcFile (char* pEtcFile, char* pSection,                               char* pKey, char* pValue){    FILE* etc_fp;    FILE* tmp_fp;    int rc;    char tempSection [ETC_MAXLINE + 2];    if ((tmp_fp = tmpfile ()) == NULL)        return ETC_TMPFILEFAILED;    if (!(etc_fp = fopen (pEtcFile, "r+")))     {        fclose (tmp_fp);        if (!(etc_fp = fopen (pEtcFile, "w")))            return ETC_FILEIOFAILED;        fprintf (etc_fp, "[%s]\n", pSection);        fprintf (etc_fp, "%s=%s\n", pKey, pValue);        fclose (etc_fp);        return ETC_OK;    }    switch (etc_CopyAndLocate (etc_fp, tmp_fp, pSection, pKey, tempSection))     {   	 case ETC_SECTIONNOTFOUND:       		 fprintf (tmp_fp, "\n[%s]\n", pSection);       		 fprintf (tmp_fp, "%s=%s\n", pKey, pValue);        	 break;        	case ETC_KEYNOTFOUND:       		 fprintf (tmp_fp, "%s=%s\n\n", pKey, pValue);        	 fprintf (tmp_fp, "%s\n", tempSection);    		 break;    	default:        	 fprintf (tmp_fp, "%s=%s\n", pKey, pValue);        	 break;    }    if ((rc = etc_FileCopy (etc_fp, tmp_fp)) != ETC_OK)        goto error;        /*     * replace etc content with tmp file content     * truncate etc content first     */    fclose (etc_fp);    if (!(etc_fp = fopen (pEtcFile, "w")))        return ETC_FILEIOFAILED;        rewind (tmp_fp);    rc = etc_FileCopy (tmp_fp, etc_fp);error:    fclose (etc_fp);    fclose (tmp_fp);    return rc;}/* * FUNCTION    :  GetField * Description :  Read the specified field from key value * Script by   :  TANG  * Created on  :  April 11, 2003  * Recent mod  :  */int GetField(char *buffer, char delimiter, char *field, int fieldnum){	char *p1,*p2;	int  i;	for(i = 1; i <= fieldnum; i++)	{	 	p1 = buffer;		p2 = strchr(p1, delimiter);		if( p2 == NULL) 		{			break;		}		buffer = p2 + 1;	}	if( p2 == NULL)	{		 strcpy(field, buffer); 	}	else	{		strncpy(field, p1,  p2 - p1);	}	return 0;}int GetField1(char *buffer, char delimiter, char *field, int fieldnum){	char *p1,*p2;	int  i;	for(i = 1; i <= fieldnum; i++)	{	 	p1 = buffer;		p2 = strchr(p1, delimiter);		if( p2 == NULL) 		{			break;		}		buffer = p2 + 1;	}	if( p2 == NULL)	{		 strcpy(field, buffer); 	}	else	{		strncpy(field, p1,  p2 - p1);	}	return 0;}/* * FUNCTION    :  GetFieCount * Description :  Count the items from the key value * Script by   :  TANG  * Created on  :  April 11, 2003  * Recent mod  :  */int GetFieldCount(char *buffer, char delimiter){	char *p1,*p2;	int counts = 0;	char *term = (char *)0x01;	while(1)	{		p1 = buffer;		p2 = strchr(p1, delimiter);		buffer = p2 + 1;		if(buffer == term) break;		counts++;	}	return counts + 1;}/* * GetDate() -- 取日期 *   in  para -- 无 *   out para -- 无 *   return   -- -1: 失败, >0: 日期 */long GetDate(){    char temp[10];    struct tm *tm;    time_t t1;    t1 = time(NULL);    tm = localtime(&t1);    sprintf(temp, "%04d%02d%02d", 1900+tm->tm_year, tm->tm_mon+1, tm->tm_mday);    return atol(temp);}/* GetTime() -- 取时间 *   in  para -- 无 *   out para -- 无 *   return   -- -1: 失败, >0: 时间 */long GetTime(){    char temp[10];    struct tm *tm;    time_t t1;    t1 = time(NULL);    tm = localtime(&t1);    sprintf(temp, "%02d%02d%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);    return atol(temp);}int CheckMemory(char *title, struct mallinfo *minfo){	FILE *fp;	char filename[100];	sprintf(filename, "%s/log/mem%ld.txt", getenv("HOME"), GetDate());	fp = fopen(filename, "at");	fprintf(fp, "==========================\n");	fprintf(fp, "%s\n", title);	fprintf(fp, "==========================\n");	fprintf(fp, "arena    = [%ld]\n", minfo->arena);	fprintf(fp, "ordblks  = [%d]\n", minfo->ordblks);	fprintf(fp, "uordblks = [%ld]\n", minfo->uordblks);	fprintf(fp, "fordblks = [%ld]\n\n", minfo->fordblks);	fclose(fp);	return 0;}/* * function	: ComputeTimeL * params	: start - 13:10:10 *		: end   - 14:10:09 * return	: time in seconds */int ComputeTime(char *start, char *end){	char p_h[3], q_h[3];	char p_m[3], q_m[3];	char p_s[3], q_s[3];	int  h, m, s, tmptime = 0;	memset(p_h, 0, sizeof(p_h));	memset(q_h, 0, sizeof(p_h));	GetField(start, ':', p_h, 1);	GetField(end, ':', q_h, 1);	memset(p_m, 0, sizeof(p_m));	memset(q_m, 0, sizeof(p_m));	GetField(start, ':', p_m, 2);	GetField(end, ':', q_m, 2);	GetField(start, ':', p_s, 3);	GetField(end, ':', q_s, 3);	tmptime = (60*60*atoi(q_h) + 60*atoi(q_m) + atoi(q_s)) - \		  (60*60*atoi(p_h) + 60*atoi(p_m) + atoi(p_s));	return tmptime;}/* * function	: ComputeTimeL * params	: start - 131010 *		: end   - 141009 * return	: time in seconds */int ComputeTimeL(char *start, char *end){	char p_h[3], q_h[3];	char p_m[3], q_m[3];	char p_s[3], q_s[3];	int  h, m, s, tmptime = 0;	memset(p_h, 0, sizeof(p_h));	memset(q_h, 0, sizeof(p_h));	memcpy(p_h, start + 0, 2);	memcpy(q_h, end + 0, 2);	memset(p_m, 0, sizeof(p_m));	memset(q_m, 0, sizeof(p_m));	memcpy(p_m, start + 2, 2);	memcpy(q_m, end + 2, 2);/**	memcpy(p_s, start + 4, 2);	memcpy(q_s, end + 4, 2); **/	sprintf(p_s, "%c%c", start[4], start[5]);	sprintf(q_s, "%c%c", end[4], end[5]);	tmptime = (60*60*atoi(q_h) + 60*atoi(q_m) + atoi(q_s)) - \		  (60*60*atoi(p_h) + 60*atoi(p_m) + atoi(p_s));	return tmptime;}int IsNullStr(char *string){

⌨️ 快捷键说明

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