首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >完成大写目录时的libreadline问题

完成大写目录时的libreadline问题
EN

Stack Overflow用户
提问于 2016-09-20 02:54:50
回答 1查看 40关注 0票数 1

这是我正在创建的一个外壳的一部分。我在使用libreadline时遇到了一些问题,因为当shell加载时,我尝试使用自动完成(所以按TAB)进入一个目录,在我按enter键之后,我访问了该目录,但在打印另一个提示符之前,我得到了一些奇怪的输出。我注意到,只有当目录名称以大写字母开头时,才会发生这种情况。

示例:"user::~% cd Github“<--按tab键自动完成Github

下一个提示是:"8b�/�user ::GitHub%“我真的不明白为什么,这对我来说真的很奇怪。

代码语言:javascript
复制
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <signal.h>
#include <readline/history.h>
#include <readline/readline.h>
#include "flush.h"
#include "env.h"
#include "fget.h"

char * flush_builtins[] =
{
  "cd",
  "help",
  "exit",
  "ver",
  "fget"
};

int flush_num_builtins() {
  return sizeof(flush_builtins) / sizeof(char *);
}

int (*flush_func[]) (char **) =
{
  &flush_cd,
  &help,
  &exit_flush,
  &ver,
  &fget
};

static int flush_startp(char **args)
{
  pid_t pid;
  int status;
  pid = fork();
  if (pid == 0)
  {
    if (execvp(args[0], args) == -1)
    {
      fprintf(stderr, "flush: command not found\n");
    }
    exit(1);
  }
  else if (pid < 0)
  {
    fprintf(stderr, "flush: command not found\n");
  }
  else
  {
    do
    {
      waitpid(pid, &status, WUNTRACED);
    } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  }

  return 1;
}

static int flush_exec(char **args)
{
  int i;

  if (args[0] == NULL)
  {
    return 1;
  }

  for (i = 0; i < flush_num_builtins(); i++)
  {
    if (strcmp(args[0], flush_builtins[i]) == 0) {
      return (*flush_func[i])(args);
    }
  }

  return flush_startp(args);
}

static char * flush_read(void)
{
  fflush(stdout);
  char *line_read = malloc(sizeof(char) * LINE_BUF);
  char *prompt = malloc(sizeof(char) * LINE_BUF);
  char *current, buffer[TOK_BUF];
  current = getcwd(buffer, TOK_BUF);

  strcat(prompt, get_user());
  strcat(prompt, " :: ");

  if (strcmp(current, get_home()) == 0)
  {
    strcat(prompt, "~");
  }

  else
  {
    strcat(prompt, get_cwd());
  }

  strcat(prompt, " % ");
  line_read = readline(prompt);

  if (line_read && *line_read)
  {
    add_history(line_read);
  }

  return line_read;
  free(prompt);
  free(line_read);
  free(current);
}

static char **flush_getargs(char * line)
{
  int bufsize = TOK_BUF;
  int i = 0;
  char **tokens = malloc(bufsize * sizeof(char *));
  char **token;

  if (!tokens)
  {
    fprintf(stderr, "allocation error\n");
    exit(1);
  }

  token = strtok(line, DELIM);
  while (token != NULL)
  {
    tokens[i] = token;
    i++;
    token = strtok(NULL, DELIM);
  }

  tokens[i] = NULL;
  return tokens;
}


static void flush_loop(void)
{
  char *line;
  char **args;
  int  status;

  do
  {
    line = flush_read();
    args = flush_getargs(line);
    status = flush_exec(args);
    free(line);
    free(args);
  } while (status);
}

static void handler(int num)
{
  signal(SIGINT, handler);
  flush_loop();
  fflush(stdout);
}

int main()
{
  init();
  signal(SIGINT, handler);
  flush_loop();
  return 0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-20 03:07:47

不能将strcat与非\0终止的字符串一起使用:

代码语言:javascript
复制
char *prompt = malloc(sizeof(char) * LINE_BUF);
char *current, buffer[TOK_BUF];
current = getcwd(buffer, TOK_BUF);

strcat(prompt, get_user());

使用strcpy代替strcat,或者使用calloc代替malloc

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39580196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档