📄 netlink_socket.c
字号:
if (fn==NULL||str==NULL)
return -1;
//fp = fopen( "file.txt", "a" ); //append
fp = fopen( fn, "w" );
if ( fp==NULL )
{
perror( "open file failure" );
return -1;
}
//fseek( fp, 0, SEEK_END );
fputs( str, fp );
fclose( fp );
return 0;
}
int FileAddLine( char * filename, char * content )
{
FILE *fp = NULL;
if ( filename==NULL || content==NULL )
{
PrintS( "para is null" );
return -1;
}
fp = fopen( filename, "a" );
if ( fp==NULL ) // open file failure.
{
perror("fopen file error");
return -2;
}
fputs( content, fp );
fclose( fp );
return 0;
}
int FileAddInt( char * filename, int num )
{
char buf[256] = "";
sprintf( buf, "%d\n", num );
return FileAddLine( filename, buf );
}
// 将str中从子串sub1到子串sub2之间的内容替换为rplc
int StrSubReplace( char *str, const char *sub1, const char *sub2, const char *rplc )
{
char *p, *p1 ;
if ( str==NULL || sub1==NULL || sub2==NULL || rplc==NULL )
return -1;
p = strstr( str, sub1 );
if ( p==NULL ) return -1;
p += strlen(sub1);
*p++ = '\0';
p = strstr( p, sub2 );
if ( p==NULL ) return -1;
p1 = strdup( p );
if ( p1==NULL ) return -1;
strcat( str, rplc );
strcat( str, p1 );
free( p1 );
return 0;
}
// #define FILENAME "2101-isp-conf"
// void main()
// {
// char ss[10240];
// FileReadWhole( FILENAME, ss, 10240 );
// printf("%s\n\n\n", ss);
// StrSubReplace( ss, "ATD", "\n", "*99##" );
// printf("%s\n", ss);
// //FileWrite( FILENAME, ss );
// //FilePrint( FILENAME );
// }
//////////////////////////////////////////////////////////////////////////
// fifo
// pathname: "/tmp/fifo.1"
// mode: O_WRONLY, O_RDONLY
int FifoOpen( const char * pathname, int mode )
{
int fd;
if ( (mkfifo( pathname, O_RDWR | O_CREAT | O_EXCL)<0) &&
(errno != EEXIST) )
{
perror("mkfifo");
return -1;
}
fd = open( pathname, mode, 0 );
if ( fd<0 )
{
perror("open error");
return -1;
}
return fd;
}
// int main(int argc, char *argv[])
// {
// if ( fork()==0 )
// {
// int fd;
// char buf[256] = "";
// int len;
// usleep(500000);
//
// fd = FifoOpen(FIFO1, O_RDONLY);
// if ( fd==-1 )
// return -1;
//
// //printf("read....\n");
// len = read( fd, buf, 256 );
// printf( "read: len=%d, buf=%s\n", len, buf );
// exit(0);
// }
// int fd = FifoOpen( FIFO1, O_WRONLY);
// if (fd==-1) return -1;
//
// //printf("Please input:");
// char buf[256] = "abc";
// //scanf( "%s", buf );
// write( fd, buf, strlen(buf)+1 );
// printf( "write data: %s\n", buf );
//
// return 0;
// }
//////////////////////////////////////////////////////////////////////////
int CreateChildProcess( int (*proc)(void) ) // whr 04/02
{
pid_t pid;
int ret;
switch (pid = fork())
{
case -1: /* error */
perror("fork");
return -1;
case 0: /* child */
ret = proc();
exit(ret);
default: /* parent */
break;
}
return pid;
}
//////////////////////////////////////////////////////////////////////////
// #define BOOL unsigned char
// #define FALSE 0
// #define TRUE 1
// #define FAILURE -1
// #define SUCCESS 0
#define FILE_SIZE 64*1024
//#define NULL 0
#define FILENAME1 "/tmp/desay_para.proc"
static int StringTrim( char * str )
{
char *p = NULL;
if ( str==NULL )
return FAILURE;
//p = strtok( str, " \t\r\n" );
p = strtok( str, " \t" );
if ( p!=NULL )
strcpy( str, p );
else
{
str[0] = '\0';
return FAILURE;
}
return SUCCESS;
}
char * ParaGet( char *para )
{
FILE *fp = NULL;
static char line[256] = "";
char * p;
if ( para==NULL )
return NULL;
// if file is not exist, then create it.
fp = fopen( FILENAME1, "a+");
if ( fp != NULL )
fclose(fp);
fp = fopen( FILENAME1, "r" ); // open of reading file
if ( fp==NULL )
{
// printf( "open file %s failure.\n", fp );
perror( "open file failure" );
return NULL;
}
while (!feof(fp) &&
fgets(line, 256,fp) )
{
StringTrim( line );
p = strchr( line, '=' );
if (p==NULL)
continue;
*p = '\0';
if ( strcmp(line, para)==0 )
{
fclose ( fp );
return p+1;
}
}
fclose( fp );
return NULL;
}
int ParaSet( char *para, char * value )
{
FILE *fp = NULL;
char content[FILE_SIZE] = "";
char sTmp[256];
char sName[256]="";
int len = 0;
char *p;
int cstart = -1;
int tlen;
if ( para == NULL || value==NULL )
return -1;
//sprintf( content, "%s=%s", para, value );
// if file is not exist, then create it.
fp = fopen( FILENAME1, "a+");
if ( fp != NULL )
fclose(fp);
// read whole file to content
fp = fopen( FILENAME1, "r+" );
if ( fp==NULL )
{
//printf( "open file %s failure.\n", fp );
perror( "open file failure" );
return -2;
}
while (!feof(fp) &&
fgets(sTmp, 256, fp) )
{
StringTrim( sTmp );
if ( cstart==-1 ) // not found
{
p = strchr( sTmp, '=' );
if (p==NULL)
continue;
strncpy( sName, sTmp, p-sTmp );
sName[p-sTmp] = '\0';
if ( strcmp(para, sName)==0 )
{
cstart = len; // 指定位置
}
strcpy( content+len, sTmp );
if ( cstart != -1)
len+=256;
else
len+=strlen(sTmp);
}
else
{
strcpy( content+len, sTmp );
len += strlen(sTmp);
}
}
//
tlen = len-1<0 ? 0 : len-1;
if (content[tlen]=='\n' || content[tlen]=='\0' )
sprintf( sTmp, "%s=%s\n", para, value );
else
sprintf( sTmp, "\n%s=%s\n", para, value );
if ( cstart == -1 ) // not find.
{
fseek( fp, 0, SEEK_END );
fputs( sTmp, fp );
fclose( fp );
return 0;
}
//
strcpy( content+cstart, sTmp );
strcpy( content+cstart+strlen(sTmp), content+cstart+256 );
//fseek( fp, 0, SEEK_SET );
// rewind(fp);
fclose( fp );
fp = fopen( FILENAME1, "w" ); // Opens an empty file for writing.
// If the given file exists, its contents are destroyed.
if ( fp==NULL )
{
//printf( "open file %s failure.\n", fp );
perror( "open file failure" );
return -3;
}
// {
// int i;
// for ( i=strlen(content); i<len; i++ )
// content[i] = ' ';
// content[i] = '\0';
// }
fputs( content, fp );
fclose( fp );
return 1;
}
int GetFileLength( const char * filename )
{
FILE * fp;
int filelen;
fp = fopen( filename, "r" );
if ( fp == NULL )
{
if ( errno==ENOENT ) // file is not exist
return -2;
perror("fopen error when get file length.");
return -1;
}
// get file len
fseek( fp, 0, SEEK_END );
filelen = ftell(fp);
fclose( fp );
return filelen;
}
BOOL FileIsExist( char * filename )
{
FILE *fp = NULL;
if ( filename==NULL )
{
PrintS( "filename is null" );
return FALSE;
}
fp = fopen( filename, "r" );
if ( fp==NULL ) // open file failure.
{
//PrintInt( errno );
//PrintInt( fp );
if ( errno==ENOENT ) // file is not exist
return FALSE;
else
return TRUE;
}
fclose( fp );
return TRUE;
}
int GetPidByName(const char *pName){#define FNAME "/tmp/getpidbyname" FILE * fp=NULL; char tmp[256] = ""; int pid = 0; if ( pName==NULL ) return -1; sprintf( tmp, "ps | grep %s > "FNAME, pName ); if ( system ( tmp ) < 0 ) { perror("GetPidByName: system error"); return -2; } fp = fopen( FNAME, "r" ); if ( fp==NULL ) { perror("GetPidByName: fopen error"); return -3; } while ( !feof(fp) && fgets(tmp, 256, fp) ) { if ( strstr(tmp, pName)!=NULL ) // find first { fclose ( fp ); // dPrint( "tmp=%s", tmp ); pid = atoi( tmp ); if ( pid != 0 ) return pid; else return -4; } }// memset( tmp, 0, 256 );// fgets( tmp, 256, fp );// Print( "tmp=%s", tmp );// close( fp );// pid = atoi( tmp );// if ( pid!=0 )// {// return pid;// }
#undef FNAME fclose ( fp ); return -5;}int KillProcessByName( const char * ProcessName ){ int pid; char spid[256]; if ( ProcessName==NULL ) return -1; pid = GetPidByName( ProcessName ); //Print( "pid=%d", pid ); if ( pid < 0 ) sprintf( spid, "killall %s > /dev/null", ProcessName ); // killall command sometimes can not work well. else sprintf( spid, "kill -9 %d > /dev/null", pid ); system( spid ); return 0;}
int KillProcessByNameEx( const char * ProcessName )
{
int ret;
char spid[256];
if ( ProcessName==NULL )
return -1;
ret = GetPidByName( ProcessName );
if ( ret<0 ) return -1;
// kill pppd by killall command
sprintf( spid, "killall -1 %s 1> /dev/null 2> /dev/null", ProcessName );
system( spid );
// kill pppd by kill command second time
sprintf( spid, "kill -9 %d 1> /dev/null 2> /dev/null", ret );
system( spid );
}
char * GetCurrentTime(void)
{
time_t timep; struct tm tm;
char content[256];
time(&timep);
//#if defined(linux)
// setenv("TZ", nvram_safe_get("time_zone"), 1);
//#endif
memcpy(&tm, localtime(&timep), sizeof(struct tm));
// p = localtime(&timep); // 取得当地时间
sprintf( content, "%d/%02d/%02d %02d:%02d:%02d",
(1900+tm.tm_year), (1+tm.tm_mon), tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec );
return strdup( content );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -