954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Chdir in c program

Hello everyone.

I'm trying to code the cd shell command in c. I used chdir function to code it but the problem is that when I execute the program on the terminal it doesn't change the directory. I kept a series printfs and conditions to test if it changes, though it does change, I don't see it on the terminal. Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

int main( int argc, char **argv[] ) {
  
  int result = 0;
  
  if (argc > 2) {
    printf("%s: Too many operands \nUsage: %s <pathname>\n", (char *) argv[0], (char *) argv[0]);
    exit(1);
  }
  
  if(argc == 1) {
    printf("argc is 1\n");
    const char* home = getenv("HOME");
    int i = chdir(home);
    
    if(i < 0)
      printf("directory couldn't be changed\n");
     
  
    else{
      printf("directory changed\n");
      printf("home = %s\n", home);
    }
    
    
    exit(0);
  }
  
  result = chdir(argv[1]);
  
  if(result == 0){
    printf("directory changed\n");
    exit(0);
  }
  
  else{
    switch(result){
      case EACCES: perror("Permission denied");
      break;
      case EIO:	 perror("An input output error occured");
      break;
      case ENAMETOOLONG: perror("Path is to long");
      break;
      case ENOTDIR: perror("A component of path not a directory"); 
      break;
      case ENOENT: perror("No such file or directory"); printf("enoent\n");
      
      default: perror("Couldn't change directory to %s", (char *) argv[1] ); 
    }
  }
   
  return 0;
}

Any ideas? Thanks

mkab
Light Poster
29 posts since Feb 2011
Reputation Points: 10
Solved Threads: 2
 
int main( int argc, char **argv[] ) {//Wrong
int main( int argc, char *argv[] ) {//right
diwakar wagle
Posting Whiz in Training
203 posts since Nov 2008
Reputation Points: 48
Solved Threads: 31
 

Oh yeah, sorry, it's so wrong. Corrected it. But still...

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

int main( int argc, char **argv ) {
  
  int result = 0;
  
  if (argc > 2) {
    printf("%s: Too many operands \nUsage: %s <pathname>\n", argv[0], argv[0]);
    exit(1);
  }
  
  if(argc == 1) {
    printf("argc is 1\n");
    const char* home = getenv("HOME");
    int i = chdir(home);
    
    if(i < 0)
      printf("directory couldn't be changed\n");
     
  
    else{
      printf("directory changed\n");
      printf("home = %s\n", home);
    }
    
    
    exit(0);
  }
  
  result = chdir(argv[1]);
  
  if(result == 0){
    printf("directory changed\n");
    exit(0);
  }
  
  else{
    switch(result){
      case EACCES: perror("Permission denied");
      break;
      case EIO:	 perror("An input output error occured");
      break;
      case ENAMETOOLONG: perror("Path is to long");
      break;
      case ENOTDIR: perror("A component of path not a directory"); 
      break;
      case ENOENT: perror("No such file or directory"); printf("enoent\n");
      
      default: print("Couldn't change directory to %s", argv[1] ); 
    }
  }
   
  return 0;
}
mkab
Light Poster
29 posts since Feb 2011
Reputation Points: 10
Solved Threads: 2
 

You can not make the results of the C program permanent on the terminal. Like all other shell programs anything done within the program (or shell script) is destroyed when the program ends.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

OK thanks

mkab
Light Poster
29 posts since Feb 2011
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: