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

📄 lib.c

📁 本Linux网络应用程序采用客户-服务器模型,并发型交互。在OSI参考模型的传输层,通过调用TCP套接字(Socket)的各种函数,使服务器和各个客户端之间建立快速可靠的连接
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int isin(char *s, int c)
{
	int offs=0;

	while(s[offs] != '\0') {
		if (c == s[offs])
			return offs;
		else
			offs++;
	}
	return -1;
}

/*
 * Make arglist array (*arglist) for tokens in line which are separated
by
 * demchar. Return -1 on error or the number of tokens otherwise.
 * In the args, when type == 0, does not malloc memory for copy.
 * when type == 1, produce a clone of line.
 */
/*
 * changed the indication of type, this is an example to explain it:
 * there is a string line: a,b,,c,
 * demchar is: ,
 * when type==0: 1. a  2. b  3. c
 * when type==1: 1. a  2. b  3. (null)  4. c  5.(null)
 * the former indication of type is kept as 0.
 * (Mar/19/2001)
 */
int parse_line(char* line, char* demchar, char*** arglist, int type)
{
	char *t, *p, *q;
	char* snew;
	int numtokens = 0;
	int len;
	int i;
	int c;

	/*snew is real start of string after skipping leading demchar*/
	if (type == 0)
		snew=line+strspn(line, demchar);
	else 
		snew=line;

	len = strlen(snew) + 1;
	if ((t=(char*)malloc(len * sizeof(char)))==NULL){
		*arglist=NULL;
		numtokens=-1;
		return numtokens;
	}
	
	memset(t, '\0', len);
	strcpy(t, snew);

	/*
	 * get the numtokens under two conditions. (Mar/19/2001)
	 */
	if (type == 0) {   
		if (strtok(t, demchar)==NULL)
			numtokens=0;
		else
			for(numtokens=1; strtok(NULL, demchar)!=NULL; 
				numtokens++)
				;
	} else {
		p = t;
		
		do {
			q = p + strcspn(p, demchar);
			c = q[0];
			p = q + 1; 
			numtokens++;
		} while (isin(demchar, c) >= 0);
	}
	

	/*create an argument array to contain ptrs to tokens*/
	if( (*arglist = (char**)malloc((numtokens+1) * sizeof(char**))) == NULL
){
		memset(t, 0, len*sizeof(char));
		free(t);
		numtokens=-1;
		return numtokens;
	}
	
	/*
	 * insert pointers to tokens into the array
	 */	
	memset(t, 0, len*sizeof(char));
	free(t);
	
	if (numtokens>0){
		t = snew;
		
		if (type == 0) {
			**arglist=strtok(t, demchar);
			for(i=1; i<numtokens+1; i++)
				*((*arglist)+i)=strtok(NULL, demchar);
				
		} else {
			p = t;
			i = 0;
			numtokens = 0;
		
			do {
				q = p + strcspn(p, demchar);
				c = q[0];
				q[0] = '\0';
				*((*arglist)+i) = p;
				p = q + 1; 
				numtokens++;
				i++;
			} while (isin(demchar, c) >= 0);
		}
	} else {
		**arglist=NULL;
		return numtokens;
	}
	return numtokens;
}

/*
 * Judge if the s is a number string
 * if is, return 1, otherwise, return 0
 */
int isnum(char *s)
{
	int i = 0;

	while (s[i] != '\0') {
		if (isdigit(s[i]) == 0)
			return 0;
		else
			i++;
	}
	return 1;
}

unsigned long ip2val(char *ip)
{
	struct in_addr inaddr;

	inet_aton(ip, &inaddr);
	return (ntohl(inaddr.s_addr));
}

char *val2ip(unsigned long val, char *ip)
{
	struct in_addr inaddr;

	inaddr.s_addr = htonl(val);
	strcpy(ip, inet_ntoa(inaddr));
	return ip;
}

unsigned long long str2ll(char *s, int base)
{
	int i;
	char c;
	int len;
	unsigned long long val=0;

	len = strlen(s);
	for (i=1; i<=len; i++) {
		c = s[i - 1];
		if ( ((c <= 'f') && (c >= 'a')) ||
			 ((c <= 'F') && (c >= 'A')) ||
			 ((c <= '9') && (c >= '0')) ) {
			c = ((c <= '9') && (c >= '0')) ? (c - 48) : (toupper(c) - 55);

			if (i > 1) 
				val *= (unsigned long long)base;

			val += (unsigned long long)c; 
		} else 
			return 0L;
	}
	return val;
}

⌨️ 快捷键说明

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