gpt4 book ai didi

c - shell 基名错误

转载 作者:行者123 更新时间:2023-11-30 18:04:58 25 4
gpt4 key购买 nike

我也尝试从基本名称获取命令。这就是为什么我在解析该行后使用类似的东西的原因...

 while (*line == ' ' || *line == '\n') 
*line++ = '\0';

line = basename(line); // base
*argv++ = line;

好吧,basename 似乎运行良好,但如果我输入像 ls -l 这样的命令,则会出现错误...

输入输出和输出是这样的

ls
arg[0]: 'ls' and arg[1]: '(null)'
shellTest

bin/ls
arg[0]: 'ls' and arg[1]: '(null)'
shellTest

ls -l
arg[0]: '-l' and arg[1]: '-l'
Execution of the command is failed
: No such file or directory

如果我删除 line = basename(line); 那么除了基本名称之外一切都正常,所以解析不会成为问题......

如何解决这个问题?

最佳答案

问题可能出在使用 basename() 的周围代码中。 basename() 的 POSIX 标准说:

DESCRIPTION

The basename() function shall take the pathname pointed to by path and return a pointer to the final component of the pathname, deleting any trailing '/' characters.

If the string pointed to by path consists entirely of the '/' character, basename() shall return a pointer to the string "/". If the string pointed to by path is exactly "//", it is implementation-defined whether '/' or "//" is returned.

If path is a null pointer or points to an empty string, basename() shall return a pointer to the string ".".

The basename() function may modify the string pointed to by path, and may return a pointer to static storage that may then be overwritten by a subsequent call to basename().

The basename() function need not be thread-safe.

RETURN VALUE

The basename() function shall return a pointer to the final component of path.

该规范中有很大的回旋余地。

但看起来有点像您试图使用同一空间同时存储两个值,但没有成功。

关于c - shell 基名错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7170150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com