Unix/Linux Go Back    

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

writing a shell program in ubuntu


Closed Linux or Unix Question    
Thread Tools Search this Thread Display Modes
Old Unix and Linux 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?

#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   
 while (currentpath!=NULL)
   paths[pathindex]=currentpath; // filling the paths array
   currentpath=strtok(NULL,":"); // from enviroment variable
   pathindex++;   // PATH.
 printf("\n\nShell Program (OS Course)\n");
 while (1)
   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
   while (paths[pathindex]!=NULL)
    if ((pid=fork())<0)
     printf("Error: Couldn't fork\n");
    if (pid==0)
    printf("trying to run %s in %s\n",arg[0],paths[pathindex]);
     if (ret==-1) exit(1);
    else { wait(); pathindex++; }
 return 0;

Sponsored Links
Old Unix and Linux 11-21-2010
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Join Date: Jun 2009
Last Activity: 27 June 2016, 6:29 AM EDT
Location: Zürich
Posts: 8,595
Thanks: 341
Thanked 1,091 Times in 948 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 01:10 PM.