hello
I trying to run a program called bb with this Linux shell code and its looking in this directories:
trying to run bb in /usr/lib/lightdm/lightdm
trying to run bb in /usr/local/sbin
trying to run bb in /usr/local/bin
trying to run bb in /usr/sbin
trying to run bb in /usr/bin
trying to run bb in /sbin
trying to run bb in /bin
trying to run bb in /usr/games
trying to run bb in /usr/local/games
and there is no results
any ideas?
why its not looking in all directories of the system?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t pid;
char *sh="Shell>";
int i,pathindex=0, ret;
char line[80]; // getting the user prompt
char *paths[20]={"NULL"}; // 20 values path array
char *currentpath, *path; // current path to execute in
char *arg[]={NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL} ;
//////////////////////////////////////////////////////////////////////////////
path=getenv("PATH");
currentpath=strtok(path,":");
while (currentpath!=NULL)
{
paths[pathindex]=currentpath; // filling the paths array
currentpath=strtok(NULL,":"); // from enviroment variable
pathindex++; // PATH.
}
system("/usr/bin/clear");
printf("\n\nShell Program (OS Course)\n");
printf("-------------------------\n\n");
while (1)
{
printf("%s",sh);
gets(line);
arg[0]=strtok(line," "); // getting command
if (strcmp(line,"leave")==0) exit(0);
for (i=1;i<10;i++) arg=strtok(NULL," "); //getting command arguments
pathindex=0;
while (paths[pathindex]!=NULL)
{
if ((pid=fork())<0)
{
printf("Error: Couldn't fork\n");
exit(1);
}
if (pid==0)
{
printf("trying to run %s in %s\n",arg[0],paths[pathindex]);
ret=execv(paths[pathindex],arg);
if (ret==-1) exit(1);
}
else { wait(); pathindex++; }
}
}
return 0;
}
Holon Institute of Technology/ B.Sc Computer Science / Operation Systems Course with Dr.Wiseman
Unless you are required to implement the path lookup yourself, there are exec* functions which will check the components of PATH until a matching file is found. Those variants have a 'p' in their name, such as execvp, execlp, etc.
Unrelatedly, what if there are more than 19 components in PATH? Your code cannot cope with that situation. (Although this would be unacceptable for production code, it may be acceptable for the purpose of this homework assignment.)
If you do find and run the executable, should you abort the search? Your code will attempt to run multiple commands with the same name, if they exist.
The file is the executable's file name prefixed with an entire path. You supply a path, but no executable.
BTW, wouldn't it be better to fork once and then try to execute a file than forking and failing exec n times, each time wasting a resource consuming process creation?
The type of each argument that you pass to execv may be correct, but paths[pathindex] is a path to a directory where execv expects a path to an executable.
For additional feedback, if execv returns -1, you can check the value of errno.
Well, read the man page, and read the reference that alister gave in his post, where the use and function of the exec family is explained. You have to learn to tell an image (file) from a path, amongst other things.
You said you want to execute the bb program. Which is the executable image. I don't see bb in your code snippet. Try to execv /path/to/image/bb .
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.
Please review the rules, which you agreed to when you registered, if you have not already done so.
More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.