dualcyclone
03-09-2004, 10:49 AM
Hi,
I have made my own CD command for a project, it works, but obviously not properly as it is not builtin to my own shell.
eg, when it changes directory, it changes for the amount of time the function call is made, then returns to the normal system settings.
I am now building this function in and am having a few problems with it.
I dont really want to post the entire code on the site incase anyone does a search for it and finds it, and this would lead to a big implication with the university.
Here are the two parts that are in the shell that run the mycd:
int builtin() {
if (check("exit"))
exit(0);
else if (check("mycd"))
return mycd();
else if (check("mypwd"))
return mypwd();
else
return 0;
}
(from parse, builtin checks to see if user has typed any of the above commands, then runs them)
int mycd(int argc, char **argv) {
char path[MAXNAME];
if(argc == 1)
strcpy(path, getenv("HOME"));
else
strcpy(path, argv[1]);
if(chdir(path) == 0) {
if (setenv("PWD", path, 1) == 0)
printf("directory: %s\n", getenv("PWD"));
else
printf("insufficient space in the environment");
} else
perror("error changing dir");
return(0);
}
Errors which occur when I run the shell are:
~if mycd is the first command to run in the shell:
>>>>>Segmentation fault, then exits from my shell
~otherwise:
>>>>>mycd on its own; error changing dir: cannot find file or directory
>>>>>mycd with args; error changing dir: cannot find file or directory
The mycd command. If anyone can see what I am doing wrong, I'd love the help, once I have mycd working, i should be able to get on with mykill and myls easily!!
Thanks for anyones help
I have made my own CD command for a project, it works, but obviously not properly as it is not builtin to my own shell.
eg, when it changes directory, it changes for the amount of time the function call is made, then returns to the normal system settings.
I am now building this function in and am having a few problems with it.
I dont really want to post the entire code on the site incase anyone does a search for it and finds it, and this would lead to a big implication with the university.
Here are the two parts that are in the shell that run the mycd:
int builtin() {
if (check("exit"))
exit(0);
else if (check("mycd"))
return mycd();
else if (check("mypwd"))
return mypwd();
else
return 0;
}
(from parse, builtin checks to see if user has typed any of the above commands, then runs them)
int mycd(int argc, char **argv) {
char path[MAXNAME];
if(argc == 1)
strcpy(path, getenv("HOME"));
else
strcpy(path, argv[1]);
if(chdir(path) == 0) {
if (setenv("PWD", path, 1) == 0)
printf("directory: %s\n", getenv("PWD"));
else
printf("insufficient space in the environment");
} else
perror("error changing dir");
return(0);
}
Errors which occur when I run the shell are:
~if mycd is the first command to run in the shell:
>>>>>Segmentation fault, then exits from my shell
~otherwise:
>>>>>mycd on its own; error changing dir: cannot find file or directory
>>>>>mycd with args; error changing dir: cannot find file or directory
The mycd command. If anyone can see what I am doing wrong, I'd love the help, once I have mycd working, i should be able to get on with mykill and myls easily!!
Thanks for anyones help