Ex1 homework help


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Ex1 homework help
# 1  
Old 11-25-2012
Error 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?

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
# 2  
Old 11-25-2012
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
This User Gave Thanks to bakunin For This Post:
# 3  
Old 11-25-2012
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?
# 4  
Old 11-25-2012
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...ions/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

Last edited by alister; 11-25-2012 at 04:04 PM..
This User Gave Thanks to alister For This Post:
# 5  
Old 11-25-2012
but I do use execv in the right syntax:
syntax:int execv(const char *path, char *const argv[]);
my: execv(paths[pathindex],arg);
# 6  
Old 11-25-2012
man page of execv:
Quote:
The initial argument for these functions is the pathname of a file which is to be executed.
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?
This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-25-2012
Quote:
Originally Posted by alexthelion
but I do use execv in the right syntax:
syntax:int execv(const char *path, char *const argv[]);
my: execv(paths[pathindex],arg);
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
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Homework Help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: Write a command to display lines ending with the... (1 Reply)
Discussion started by: elh009
1 Replies

2. Homework & Coursework Questions

Homework

plz i need code to search about numbers like this : 962785785698 962795565488 962785321565 962777321684 962795979515 i need code to detect just numbers start with "96278" i need it in awk !! thanks (1 Reply)
Discussion started by: eyad mohammad
1 Replies

3. Homework & Coursework Questions

Please help me with my homework!!!

A shell script is a script written for the shell, or command line interpreter, of an operating system. Typical operations performed by shell scripts include file manipulation, program execution, printing text etc. Shell : In computing, a shell is a piece of software that provides an interface for... (1 Reply)
Discussion started by: ubun
1 Replies

4. Shell Programming and Scripting

Homework

1. Write a shell script to print the file names of all files having .txt extension of a given directory after converting to uppercase letters. The input (directory name) should be given as command line argument. The script will also check whether sufficient arguments are passed or not and whether... (1 Reply)
Discussion started by: aninda1989
1 Replies

5. Homework & Coursework Questions

Need some help with my homework

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi, since I'm relatively new to Unix I need some help. I've installed FreeBSD 8 on a virtual machine and I've done... (4 Replies)
Discussion started by: iLeeT777
4 Replies

6. Homework & Coursework Questions

Homework Help.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hello, My name is Jordan and studying at the New Bulgarian University in Sofiya. This is my homework but can... (2 Replies)
Discussion started by: freestar
2 Replies

7. Post Here to Contact Site Administrators and Moderators

Homework?

https://www.unix.com/shell-programming-scripting/113946-scripts.html Looks like an assignment or homework to me, does it to you? (5 Replies)
Discussion started by: TonyFullerMalv
5 Replies
Login or Register to Ask a Question