LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2002, 04:38 AM   #1
pdstatha
LQ Newbie
 
Registered: Apr 2002
Location: UK - Swansea
Posts: 3

Rep: Reputation: 0
Changing directory


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;
}
 
Old 04-11-2002, 07:08 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
Well the program actually does change the directory but only for the environment in which the program is running. I hope you weren't expecting it to change the directory in the shell which you are running the program. It won't work that way.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
changing home directory peaceslp Linux - General 1 12-09-2005 07:13 AM
Changing the home directory kyleinc Linux - General 2 02-26-2005 11:54 PM
Changing the name of the directory I am in fieldmethods Linux - General 4 01-08-2004 04:07 PM
changing home directory netboy_541 Linux - General 5 09-10-2003 04:09 AM
Changing directory permissions Bagsy Linux - Newbie 1 07-08-2003 05:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration