strtok 只返回一个 token
我正在编写一个简单的 shell,它接受一些标准命令,例如 C 中的 cd 和 ls。我正在尝试实现一个功能,用户可以输入“;”位于命令之间,以便可以将一堆命令写在同一行并单独执行。因此,如果我输入“cd Desktop; ls”,shell 应该 cd 到 Desktop 并打印目录中的内容。问题是它只执行第一个命令。这是我的主要方法:
char input[1024];
while(1)
{
printf("%s ", prompt);
fgets(input, 1024, stdin);
char delims[] = ";";
char *result = NULL;
result = strtok( input, delims );
while( result != NULL )
{
printf("%s\n", result);
char * copy = malloc(strlen(result) + 1); //Create a copy of the input token
strcpy(copy, result);
format(copy);
if(programs)
{
handle();
cleanup(programs);
programs = NULL;
}
free(copy);
result = strtok( NULL, delims );
cmdno++;
}
}
首先,我尝试根据“;”将输入分解为标记。然后将令牌提供给 format() 方法,如下所示:
int format(char input[])
{
input = strtok(input, "\n");
...
}
我知道 strtok 会对原始字符串进行更改,这就是为什么我在将令牌传递给 format 之前首先创建令牌的副本。我正在做的事情正确吗?
I'm writing a simple shell that accepts some standard commands like cd and ls in C. I'm trying to implement a feature where the user can enter a ";" in between commands so that a bunch of commands can be written on the same line and be executed separately. So if I input "cd Desktop; ls" the shell should cd to Desktop and print the what's in the directory. The problem is it only executes the first command. Here's my main method:
char input[1024];
while(1)
{
printf("%s ", prompt);
fgets(input, 1024, stdin);
char delims[] = ";";
char *result = NULL;
result = strtok( input, delims );
while( result != NULL )
{
printf("%s\n", result);
char * copy = malloc(strlen(result) + 1); //Create a copy of the input token
strcpy(copy, result);
format(copy);
if(programs)
{
handle();
cleanup(programs);
programs = NULL;
}
free(copy);
result = strtok( NULL, delims );
cmdno++;
}
}
First I try to break up the input into tokens based on ";" and then feed the token to the format() method which looks like this:
int format(char input[])
{
input = strtok(input, "\n");
...
}
I know that strtok makes changes to the original string, which is why I create a copy of the token first before passing it to format. Is what I'm doing correct??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能混合多个
strtok
调用。发生的事情是这样的:input
,以便strtok
记录并在内部存储内容input
中休息一下>copy
,因此strtok
再次记录下来,从而破坏了之前的信息strtok
> 只了解复制
业务,对原始输入
一无所知。主要问题是
strtok
不知道您正在同时做两件事。从它的角度来看,您只是在完成第一个字符串之前开始处理不同的字符串。可能的解决方案:
strtok_r
(如果有)。它不是标准 C(但它是标准 POSIX)。r
代表可重入copy
input
关于最后一点:
char *
数组并用strtok
填充它,而无需暂停来拆分子令牌。因此每个元素应该是不同的命令";"
分割后,开始处理每个数组元素You can't mix multiple
strtok
calls. Here's what's happening:input
sostrtok
takes note and stores stuff internallyinput
copy
so againstrtok
takes note, thereby destroying the previous infostrtok
only knows about thecopy
business and doesn't know anything about the originalinput
.The main problem is that
strtok
doesn't know that you're doing two things at the same time. From its point of view, you simply started processing a different string before finishing the first string.Possible solutions:
strtok_r
if you have it. It's not standard C (but it is standard POSIX). Ther
stands for reentrantcopy
before finishing withinput
About that last point:
char *
and fill it withstrtok
without pausing to split sub-tokens. So each element should be a different command";"
split, start processing each of the array elements这个怎么样:
What about this: