Shell program in C

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Shell program in C
# 1  
Old 11-21-2010
Shell program in C

Hi all,

I have an assignment from school to write a shell program in linux. the idea is to exercise fork() and execv() functions.. the shell program is supposed to be the master and every command that the user prints will run in a new process. we also need to try running the command in every path from the enviroment variable PATH.

for some reason, the execv function isn't working.. any idea what i'm doing wrong?


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;
}

I tried compiling a simple test file just to see if execv works and it did..
it was something like:
Quote:
#include <stdio.h>
#include <unistd.h>
void main()
{
int ret;
char *cmd[] = { "ls", NULL};
ret = execv ("/bin/ls", cmd);
printf("%d",ret);
}

Holon Institute of Technology/ B.Sc Computer Science / Operation Systems Course with Dr.Wiseman
# 2  
Old 11-23-2010
An argv vector normally ends in a null pointer? Usually, strtok should be tested to ensure there is a return (consider a for ( strtok ptr, ptr, strtok( NULL ) loop for neatness) (strtok is a good core dump generator if not handled very carefully)?

execvp does examine path, so I guess you are assigned writing execvp from execv.

I like to invest in white space to set off every loop, if, switch with new lines and indentation. I am not enjoying reading the code, so no a+ right off. You deserve structural indentation and spacing! You might always use {} with if/while/for, so you can easily add in debugging commands or additional function points.
Code:
some_action ;
 
if ( bool-predicate-one
  || bool-predicate-two ){
    action ;
  }
 
next-action ;

Also, never use gets(); it is unsafe, use fgets( ..., stdin ), getchar() or read( 0, ... ).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

C shell program

1. I've have to write a shell program that accepts Ctrl+T (in linux os in c language) and should print out the current time and date to the screen. I've written the following code but i've to type ^T individual rather than pressing ctrl+T(^T) to get the output. : 2. How do i make the shell... (2 Replies)
Discussion started by: zorro_phu
2 Replies

2. Shell Programming and Scripting

Shell Program , need help!!

Hi all, I am trying to get a file from an ftp server and i have the list of files which needs to be get from the ftp server. grep unix_prg*.* log.txt > log1.txt log1.txt (which has the list of files) 06-29-09 00:00AM 3550258 unix_prg090629 06-28-09 07:00PM ... (7 Replies)
Discussion started by: raghav1982
7 Replies

3. Shell Programming and Scripting

shell program

Iam having file 1 as wc -l file1 is 8 QWEERTYUU|7927836357398398398913 yuyuyu uyiuyuyuyuy yuiyuiyuyuyy FDHGFSHAJK|1476887897877777777771 iopwiiwpoi e . . . . I Need to read the abouve line in file1 so iam using the command as tail -n 8... (8 Replies)
Discussion started by: nivas
8 Replies

4. Shell Programming and Scripting

help with shell program

I want to print the value of variables a1, a2, a3 in for loop in the following program: a1=this a2=is a3=printed for((i=1;i<4;i++)) do var=a$i #w=`echo $var` e=${var} echo $e done But actually I get a1,a2,a3 as the output not the "this is printed" So the main question is if I... (3 Replies)
Discussion started by: adgarg
3 Replies

5. Shell Programming and Scripting

very first shell program.

in the beginners book i have it gives an exercise to try. saying to make a script that examines the time. it should keep examining every second or so and say some sort of message. Can anyone help me get going. Thanks (3 Replies)
Discussion started by: bebop1111116
3 Replies

6. Shell Programming and Scripting

shell program

hello, i have problem in awk filter in shell to concatenate two string plz send me a solution as soon as possible from jyoti ardeshana (4 Replies)
Discussion started by: jyotiardeshana
4 Replies

7. Shell Programming and Scripting

shell program

How to write a shell script which takes 3 strings as positional parameters,first and second are file names and third is a directory.if the two files exist in `pwd` and they contain a specific pattern and their size is greater than 32 bytes,moves these files into directory? (1 Reply)
Discussion started by: rameshparsa
1 Replies

8. Programming

Shell Program

I am programming the following simple shell program. It works for the most part, things like 'ls' and 'ps' work just fine. However when I add options, (example, ls -l) it does not execute the command. Also, I've been trying to add an "exit" command, so that I don't have to use the iterrupt; but... (3 Replies)
Discussion started by: TexasGuy
3 Replies

9. Shell Programming and Scripting

C shell Program

Hellow every body I am trying to write a very simple script in an executable file as following if ($?PWD) then echo "OK" else echo "No" endif but I am getting error during execution in c shell as synthax error at line 4 , unexpected end of file Please advise (2 Replies)
Discussion started by: Reza Nazarian
2 Replies

10. Shell Programming and Scripting

Help me with this Shell Program

Now, am in a very tight situation here. I really dont expect anyone to understand but please, try your best. am trying to right a program that goes back to the previous entry to correct a mistake. heres what am trying to do. i write a program like this Name : James Holgston... (1 Reply)
Discussion started by: TRUEST
1 Replies
Login or Register to Ask a Question