Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A shell program in c++

Status
Not open for further replies.

LaoMa

Programmer
Aug 24, 2002
10
US
Hi,I'm writing a shell in c++ for Unix/Linux,which takes any system command
and execute it.
It's pretty simple now.In the first step,I just want to type certain command
and display it,like this:
#include <iostream>
using namespace std;
struct command_t{
char* name;
int argc;
char *argv[];
};

int main()
{
struct *command_t command;

while(true){
cout<<&quot;$&quot;;
cin>>command->argv[0]>>command->argv[1]>>command->argv[2];
cout<<command->argv[0]<<command->argv[1]<<command->argv[2];
}

return 0;
}

However,whenever I execute it in Linux,it keeps showing
segmentation fault
if I just execute cout<<&quot;$&quot;;
it is fine.
Thanks for any help or explanation.
 
You don't allocate any space for your command pointer. Change your declaration to
Code:
struct *command_t command = new command_t;
or
Code:
struct *command_t command = (command_t*)malloc(sizeof(command_t));
//Daniel
 
why the keyword struct in front of the pointer?
struct *command_t command;
should perhaps be:
comand_t* command;


Out of curiosity, what functionality are you wanting to add with this shell (when it is complete)?
 
Remark that for command->argv you must allocate memory for the array of arguments (or specify the maximum members of the array statically) and allocate memory for each argument.

Hope this helps,
Stefaan
 
Hi,

why not easier


main()
{
char rc,buffer[80];

while( 1 )
{
PROMPT() ;
gets( buffer ) ;

if( !strcmp( buffer, &quot;exit&quot; ) ) // or ctrlz
break ;

if( !Idontlikecommand( buffer,&rc ) )
{
YOURMESSAGE( buffer,rc)
continue;
}

system( buffer ) ;
}
printf(&quot; bye\n&quot; );
}


ciao !
 
Yes.Actually I already tried:
struct command_t* command=new command_t;
no success,still showing up
segmentation fault.
The problem is in cin>>command_t->argv[0] (or strcpy),if I assign a constant like
command_t->argv[0]=&quot;****&quot;;
it doesn't complain.
I wonder do I need to allocate memory for char* argv[]?
Thanks.
 
Hi,
I just resolved the problem by getting the command line with getline.(......),and then tokenized it,assigned the values to argv[0],argv[1]...actually no limit needs to be specified.Finally I found out I didn't have to allocate for argv[].
Thanks all!
 
On a related note, mixing cin and getline may result in some intresting behavior... The cin will often leave the return on the stream, and if this happens the next getline (deliminated by '\n') will read it. cin also doesn't have a flush method (cout does) and the way arround it is after a cin, write a statement like:

string junk;
getline(cin,junk,'\n');

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top