【原创】目录历史源码

CleanLi(黎涛) 发表于2015-11-05    类别: 技术(66)    标签: 原创(65)

自己写了一个记录访问目录历史的东西,code在这里备份一下。

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int main(int argc, char** argv)
    {
            FILE* fp = NULL;
            char s[1000], t[1000];
            char *pwd;
            char *home;
            int i = 0, j;
            home = getenv("HOME");
            memset(s, 0, 1000);
            sprintf(s, "%s/.dhs/dhsty", home);
            fp = fopen(s, "r");
            if(fp){
                    while(!feof(fp)){
                            memset(s, 0, 1000);
                            fgets(s, 1000, fp);
                            s[strlen(s)-1] = 0;
                            printf("%d--%s ", i++, s);
                            //printf("%x == ", s[strlen(s)-1]);
                    }
                    fclose(fp);
                    fp = NULL;
            }
            if(i==0){
                    printf("no rec ");
                    return 0;
            }
            printf("input:");
            scanf("%d", &j);
            printf("get %d ", j);
            i = 0;
            memset(s, 0, 1000);
            sprintf(s, "%s/.dhs/dhsty", home);
            fp = fopen(s, "r");
            if(fp){
                    while(!feof(fp)){
                            fgets(s, 1000, fp);
                            if(s[strlen(s)-1] == 0x0a){
                                    s[strlen(s)-1] = 0;
                            }
                            if(i==j){
                                    printf("%d--%s ", i++, s);
                                    break;
                            }
                            i++;
                            //printf("%x == ", s[strlen(s)-1]);
                    }
                    fclose(fp);
                    fp = NULL;
            }
            memset(t, 0, 1000);
            sprintf(t, "%s/.dhs/wantedpath", home);
            fp = fopen(t, "w");
            if(fp){
                    fputs(s, fp);
                    fclose(fp);
                    fp = NULL;
            }
            return 0;
    }


    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int main(int argc, char** argv)
    {
            FILE* fp = NULL;
            char s[1000];
            char *pwd;
            char *home;     
                            
            home = getenv("HOME");
            memset(s, 0, 1000);
            sprintf(s, "%s/.dhs/dhsty", home);
            fp = fopen(s, "r");
            //fp = fopen("/home/clean/.dhs/dhsty", "r");
            //fp = fopen("dhsty", "a");
            pwd = getenv("PWD");
            printf("%s", pwd);
            if(fp){
                    while(!feof(fp)){
                            fgets(s, 1000, fp);
                            //printf("%d-- %s ", (int)strlen(s), s);
                            //printf("%x == ", s[strlen(s)-1]);
                            s[strlen(s)-1] = 0;
                            if(!strcmp(s, pwd)){
                                    printf("-same exist ");
                                    fclose(fp);
                                    return 0;
                            }
                    }
                    fclose(fp);
            }               
            memset(s, 0, 1000);     
            sprintf(s, "%s/.dhs/dhsty", home);
            fp = fopen(s, "a");
            if(!fp){                
                    printf("open fail ");
                    return -1;
            }               
            /*
            strcpy(s, "cd ");
            */
            fputs(pwd, fp);
            fputs(" ", fp);
            printf("-added ");
            /*
            if(argc > 1){ 
                    strcpy(s+3, argv[1]);
                    printf("%s ", s);
                    system(s);
            }
            */
            fclose(fp);
            return 0;
    }

相关文章

   2024-10-01 【记录】Fork Github网站仓库后续设置
   2023-12-31 【记录】浮点数存储方式
   2023-10-29 【记录】Windows下试用raid1
   2023-10-03 【记录】电脑网卡唤醒开机
   2023-08-12 【原创】以太网接口电路驱动调试总结