2016-09-19 12 views
1

これは私が作成しているシェルの一部です。私はlibreadlineを使用していくつかの問題を抱えているので、シェルがロードされ、私はディレクトリにアクセスするが、別のプロンプトが表示される前にいくつかの奇妙な出力が表示されるので、オートコンプリート(従ってTABを押す) 。私は、ディレクトリの名前が大文字で始まる場合にのみ、これが起こることに気付きました。大文字のディレクトリを完成させるときのlibreadlineの問題

例: "ユーザー::〜%のCD Githubの" < - オートコンプリートするタブを押し書かGitHubの

次のプロンプトがある:私は本当に理由を理解することはできません "の8b /ユーザー::のGitHub%" 、これは私にとって本当に奇妙なことです。

#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; 
} 

答えて

0

あなたは非\0終端文字列でstrcatを使用することはできません。

char *prompt = malloc(sizeof(char) * LINE_BUF); 
char *current, buffer[TOK_BUF]; 
current = getcwd(buffer, TOK_BUF); 

strcat(prompt, get_user()); 

利用strcpy代わりのstrcat、またはその代わりmalloc

+1

おかげで男のcallocが、私はそれを知りませんでした。 –

関連する問題