int ip_to_q_name(char*ip,int len,char*qname) { assert(ip); assert(qname); //hyb_debug("origin:%s\n",ip); int ptr_cnt = 0; int count = 0; int i = 0; char *delims = "."; char *myStrBuf=NULL; char *ips[20]; char *p =NULL; while(p = strtok_r(ip,delims,&myStrBuf)) { ip = NULL; ips[count] = p; count ++; } for(i = count-1;i>=0;i--) { int size = strlen(ips[i]); //hyb_debug("ip:%s size:%d\n",p,size); memcpy(qname+ptr_cnt,ips[i],size); ptr_cnt += size; memcpy(qname+ptr_cnt,delims,1); ptr_cnt += 1; } //memcpy_s(qname,ip,len); strcat_n(qname,256,"in-addr.arpa"); //hyb_debug("New ipptr is :%s\n",qname); return strlen(qname); }
int main(int argc, char *argv[]) { setProgramStatus(EDITOR); if (argc == 1) { printf(1, "please input the command as [editor file_name]\n"); setProgramStatus(SHELL); exit(); } //存放文件内容 char *text[MAX_LINE_NUMBER] = {}; text[0] = malloc(MAX_LINE_LENGTH); memset(text[0], 0, MAX_LINE_LENGTH); //存储当前最大的行号,从0开始。即若line_number == x,则从text[0]到text[x]可用 int line_number = 0; //尝试打开文件 int fd = open(argv[1], O_RDONLY); //如果文件存在,则打开并读取里面的内容 if (fd != -1) { char buf[BUF_SIZE] = {}; int len = 0; while ((len = read(fd, buf, BUF_SIZE)) > 0) { int i = 0; int next = 0; int is_full = 0; while (i < len) { //拷贝"\n"之前的内容 for (i = next; i < len && buf[i] != '\n'; i++) ; strcat_n(text[line_number], buf+next, i-next); //必要时新建一行 if (i < len && buf[i] == '\n') { if (line_number >= MAX_LINE_NUMBER - 1) is_full = 1; else { line_number++; text[line_number] = malloc(MAX_LINE_LENGTH); memset(text[line_number], 0, MAX_LINE_LENGTH); } } if (is_full == 1 || i >= len - 1) break; else next = i + 1; } if (is_full == 1) break; } close(fd); } //输出文件内容 show_text(text); //输出帮助 com_help(text); //处理命令 char input[MAX_LINE_LENGTH] = {}; while (1) { printf(1, "\nplease input command:\n"); memset(input, 0, MAX_LINE_LENGTH); gets(input, MAX_LINE_LENGTH); int len = strlen(input); input[len-1] = '\0'; len --; //寻找命令中第一个空格 int pos = MAX_LINE_LENGTH - 1; int j = 0; for (; j < 8; j++) { if (input[j] == ' ') { pos = j + 1; break; } } //ins if (input[0] == 'i' && input[1] == 'n' && input[2] == 's') { if (input[3] == '-') com_ins(text, atoi(&input[4]), &input[pos]); else com_ins(text, line_number+1, &input[pos]); //插入操作需要更新行号 line_number = get_line_number(text); } //mod else if (input[0] == 'm' && input[1] == 'o' && input[2] == 'd') { if (input[3] == '-') com_mod(text, atoi(&input[4]), &input[pos]); else com_mod(text, line_number + 1, &input[pos]); } //del else if (input[0] == 'd' && input[1] == 'e' && input[2] == 'l') { if (input[3] == '-') com_del(text, atoi(&input[4])); else com_del(text, line_number + 1); //删除操作需要更新行号 line_number = get_line_number(text); } else if (strcmp(input, "show") == 0) { auto_show = 1; printf(1, "enable show current contents after text changed.\n"); } else if (strcmp(input, "hide") == 0) { auto_show = 0; printf(1, "disable show current contents after text changed.\n"); } else if (strcmp(input, "help") == 0) com_help(text); else if (strcmp(input, "save") == 0 || strcmp(input, "CTRL+S\n") == 0) com_save(text, argv[1]); else if (strcmp(input, "exit") == 0) com_exit(text, argv[1]); else { printf(1, "invalid command.\n"); com_help(text); } } setProgramStatus(SHELL); exit(); }