嗨,我正在用 C 语言编写一个用于赋值的 shell,但我不确定如何在函数 count() 中计算和返回缓冲区中的参数数量。这就是我到目前为止所拥有的。提前致谢。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int count(char* buffer)
{
int count=0;
//COUNT ARGS WITHIN BUFFER HERE
return count;
}
int main(int argc, char **argv)
{
//buffer is to hold the commands that the user will type in
char buffer[512];
// /bin/program_name is the arguments to pass to execv
//if we want to run ls, "/bin/ls" is required to be passed to execv()
char* path = "/bin/";
while(1)
{
//print the prompt
printf("myShell>");
//get input
fgets(buffer, 512, stdin);
//fork!
int pid = fork();
//Error checking to see if fork works
//If pid !=0 then it's the parent
if(pid!=0)
{
wait(NULL);
}
else
{
int no_of_args = count(buffer);
//we plus one so that we can make it NULl
char** array_of_strings = malloc((sizeof(char*)*(no_of_args+1)));