writing a shell program in ubuntu | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

writing a shell program in ubuntu

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-21-2010
r3vive r3vive is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 23 November 2010, 1:51 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
writing a shell program in ubuntu

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

Sponsored Links
    #2  
Old 11-21-2010
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 15 April 2014, 6:15 PM EDT
Location: in der Schweiz
Posts: 8,322
Thanks: 312
Thanked 1,013 Times in 886 Posts
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.

Thank You.

The UNIX and Linux Forums.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need help writing a program to look for doubles rickym2626 UNIX Desktop for Dummies Questions & Answers 2 04-13-2009 03:46 PM
Problem with writing a program CompNoob UNIX for Dummies Questions & Answers 1 09-07-2008 04:00 AM
Having trouble writing a basic shell program SeanWuzHere Shell Programming and Scripting 3 11-30-2006 12:41 PM
Need help on a program I'm writing dmosheye Shell Programming and Scripting 2 09-21-2006 03:39 PM
writing a program carlvernon UNIX for Dummies Questions & Answers 3 06-01-2006 03:47 PM



All times are GMT -4. The time now is 12:14 AM.