Hey all,
I've been trying to build a simple c shell for assignment, and im a bit stuck.
It doing something different on my laptop to that it does on my pc :S laptop is on mint 7 pc is virtual machine on mint 8.
Im using the getcwd to get the current directory, and on my pc it worked fine, but on laptop i just get returned null.
Also on laptop when i come out of the program, it goes back to normal shell, then when i press button it goes back into program on a constant loop, and wont go out, even using ctrl c , which has baffled me a fair bit if im honest.
Heres my code
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
system("clear");
int z =1;
do
{
char *dir;
char *user;
char *progname;
char *parameters;
char *cmd, *command;
char buffer[50], *input;
int childstatus = 1, status;
int cmdlen;
pid_t child;
user = (char *)getenv("USER");
getcwd(dir,50);
if( user == NULL )
{
user == "Unknown";
}
printf("%s@dmush ~%s $: ", user, dir);
input = fgets(buffer, 50, stdin);
cmd = strtok(input, " ");
command = cmd;
while(1)
{
if( command == "exit")
{
break;
}
else if(command == "set")
{
/*
* set an environment variable
*/
//setenv(parm1, parm2, true);
printf("environ var not set\n");
}
//Extract next part of command
cmd = strtok(NULL, " ");
//Check that there is nothing else to extract
if (cmd == NULL)
{
break;
}
childstatus = fork();
progname = command;
parameters = cmd;
if (childstatus == 0)
{ //if parent process is running
wait(&status);
}
else
{
int i;
for (i=0; i<3; i++)
{
execlp(progname,progname,parameters,(char *)0);
}
}
break;
}
}while (z == 1);
return 0;
exit(0);
}
Thanks for any help