PDA

View Full Version : WHAT is the PROBLEM??



Unregistered
02-06-2002, 10:42 PM
so I have this mini-shell running....and it's forking correctly but It's not executing the commands that I want it to run. It only executes ls no matter what command i type in (date, more, etc). I've modified the execve statement as many ways as I can think of, but to no avail. Here is my code:

mars:$ cat modshell1.cpp
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream.h>
#include <string>
#include <iomanip.h>
#include <sys/wait.h>

main()
{
cout<<"You have entered my 1814 shell"<<endl;
string command;
char *line[20];
int n, pid, parentpid, childpids[0];


while (command !="exit")
{

cout<<"GIMME:#";
cin>>command;
cout<<"The command you typed was: " <<command<<endl;
if (command == "exit")
return 0;

//end of while

parentpid=getpid();
printf("I am the parent process and my pid is %d\n", getpid());
if (pid=fork())
{
int status;
waitpid(-1, &status, 0);
childpids[n]=pid;
}
else
{
printf("I am a child process and my pid is %d\n", getpid());
line[0] = "ls";
line[1] = "-la";
line[2] = NULL;
execve("/bin/ls", line, NULL);
exit(0);
}
printf("I am still the parent process and my pid is %d\n",
getpid());

// line[0] = "ls";
// line[1] = "-la";
// line[2] = NULL;

// execve("/bin/ls", line, NULL);
// kill(childpids[n]);
}//end of while
if (command =="exit")
return 0;

printf("I am still the parent process and my pid is %d\n", getpid());
}//end of main

Unregistered
02-06-2002, 10:46 PM
I guess I could have cleaned that up, huh? here it is:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream.h>
#include <string>
#include <iomanip.h>
#include <sys/wait.h>

main()
{
cout<<"You have entered my 1814 shell"<<endl;
string command;
char *line[20];
int n, pid, parentpid, childpids[0];


while (command !="exit")
{

cout<<"GIMME:#";
cin>>command;
cout<<"The command you typed was: " <<command<<endl;
if (command == "exit")
return 0;

parentpid=getpid();
printf("I am the parent process and my pid is %d\n", getpid());
if (pid=fork())
{
int status;
waitpid(-1, &status, 0);
childpids[n]=pid;
}
else
{
printf("I am a child process and my pid is %d\n", getpid());
line[0] = "ls";
line[1] = "-la";
line[2] = NULL;
execve("/bin/ls", line, NULL);
exit(0);
}
printf("I am still the parent process and my pid is %d\n",
getpid());
}//end of while
if (command =="exit")
return 0;

printf("I am still the parent process and my pid is %d\n", getpid());
}//end of main

Salem
02-07-2002, 01:20 PM
> cin>>command;
Well you have to break the command up into words, like "ls", "-la"

Ok, so there's another problem, which is cin only reads one word at a time, so how about

cin.getline( command );

Right, having got a string of space separated words, you need to split them up into words

For example
char *str = command.c_str(); // is this how you get at the string within a c++ string object?

Then to actually break out the words


char *p;
int i = 0;
for ( p = strtok( str, " " ); p != NULL ; p = strtok( NULL, " " ) ) {
line[i++] = p;
}
line[i] = NULL;


Then it should be
execve("/bin/ls", line, NULL);

Or perhaps
execve(line[0], line, NULL);

One more thing
if (pid=fork())

Should be
if ( (pid=fork()) != 0 )

In this example, it doesn't much matter, but you will be badly burnt in future if you type = when you meant ==