Shell Program

I need help figuring out how to read user input into the exec() function.

This is the code I am working with.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//*********************************************************
//
// Includes and Defines
//
//*********************************************************
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <cstdlib>


using namespace std;


//*********************************************************
//
// Extern Declarations
//
//*********************************************************
using namespace std;
extern "C"
{
  extern char **gettoks();
} 


//*********************************************************
//
// Main Function
//
//*********************************************************
void type_prompt();
void type_prompt()
{
	int counter = 0;
	cout << "[sprechtshell (" << counter <<")~]";
	counter++;
}


	

int main( int argc, char *argv[] )
{
	

	int fd[2];
  // local variables
  int ii;
  char **toks;
  int retval;

  // initialize local variables
  ii = 0;
  toks = NULL;
  retval = 0;



  // main (infinite) loop
  while( true )
    {
	  int childPid;
	  char *cmdLine;
	  
	  
	  type_prompt();
	  
      // get arguments
      toks = gettoks();
	  

      if( toks[0] != NULL )
	{
		childPid = fork();
		if ( childPid == 0)
		{
			
			execl(toks[0], toks[1], NULL);
	  // simple loop to echo all arguments
	  /*for( ii=0; toks[ii] != NULL; ii++ )
	    {
	      cout << "Argument " << ii << ": " << toks[ii] << endl;
	    }*/

			if( !strcmp( toks[0], "exit" ))
				break;
		}
    }
	}

  // return to calling environment
  return( retval );
}
Last edited on
I don't see a function called exec().

to get user input
1
2
3
cout << "type a value: ";
cin >> Avalue;
cout << Avalue;
I don't see a function called exec().

He's trying to parse command line arguments, if I'm not mistaken. He is also overlooking the basics of passing arguments to functions, unless this is somehow supposed to be a replacement for it? toks = gettoks();
There is not reason to make a separate function to get the arguments since they are right there to begin with. You just need to loop through them.

http://www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html
So yea, it's supposed to work like a command line shell. The first parameter in execl is supposed to be const char *pathname. What I need to know is how to get the user input and pass it to that first parameters in execl.
What did you not understand from the link I posted?
Topic archived. No new replies allowed.