my_alias.c
/********************************************************************************* * 文件名称:my_alias.c * 功能描述:alias and unalias cmd * 维护记录:2009-3-25 V1.01 by skyily *********************************************************************************/ #include "my_alias.h" #include "main.h"
MY_ALIAS *alias_head;
/********************************************************************************* * 语法格式: void alias_init(void) * 实现功能: initialize alias environment * 入口参数: 无 * 出口参数: 无 *********************************************************************************/ void alias_init() { FILE *fp; char tmp_buf[1024] = {""}; char tmp_by[1024] = {""}; char tmp_ture[1024] = {""}; MY_ALIAS *node = NULL; MY_ALIAS *bp = NULL; int i = 0; int j = 0; if((fp = fopen("/home/skyily/temp/shellprograme/.aliasrc", "r")) == NULL) { perror("open .aliasrc error!"); return ; } while(fgets(tmp_buf, 1024, fp) != NULL) { if(strncmp(tmp_buf, "alias", 5) == 0) { node = (MY_ALIAS *)malloc(sizeof(MY_ALIAS)); i = 6; j = 0; while(tmp_buf[i] != '=') { tmp_by[j++] = tmp_buf[i++]; } i += 2; j = 0; while(tmp_buf[i] != '\'') { tmp_ture[j++] = tmp_buf[i++]; } // sscanf(tmp_buf, "alias %s='%s'\n", node->by_name, node->ture_name);
strcpy(node->by_name, tmp_by); strcpy(node->ture_name, tmp_ture); // printf("%s\n", tmp_by);
// printf("%s\n", tmp_ture);
memset(tmp_by, 0, 1024); memset(tmp_ture, 0, 1024); if(alias_head == NULL) bp = alias_head = node; else bp->next = node; bp = node; bp->next = NULL; node = NULL; } memset(tmp_buf, 0, 1024); } // close(fp);
return ; }
/********************************************************************************* * 语法格式: char *alias_check(char *buf, int c_size) * 实现功能: check alias in the alias environment * 入口参数: buf is cmd_buf, operate cmd_buf; c_size is size of cmd burch * 出口参数: alias_head *********************************************************************************/ char *alias_check(char *buf, int c_size) { MY_ALIAS *p = alias_head; char tmp_buf[CMD_SIZE] = {""}; int i = 0; //char *tmp_ar[CMD_BUNCH_SIZE];
if(argv[0] != NULL) { while(p != NULL) { if(strcmp(argv[0], p->by_name) == 0) { // printf("%s\n", p->ture_name); //test
// strcpy(argv[0], p->ture_name); //wrong...
argv[0] = NULL; strcpy(tmp_buf, p->ture_name); // printf("%s\n", argv[0]); //test
for(i = 1; i < c_size; i++) { // printf("%s\n", argv[i]); //test
strcat(tmp_buf, " "); strcat(tmp_buf, argv[i]); argv[i] = NULL; } // printf("%s\n", tmp_buf); //test
strcpy(buf, tmp_buf); //
return buf; // break;
} p = p->next; } } return NULL; }
/********************************************************************************* * 语法格式: void alias_display(void) * 实现功能: display alias environment * 入口参数: 无 * 出口参数: 无 *********************************************************************************/ void alias_display() { MY_ALIAS *p = alias_head; char tmp_buf[1024] = {""}; while(p != NULL) { sprintf(tmp_buf, "alias %s='%s'", p->by_name, p->ture_name); printf("%s\n", tmp_buf); memset(tmp_buf, 0, 1024); p = p->next; } return ; }
/********************************************************************************* * 语法格式: MY_ALIAS *alias_cmd(int c_size) * 实现功能: implement alias cmd * 入口参数: c_size is size of cmd burch * 出口参数: alias_head *********************************************************************************/ MY_ALIAS *alias_cmd(int c_size) { MY_ALIAS *tmp = alias_head; MY_ALIAS *p = NULL; MY_ALIAS *node = NULL; char tmp_buf[1024] = {""}; char tmp_by[1024] = {""}; char tmp_ture[1024] = {""}; char end[CMD_SIZE] = {""}; int signal = 0; int i = 0; int j = 0; char *ptr; if(argv[1] != NULL) { strcpy(tmp_buf, argv[1]); if(c_size > 2) { for(i = 2; i < c_size; i++) { strcat(tmp_buf, " "); strcat(tmp_buf, argv[i]); } } i = 0; if((ptr = strchr(tmp_buf, '=')) && (*ptr != tmp_buf[0])) { while(tmp_buf[i] != '=') { tmp_by[j++] = tmp_buf[i++]; } i++; if(tmp_buf[i] != '\0') { j = 0; if(tmp_buf[i] != '\'' ) //|| tmp_buf[i] == '\"')
{ printf("alias %s format is not right\n", tmp_buf); printf("example is ll=\'ls -l\'\n"); return alias_head; } i++; while(tmp_buf[i] != '\'') { if(tmp_buf[i] == '\0') { while(1) { printf("> "); fflush(stdout); fgets(end, CMD_SIZE, stdin); if((end != NULL) && (strcmp(end, "\'\n") == 0)) { break; } memset(end, 0, CMD_SIZE); } break; } tmp_ture[j++] = tmp_buf[i++]; } while(tmp != NULL) { p = tmp; if(strcmp(tmp_by, p->by_name) == 0) { signal = 1; break; } tmp = tmp->next; } if(signal == 1) { memset(p->ture_name, 0, 200); strcpy(p->ture_name, tmp_ture); //printf("%s\n", tmp_ture);
} else { node = (MY_ALIAS *)malloc(sizeof(MY_ALIAS)); node->next = alias_head; alias_head = node; strcpy(node->by_name, tmp_by); strcpy(node->ture_name, tmp_ture); node = NULL; } } else { printf("input error\n"); } } else { printf("alias %s not found\n", tmp_buf); } } return alias_head; }
/********************************************************************************* * 语法格式: void unalias_cmd(void) * 实现功能: delelt alias of environment * 入口参数: 无 * 出口参数: 无 *********************************************************************************/ void unalias_cmd() { MY_ALIAS *p = alias_head; MY_ALIAS *tmp = NULL; if(argv[1] != NULL) { if(strcmp(argv[1], "-a") == 0) { while(alias_head != NULL) { alias_head = alias_head->next; free(p); // p = NULL;
p = alias_head; } } else { //while(p != NULL)
{ while((p != NULL) && (strcmp(argv[1], p->by_name) != 0)) { tmp = p; p = p->next; } if((p != NULL) && (strcmp(argv[1], p->by_name) == 0)) { if(p == alias_head) { alias_head = p->next; free(p); p = NULL; } else { tmp->next = p->next; free(p); p = NULL; } } else { printf("unalias %s not found\n", argv[1]); } } } } return ; } 基本已经实现,希望大家多多指点
|