Chinaunix首页 | 论坛 | 博客

  • 博客访问: 11944
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 81
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-09 22:57
个人简介

高中学历的渣渣

文章分类
文章存档

2015年(8)

我的朋友

分类: LINUX

2015-05-26 23:05:37

随手写的,对中文路径的支持不是很好

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <fcntl.h>
  5. #include <sys/types.h>
  6. #include <dirent.h>
  7. #include <termios.h>
  8. #include <string.h>
  9. #include <unistd.h>

  10. #define MAX_LENGTH              100
  11. #define KEY_BACKSPACE2          ((char)127)
  12. #define KEY_TAB              ((char)9)
  13. #define KEY_ENTRY               ((char)10)

  14. char path[MAX_LENGTH + 1];
  15. int current_i = 0;
  16. struct termios i,n;

  17. int find_index(char *base,char c){
  18.     
  19.     int i = 0;
  20.     char *t = base;        
  21.     while(*t) if (*t++ == c) i = t - base;
  22.     return i - 1;
  23. }

  24. void auto_completion(void){
  25.     
  26.     DIR * dir;
  27.     struct dirent *ent,*t_ent;
  28.     char *t;
  29.     char t2[MAX_LENGTH];
  30.     char *base = path;
  31.     int l = 0;
  32.     int i = 0;
  33.     int name_length;
  34.     int o;

  35. start:
  36.     i = find_index(base,'/');
  37.     t = &base[i];
  38.     while(*t) t++; //
  39.     if (path[0] != '/') i = -1;

  40.     switch(i){

  41.         case -1:

  42.             getcwd(t2,MAX_LENGTH);
  43.             o = strlen(t2);

  44.             if (o + current_i > MAX_LENGTH) return;
  45.             
  46.             for (l = 0; l < o ; l++){
  47.             
  48.                 path[o + l + 1] = path[l];
  49.                 path[l] = t2[l];
  50.             }
  51.             path[l] = '/';

  52.             for (l = 0; l < current_i; l++)
  53.                 putchar('\b');
  54.             
  55.             current_i += o + 1;
  56.             printf("%s",path);
  57.             l = o = 0;
  58.             goto start;

  59.         break;
  60.         case 0:
  61.             if ((dir = opendir("/")) == NULL) return;
  62.             base++;        
  63.         break;
  64.         default:
  65.             base[i] = '\0';        
  66.             if ((dir = opendir(base)) == NULL) return;
  67.             base[i] = '/';
  68.             base = &base[i+1];
  69.         break;
  70.     }

  71.     name_length = t - base;

  72.     while((ent = readdir(dir)) && (l < 2)){

  73.         if ((ent->d_type == 4) &&
  74.             (*ent->d_name != '.') &&
  75.             (strncmp(base,ent->d_name,name_length) == 0)){
  76.             
  77.             t_ent = ent;
  78.             l++;
  79.         }
  80.     }

  81.     if (l == 1){        

  82.         t = t_ent->d_name;
  83.         for (i = 0; i < name_length ; i++){

  84.             current_i--;
  85.             if (path[current_i] == '/') {
  86.                 current_i++;
  87.                 break;
  88.             };
  89.             putchar('\b');
  90.         }

  91.         for (i = 0; current_i < MAX_LENGTH && *t;i++){
  92.             
  93.             base[i] = *t++;
  94.             current_i++;    
  95.             putchar(base[i]);
  96.         }
  97.         base[i] = '/';
  98.         current_i++;    
  99.         putchar(base[i]);
  100.     }

  101.     closedir(dir);
  102. }

  103. void get_line(void){
  104.     
  105.     int i = 0;
  106.     char c;

  107.     memset(path,0,MAX_LENGTH);
  108.     current_i = 0;

  109.     while (1){

  110.         c = getchar();

  111.         if (c == KEY_ENTRY) return ; //回车键
  112.         if (current_i >= MAX_LENGTH) continue; //超出最大限制数
  113.         if (c == KEY_TAB) {    //TAB键,那么自动补全

  114.             if (!path[0]) continue;
  115.             auto_completion();
  116.             continue;
  117.         }
  118.         if (c == KEY_BACKSPACE2){

  119.             if (current_i == 0) continue;
  120.             path[current_i--] = '\0';
  121.             printf("\b \b");
  122.             continue;        
  123.         }    
  124.         path[current_i++] = c;
  125.         putchar(c);
  126.     }
  127. }

  128. int main(void){

  129.     char c;
  130.     DIR *dir;

  131.     tcgetattr(fileno(stdin),&i);
  132.     n = i;
  133.     n.c_lflag &= ~ECHO;
  134.     n.c_lflag &= ~ICANON;

  135.     if (tcsetattr(fileno(stdin),TCSAFLUSH,&n) != 0){

  136.         printf("tcsetattr");
  137.     }else {

  138.         printf(": ");
  139.         get_line();
  140.         printf("\nYou: %s\n",path);
  141.     }

  142.     tcsetattr(fileno(stdin),TCSANOW,&i);
  143.     return 0;
  144. }


阅读(782) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~