【问题标题】:Show the names of executable files by pressing TAB Linux C program按 T​​AB Linux C 程序显示可执行文件的名称
【发布时间】:2018-11-05 17:28:35
【问题描述】:

我要做一个程序,它在用户激活 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

【问题讨论】:

    标签: c linux recursion directory


    【解决方案1】:

    认为您选择的分隔符“/:”没有按预期标记 PATH 变量。我将其更改为“:”,并且正确分隔了路径中的每个条目:

    # 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 ...

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

    【讨论】:

      【解决方案2】:

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

      编辑:具体来说,您可以使用 strtok() 函数,该函数将字符串拆分为指定字符(在本例中为“:”)。您可以使用该函数将 PATH 拆分为单独的目录。

      编辑 2:确保在每个“:”之后清除“temp”的内容,因为程序当前正在合并 PATH 中的不同目录,不应合并。

      【讨论】:

      • 在 /home/user/bin 之后,输出总是相同的“没有这样的文件或目录”
      • 绝对确定您的程序正在删除冒号吗?另外,请确保您的程序有权打开其他目录。
      • 您应该确保 PATH 变量中的目录是分开的,而不是加在一起。查看答案的编辑。
      • 很高兴知道!请考虑将此答案也标记为解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      相关资源
      最近更新 更多