多进程Traceroute小工具(源码)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
#include 
#include 
#include 
#include 

#define LOG_WARNING(s, args...) printf("\033[33;48m[%s->%d]"s"\033[0m\n", __FILE__, __LINE__, ##args);
#define RED "31"
#define GREEN "32"
#define YELLOW "33"
#define BLUE "34"
#define PURPLE "35"
#define QINGLAN "36"
#define MARKG(s) printf("\033[32;48m%s\033[0m", s)
#define PRINTG(s, args...) printf("\033[32;48m"s"\n\033[0m", ##args)
#define COLOR_STR(str, corlor) "\033["corlor";48m"str"\033[0m"
#define MARK(s, args...) printf("In %s line %d, "s"\n", __FILE__, __LINE__, ##args);fflush(stdout)
#define LOG_ERROR(s, args...) {\
	char _error_log[1024];\
	sprintf(_error_log, "[Error] in %s(%s) line %d, "s"\n", __FILE__, __FUNCTION__, __LINE__, ##args);\
	logilx(_error_log, "/var/log/error_cba_v2.log");\
	printf("\033[31;48m%s\033[0m\n", _error_log);\
}

#define CPR(s, args...) printf(COLOR_STR(s, RED), ##args)
#define CPG(s, args...) printf(COLOR_STR(s, GREEN), ##args)
#define CPY(s, args...) printf(COLOR_STR(s, YELLOW), ##args)
#define CPP(s, args...) printf(COLOR_STR(s, PURPLE), ##args)
#define CPB(s, args...) printf(COLOR_STR(s, BLUE), ##args)
#define CPQ(s, args...) printf(COLOR_STR(s, QINGLAN), ##args)

char** split(char *strin, char *seprator, int *l, int max) {
	char **rtn;
	rtn = (char **) malloc(sizeof(char **));
	char *str = strdup(strin);
	char *str0 = str;
	char *p;

	p = strstr(str, seprator);
	if (p == NULL) {
		*l = 1;
		*rtn = str;
		return rtn;
	}

	int i, len, spl, inlen, plen;
	spl = strlen(seprator);
	max--;
	*l = 0;
	while (p != NULL) {
		if (*l > 0)
			rtn = realloc(rtn, ((*l) + 1) * sizeof(char **));
		inlen = strlen(str);
		plen = strlen(p);
		if (!plen)
			break;
		len = inlen - plen;

		rtn[*l] = malloc(len + 1);
		strncpy(rtn[*l], str, len);
		rtn[*l][len] = 0;

		str += (len + spl);
		p = strstr(str, seprator);

		(*l)++;
		if (*l == max || p == NULL) {
			/*if (*l == max) {
			 len = strlen(rtn[*l - 1]) + strlen(str) + 1;
			 rtn[*l - 1] = realloc(rtn[*l - 1], len);
			 strncat(rtn[*l - 1], str, len - 1);
			 } else {*/
			len = strlen(str);
			rtn = realloc(rtn, ((*l) + 1) * sizeof(char **));
			rtn[*l] = malloc(len + 1);
			strncpy(rtn[*l], str, len);
			rtn[*l][len] = 0;
			//}
			(*l)++;
			break;
		}
	}
	free(str0);
	return rtn;
}

void exeCmd(char* cmd, char* rtn, int lenRtn){
	char line_buf[256]={0};

	FILE *fp = popen(cmd, "r");
	if(fp == NULL){

		return;
	}

	rtn[0] = 0;

	while(!feof(fp))
	{
		if(fgets(line_buf, sizeof(line_buf), fp)){
			if(strlen(line_buf)+strlen(rtn) 0)
			strncat(buffer, buf, l);

		if (pos + len > limit) {
//			LOG_ERROR("filesize %s bigger than %d\n", buff_path, limit);
			break;
		}
	}
	*size = pos;
	free(buf);
	//printf("Read %s, data length: %d\n", filename, pos);
	fclose(fp);
	buffer[pos - 1] = '\0';
	return buffer;
}
void split_free(char**rtn, int l){
	int i;
	for(i=0;i 0 ? "a+" : "w+");
	//MARK("buff_path=%s", buff_path);
	if (fp==NULL) {
//		LOG_ERROR("Write file %s failed, %s", buff_path, strerror(errno));
		return;
	}
	int w = fwrite(data, 1, len, fp);
	fflush(fp);
	//MARK("Write file %s may OK,need write %d=w %d, errno=%d", buff_path, len, w, errno);
	fclose(fp);
}
void now_time_str(char* str1,int str1_len){
	time_t timep;
	struct tm systm;

	time(&timep);
	timep += 8 * 3600;

	char str[48];
	//------------------------------------------
	gmtime_r( &timep, &systm );
	//sprintf(str,"%04d%02d%02d%02d:%02d",systm.tm_year+1900,systm.tm_mon+1,systm.tm_mday,systm.tm_hour,systm.tm_min,systm.tm_sec);
	snprintf(
				str, sizeof(str), "%04d-%02d-%02d %02d:%02d:%02d",
				systm.tm_year+1900,systm.tm_mon+1,systm.tm_mday,
				systm.tm_hour,systm.tm_min,systm.tm_sec
			);
	if(str1_len1) strcpy(cfg_file, argv[1]);
	if(argc>2) strcpy(base_trace_cmd, argv[2]);

	int file_size = 0;
	char *cnts = doReadFile(cfg_file, 4096, &file_size);
	int count = 0;
	char *ip;
	char** ips = split(cnts, "\n", &count, 1024);
	char** pids = malloc(count*sizeof(char*));
	int i;
	printf("Got %d ips\n", count);
	for(i=0;i0) {
			pids[i] = ip;
			CPG("[%d] %s\n", i, pids[i]);
		} else exit(0);
	}
	CPG("All ips started\n");

	free(cnts);
	int left = count;

	char *p;

	now_time_str(cmd, sizeof(cmd));
	strcat(cmd, ": \r\n");

	writeFile("ok_ips.txt", cmd, strlen(cmd), 1);
	writeFile("no_ips.txt", cmd, strlen(cmd), 1);

	for(i=0;i0){
		for(i=0;i0) {
				p = strchr(cnts, '\n');
				if(p==NULL) {
					printf("error(not chr(10)): %s\n", cnts);
					exit(0);
				}

				p = strstr(p, pids[i]);
				sprintf(cmd, "%s,", pids[i]);
				if(p!=NULL) {
					writeFile("ok_ips.txt", cmd, strlen(cmd), 1);
					printf("%s ok\n", cmd);
				}else{
					writeFile("no_ips.txt", cmd, strlen(cmd), 1);
					printf("%s fail\n", cmd);
				}

				left --;
				pids[i][0] = 0;

			}//else CPY(" > %s\n", cmd);

			free(cnts);
		}
		CPY("Working...\n");
		sleep(1);
	}

	writeFile("ok_ips.txt", "\n", 1, 1);
	writeFile("no_ips.txt", "\n", 1, 1);
	split_free(ips, count);
}

 

gcc  -o multi_trace multi_trace.c
 

 

你可能感兴趣的:(C,&&,OC,&&,iOS)