|
i need to make program that i give some system command like ls,date et cetera.and to check if thre is path that include this command(file).i have variable commandandparameters that start to change in the last loop while and i dont know why.
i put puts(commandandparameters); to show that the output is not good if you want to run it.
#include <stdio.h><br />
#include <string.h><br />
#include <stdlib.h><br />
void main()<br />
{<br />
char *arr[5];
char command[10];<br />
int i,j;<br />
char *path=NULL,*tempath,*finalpath,*commandandparameters;<br />
do<br />
{<br />
i=0;<br />
printf("enter new command:");<br />
gets(command);<br />
arr[i]=strtok(command," ");<br />
while(arr[i]!=NULL)
{<br />
i++;<br />
arr[i]=strtok(NULL," ");<br />
}<br />
strcpy(commandandparameters,arr[0]);
for(j=1;j<i;j++)
{<br />
strcat(commandandparameters," ");<br />
strcat(commandandparameters,arr[j]);<br />
}<br />
path = getenv("PATH");<br />
tempath = strtok(path,":");<br />
while (tempath != NULL)<br />
{<br />
strcpy(finalpath,tempath);
puts(commandandparameters);<br />
strcat(finalpath,"/");
execl(finalpath,commandandparameters,NULL);<br />
tempath = strtok(NULL, ":");
}<br />
}while(command!="leave");<br />
}
|
|
|
|
|
Some code is missing in your post.
(And please, elaborate a bit).
Veni, vidi, vici.
|
|
|
|
|
Your question is not clear, and your code snippet is incomplete. However I did notice the line:
strcpy(commandandparameters,arr[0]);
This is likely to fail since you have not initialised commandandparameters to point to anything.
Note, please use <pre> tags around your code (as I have done above), rather than <code> tags.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char *arr[5]; char command[10];
int i,j;
char *path=NULL,*tempath,*finalpath,*commandandparameters;
do
{
i=0;
printf("enter new command:");
gets(command);
arr[i]=strtok(command," ");
while(arr[i]!=NULL) {
i++;
arr[i]=strtok(NULL," ");
}
strcpy(commandandparameters,arr[0]); for(j=1;j<i;j++) {
strcat(commandandparameters," ");
strcat(commandandparameters,arr[j]);
}
path = getenv("PATH");
tempath = strtok(path,":");
while (tempath != NULL)
{
strcpy(finalpath,tempath); puts(commandandparameters);
strcat(finalpath,"/"); execl(finalpath,commandandparameters,NULL);
tempath = strtok(NULL, ":"); }
}while(command!="leave");
}
|
|
|
|
|
Very nice, but you still have not explained your problem clearly. What results are you seeing in your program and what results do you expect to see?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
When posting question, first you should ensure that my problem is clearly mentioned. because we cannot read your mind or monitor. you posted your code section , that is good. but exactly what is your problem?
|
|
|
|
|