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

📄 dnsdri.cpp

📁 一个简易dns 服务器
💻 CPP
字号:
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dns.h"
#include <windows.h>
#include <process.h>
 
int Add(char **array, int *num)
{
	int i;
	char in[WORD_LENGTH];
	printf("insert one like The Domain name / IP address :");
	scanf("%s",in);
    char *wordin;

    wordin = (char *)malloc((strlen(in) + 1) * sizeof(char));
    
    strcpy(wordin, in);
	for(i=0;i<*num;i++)
		if(strcmp(array[i],wordin)>=0)
			break;
	if(i!=*num)
	if(strcmp(array[i],wordin)==0)
	{
				return 0;
	}
	int mover=*num;
	for(;mover>i;mover--)
	array[mover]=array[mover-1];
	strcpy(wordin,in);
	array[i]=wordin;
	(*num)++;
	
}

char *search(char **array, int count, char szBuff[])
{
    int b,a,c;
	char *p;
  	char *pts;
  
	for(b=0; b<count; b++)
	{
	    p=strstr(array[b],szBuff);
	
		if(p!=NULL)
		break;
		}
		
		if (p==NULL)
		{
			return szBuff;
		}
		pts=array[b];
		return pts;
		
	}


int loadArray(char *inFileName, char ***array, int *count, int *capacity)
{
    FILE *inFile;
    char word[WORD_LENGTH]; 

    if ((inFile = fopen(inFileName, "r")) == NULL)
    {
		fprintf(stderr,"Error opening input file, %s\n", inFileName);
		return -1;
    }

    *array = (char **)malloc(*capacity * sizeof(char*));
    if (*array == NULL)
    {
		fprintf(stderr, "Malloc of array in loadArray failed!\n");
		return -1;
    }

    printf("Reading file %s (each . is 1000 words read)\n", inFileName);

    *count = 0;
    while (fscanf(inFile, "%s", word) == 1)
    {
		if (*count >= *capacity)
		{
/* call a function that will double the size of the array and copy its contents */
			int m=0;
	int new_capacity=*capacity*2;
	char **new_array = (char **)malloc(new_capacity * sizeof(char*));
	while(m<*capacity)
	{
		new_array[m]=(*array)[m];
		m++;
	}
	free(*array);
	*array=new_array;
	*capacity=new_capacity;
	}
		if (insertWord(*array, count, word) != 0)
		{
		    fprintf(stderr,"    Insert returned an error!\n");
		    fclose(inFile);
		    return 1;
		}
		
		if (*count % 1000 == 0)
		{
		    printf(".");
		    fflush(stdout); 
		}
    }

    fclose(inFile);

    return 0;
}

int insertWord(char **array, int *count, char word[])
{
    char *wordPtr;

    wordPtr = (char *)malloc((strlen(word) + 1) * sizeof(char));
    if (wordPtr == NULL)
    {
		fprintf(stderr,"    Malloc of array[%d] failed!\n", *count);
		return -1;
    }
    
    strcpy(wordPtr, word);
    int ctr=0;
	for(;ctr<*count;ctr++)
		if(strcmp(array[ctr],wordPtr)>=0)
			break;
	int mover;
	for(mover=*count;mover>ctr;mover--)
		array[mover]=array[mover-1];
	array[ctr]=wordPtr;
    (*count)++;

    return 0;
}

⌨️ 快捷键说明

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