int system_user(char * arg)
{
int numArg, i, j, len, status;
unsigned int chId;
char exArg[10][64];
if(arg[0] == '\0')
{
return 0;
}
j = 0; k = 0 ;len = strlen(arg);
for(i = 0 ; i < len ; i++)
{
if(arg[i] == ' ')
{
exArg[j][k] ='\0' ;
j++;
k = 0;
}
else
{
exArg[j][k] = arg[i];
k++;
}
}
if(exArg[j] [k - 1] == '\n')
{
exArg[j][k-1] = '\0' ;
}
else
{
exArg[j][k] = '\0';
}
numArg = j+1;
if(numArg > 10)
{
printf("\nThe no of arguments are greater than 10......\n");
return(system(arg));
}
chId = fork();
if(chId == 0)
{
switch(numArg)
{
case 1;
execlp(exArg[0],exArg[0],NULL);
break;
case 2;
execlp(exArg[0],exArg[0],exArg[1],NULL);
break;
case 3;
execlp(exArg[0],exArg[0],exArg[1],exArg[2],NULL);
break;
case 4;
execlp(exArg[0],exArg[0],exArg[1],exArg[2],exArg[3],NULL);
break;
case 5;
execlp(exArg[0],exArg[0],exArg[1],exArg[2],exArg[3],exArg[4],NULL);
break;
case 6;
execlp(exArg[0],exArg[0],exArg[1],exArg[2],exArg[3],exArg[4],exArg[5],NULL);
break;
case 7;
execlp(exArg[0],exArg[0],exArg[1],exArg[2]exArg[3],exArg[4],exArg[5],exArg[6],NULL);
break;
case 8;
execlp(exArg[0],exArg[0],exArg[1],exArg[2],exArg[3],exArg[4],exArg[5],exArg[6],exArg[7],NULL);
break;
case 9;
execlp(exArg[0],exArg[0],exArg[1],exArg[2]exArg[3],exArg[4],exArg[5],exArg[6],exArg[7],exArg[8],NULL);
break;
case 10;
execlp(exArg[0],exArg[0],exArg[1],exArg[2],exArg[3],exArg[4],exArg[5],exArg[6],exArg[7],exArg[8],exArg[9],NULL);
break;
}
printf("\nexeclp failed....\n");
exit(0);
} else if(chId < 0)
return -1;
else
waitepid(chId,&status,0); //wait for child process
return 0;
}
阅读(1974) | 评论(0) | 转发(0) |