Rosie Red Rosie Red - 1 year ago 76
Bash Question

My cd function does not go to the home directory

I am building a custom shell in c, and one of the requirements is that the folder from while you run the program must be the "home" folder.

That is, if you type in just

cd ~
you should get to that directory. I have implemented it, but it is not working.

char *basedir;
void init_prompt()
basedir = cwd;
void cd_me(char **argv)

Any suggestions on how to fix this?

Also when I print basedir I am getting the correct output.

Answer Source
 char *basedir;
 basedir = cwd;

You make basedir a synonym to cwd. Whenever cwd changes, basedir follows. It's a pointer, it cannot remember its own string, it can only point to someone else's string.

You must make a copy instead.

 char basedir[100];

Add bounds checks and error handling as needed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download