Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

Ex1 homework help

$
0
0
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?

Code:

#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[i]=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

Viewing all articles
Browse latest Browse all 16232

Trending Articles