33,317
社区成员
发帖
与我相关
我的任务
分享
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define MAX_ARG 10
struct command_struct
{
char *name;
char *argv[MAX_ARG];
};
int split(char *buf,struct command_struct *com,int *num);
int main(void)
{
struct command_struct com;
char buf[256];
int num;
printf("\n[myftp]$");
memset(buf, '\0', sizeof(buf));/*init buf*/
if(NULL==fgets(buf,256,stdin))
{
perror("fgets error");
exit (1);
}
split(buf,&com,&num);
return 0;
}
int split(char *buf,struct command_struct *com,int *num)
{
char array[256];
int flag=0;
int i=0,n=0,t=0;
memset(array,'\0',sizeof(array));
for(n=0;n<strlen(buf);n++)
{
if(buf[n]!=' '&&buf[n]!='\t'&&buf[n]!='\n')
{
array[i++]=buf[n];
flag=1;/*在单词中*/
}
else if(flag=1&&(buf[n]==' '||buf[n]=='\t'||buf[n]=='\n'))
{
array[i]='\0';
strcpy(com->argv[t],array);
printf("%s ",com->argv[t]);
t++;
if(t>9)
{
exit(1);
}
memset(array,'\0',sizeof(array));
i=0;
flag=0;/*不在单词中*/
}
else
{
;
}
}
com->name=com->argv[0];
return 0;
}
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define MAX_ARG 10
struct command_struct
{
char *name;
char *argv[MAX_ARG];
};
int split(char *buf,struct command_struct *com,int *num);
int main(void)
{
struct command_struct com;
char buf[256];
int num;
int i=0;
printf("\n[myftp]$");
memset(buf, '\0', sizeof(buf));/*init buf*/
if(NULL==fgets(buf,256,stdin))
{
perror("fgets error");
exit (1);
}
split(buf,&com,&num);
//printf("\n%s %s %s %s",com.argv[0],com.argv[1],com.argv[2],com.argv[3]);
printf("\n%d",num);
for(;num>=0;num--)
{
printf("%s ",com.argv[i++]);
}
return 0;
}
int split(char *buf,struct command_struct *com,int *num)
{
char *array=NULL;
int flag=0;
int i=0,n=0,t=0;
int len=strlen(buf);
if(len<=1)
{
printf("The first arg can't only '\\n'!");
exit (1);
}
for(n=0;n<len;n++)
{
if(buf[n]==' '||buf[n]=='\t'||buf[n]=='\n')
{
buf[n]='\0';
flag=0;
}
if(buf[n]!='\0' && flag==0)
{
if(t<=9)
{
com->argv[t]=&buf[n];
flag=1;
t++;
}
else
{
printf("argument too much!\n");
exit (1);
}
}
}
*num=t-1;
com->name=com->argv[0];
return 0;
}