Ok i've written some code which implements the cd command. Problem is it doesn't actually seem to change the directory at all. It says it has but when u do a pwd ur still in the same directory!!! WHY???????????????
Code:
/*The following code will implement the*
*cd (change directory) command in UNIX.*/
#include <pwd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc,char **argv){
uid_t me;
struct passwd *user;
if(argc>1 && argv[1] != NULL){
if((errno = chdir(argv[1])) == ENOTDIR){
fprintf(stderr,"Can't change to %s\n",argv[1]);
return -1;
}else
printf("Changed to %s\n",argv[1]);
}else{
me = getuid();
user = getpwuid(me);
if(!user){
fprintf(stderr,"Couldn't find user %d\n",(int) me);
exit(EXIT_FAILURE);
}
if((errno = chdir(user->pw_dir)) == ENOTDIR){
fprintf(stderr,"Can't change to %s\n",user->pw_dir);
return -1;
}else
printf("Changed to %s\n",user->pw_dir);
}
return EXIT_SUCCESS;
}