<分区>
Possible Duplicate:
Parse string into argv/argc
我正在尝试使用 C 编写一个伪造的 shell,因此我需要能够接受一个命令,然后是该命令的 x 个参数。当我实际运行命令时,我只是使用 execvp(),所以我只需要将参数解析为一个数组。但我不确定如何在不知道确切数字的情况下执行此操作。我在想一些伪代码,比如:
while (current char in command line != '\n')
if current char is a space, increment a counter
parse characters in command line until first space and save into a command variable
for number of spaces
while next char in command line != a space
parse chars into a string in an array of all of the arguments
关于如何将其放入代码中有什么建议吗?