Ex1 homework help

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

Simple answer: because you don't look in all directories. You only look in the directories in the PATH and this is exactly what the program does.

Have you written this program yourself or copied it from somewhere?

I hope this helps.

bakunin

1 Like

partly by myself
how can I look in all directories ?

---------- Post updated at 12:37 PM ---------- Previous update was at 12:25 PM ----------

one more thing even if I will run "zip" and its located in bin directory it wont execute it, any idea why?

You're using execv incorrectly. The first argument should be a path to the executable, not a directory. See "Using execv()" @
http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html

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.

Regards,
Alister

1 Like

but I do use execv in the right syntax:
syntax:int execv(const char *path, char *const argv[]);
my: execv(paths[pathindex],arg);

man page of execv:

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?

1 Like

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.

Regards,
Alister

1 Like

Rudic maybe you right but I'm not strong in Linux so its difficult for me even trying to understand what you mean with the execv

---------- Post updated at 04:18 PM ---------- Previous update was at 04:13 PM ----------

yeah but it supposed to serach In every path in the pathindex and If it found it should be executed
i'm wrong?

I tried other method and still not working :frowning:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
 pid_t pid;
 int pStatus;
 char *sh="Shell>";
 char runLine[50];
 int i,pathindex=0, ret;
 char line[80];   // getting the user prompt
 char *ListPathes=""; //
 char *currentpath, *path;  // current path to execute in   
 char *argv[10]={NULL} ;
//////////////////////////////////////////////////////////////////////////////
printf("CMD: ");
gets(line);
 while ((strcmp(line,"leave"))!=0)
{
 if ((pid=fork())==0) //child process started
 {
   ListPathes=getenv("PATH");
   currentpath=strtok(ListPathes,":");
   while (currentpath!=NULL)
    {
      strcpy(runLine,currentpath);
      strcat(runLine,"/");
      strcat(runLine,line);
      execv(runLine,argv);
      currentpath=strtok(NULL,":");
    }
 }
 else //parent process continue
 {
 wait(&pStatus);
 if (pStatus!=0)
  printf("Error");
 }
}
}

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 .

1 Like

I tried to execute even "date" and no result

Did you try execv /bin/date ?

like that:
ret=execv("/usr/bin","date");
?

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.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.