按TAB Linux C程序显示可执行文件的名称

时间:2018-05-26 14:57:57

标签: c linux recursion directory

我必须做一个程序,用户在激活TAB时会搜索当前目录和连续目录中的所有可执行文件(如果当前目录包含目录)。 可执行文件存储在动态矩阵中,以便稍后与用户输入进行比较

我的问题是搜索在PATH中开始,并且由于某种原因,无论何时尝试打开目录,它都会给我并且错误地说“没有这样的文件或目录”。

#include "header.h"
#include <dirent.h>

char **files = NULL;
char size = 0;
char heapsize = 0;

char **tabActivation(const char *text, int start, int end);
char *filesCatch(const char *text, int state);

int main(int argc, const char *argv[])
{

    char *line;
    rl_attempted_completion_function = tabActivation;

    while (1)
    {
        line = readline("msh$ ");

        if (line == NULL)
        {
            perror("malloc error!\n");
            exit(1);
        }
        if (!strcmp(line, "exit")) exit(0);
        if (strlen(line) == 0)continue;

        add_history(line);
        CMD *root = parse_line(line);

        print_command_list(root);
        free_command_list(root);
        free(line);
    }
}

char **tabActivation(const char *text, int start, int end)
{
    rl_attempted_completion_over = 1;
    return rl_completion_matches(text, filesCatch);
}

char *filesCatch(const char *text, int state)
{
    char pwd[1024] = "", *p=NULL, *limit="/:",temp[1024]="";
    char oldpwd[1024] = "";


    files = (char **) malloc(10 * sizeof(char *));
    heapsize = 10;

    strcpy(pwd, getenv("PATH"));
    strcpy(oldpwd,pwd);


    DIR *dir;
    struct dirent *entry;

    p = strtok(pwd,limit);
    strcat(temp,"/");
    strcat(temp,p);

    while(p!=NULL)
    {
        printf("CURRENT DIRECTORY= %s\n",temp);

        if ((dir = opendir(temp)) == NULL)
            perror("opendir() error");
        else
        {
            puts("contents of root:");
            while ((entry = readdir(dir)) != NULL)
                printf("  %s\n", entry->d_name);
            closedir(dir);
        }

        p = strtok(NULL,limit);
        strcat(temp,"/");
        strcat(temp,p);

        printf("\n");
        printf("\n");
    }
    return NULL;
}

pwd(PATH)输出:/ home / user / bin:/ usr / sbin:/ sbin:/ usr / local / bin:/ usr / bin:/ bin:/ usr / local / games:/ usr / games

节目输出:

CURRENT DIRECTORY= /home
Contents of DIRECTORY:

  .
  ..
  user


CURRENT DIRECTORY= /home/user
Contents of DIRECTORY:

  .icons
  .dmrc
  worksheet8
  .thumbnails
  Templates
  .bashrc
  file
  .calendar
  .themes
  up-down-mutex.c
  Pictures
  ls2.txt
  main.c
  exerc1
  teste.txt
  .
  Public
  Downloads
  7-sigchld.c
  worksheet7
  project
  tmp
  Music
  run
  .Xresources
  .vboxclient-draganddrop.pid
  worksheet6
  .bash_logout
  .dbus
  Documents
  bin
  teste
  .vboxclient-clipboard.pid
  .vboxclient-seamless.pid
  .bash_aliases
  .config
  .xsessionrc
  worksheet3
  worksheet5
  .pbuilderrc
  .gtkrc-2.0.mine
  .profile
  .profile~2018-02-19T13:24:58~
  .Xauthority
  teste.c
  ls.txt
  .conkyrc
  .gconf
  .gtk-bookmarks
  .bash_history
  .xsession-errors
  .gtkrc-2.0
  worksheet1
  .vboxclient-display.pid
  .local
  worksheet4
  .lesshst
  Videos
  .cache
  .fonts
  .gmrunrc
  ..
  project.zip
  .xsession-errors.old
  worksheet2
  .mozilla
  .gksu.lock


CURRENT DIRECTORY= /home/user/bin
Contents of DIRECTORY:

  .
  ..


CURRENT DIRECTORY= /home/user/bin/usr
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin/sbin
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin/sbin/usr
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin/sbin/usr/local
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin/sbin/usr/local/bin
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin/sbin/usr/local/bin/usr
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin/sbin/usr/local/bin/usr/bin
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin/sbin/usr/local/bin/usr/bin/bin
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin/sbin/usr/local/bin/usr/bin/bin/usr
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin/sbin/usr/local/bin/usr/bin/bin/usr/local
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin/sbin/usr/local/bin/usr/bin/bin/usr/local/games
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin/sbin/usr/local/bin/usr/bin/bin/usr/local/games/usr
opendir() error: No such file or directory


CURRENT DIRECTORY= /home/user/bin/usr/sbin/sbin/usr/local/bin/usr/bin/bin/usr/local/games/usr/games
opendir() error: No such file or directory
Segmentation fault

2 个答案:

答案 0 :(得分:2)

PATH包含多个目录。确保将变量拆分为多个路径,并分别搜索每个路径。

编辑:具体来说,你可以使用strtok()函数,它将字符串拆分为指定的字符(&#34;:&#34;在这种情况下)。您可以使用该函数将PATH拆分为单独的目录。

编辑2:确保清除&#34; temp&#34;的内容在每个&#34;:&#34;之后,因为程序当前正在合并PATH中的不同目录,这些目录不应该合并。

答案 1 :(得分:0)

思考定界符&#34; /:&#34;你选择了没有按预期方式对PATH变量进行标记。我将其更改为&#34;:&#34;并且我正确地将路径中的每个条目分开:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>

int main (int argc, char **argv)
{
    char pwd[4096]="", *limit=":";
    char *p = NULL;
    strcpy (pwd, getenv("PATH"));

    p = strtok (pwd, limit);
    printf ("%s\n", p);

    while (p != NULL){
        p = strtok(NULL,limit);
        printf ("%s\n", p); 
        }
return (0); 
}
  

输出:/ usr / local / sbin / usr / local / bin / usr / sbin ...

     

不确定这是不是你想要的。