有些代码看不懂
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define oops(m) do{perror(m); exit(1);}while(0)
int main()
{
char buf[32];
int pid = 0;
int num = 0;
int i = 0;
int flag = 0;
char * p = NULL;
while (1)
{
flag = 0;
i = 0;
num = 0;
printf("Please input a cmd: ");
fgets(buf, 32, stdin);
if (strncmp(buf, "end", 3) == 0)
break;
p = strstr(buf, "\n");
if (p != NULL)
*p = '\0';
while (buf[i] != '\0')
{
if (buf[i] == ' ' && buf[i+1] != ' ')
{
num++;//这里不懂
flag = 1;//还有这里,所谓的标记,是什么意思
}
else
flag = 0;//???
i++;
}
char* argv[num+2];// 为什么又是+2
argv[num+1] = NULL;
p = buf;
for (i = 0; i < num+1; i++)
{
argv[i] = p;
p = strstr(p, " ");
if (p == NULL)
break;
while (*p == ' ' && *(p+1) == ' ')
{
*p = '\0';
p++;
}
*p = '\0';
p++;
}
pid = fork();
if (pid == -1)
{
oops("fork");
}
else if (pid == 0)
{
execvp(argv[0], argv);
perror("execvp");
}
wait(NULL);
}
return 0;
}
其他的都还懂