1,105,592 Community Members

Chdir in c program

Member Avatar
mkab
Light Poster
29 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
D33wakar
Posting Whiz in Training
206 posts since Nov 2008
Reputation Points: 36 [?]
Q&As Helped to Solve: 31 [?]
Skill Endorsements: 2 [?]
 
0
 
int main( int argc, char **argv[] ) {//Wrong
int main( int argc, char *argv[] ) {//right
Member Avatar
mkab
Light Poster
29 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
}
Member Avatar
Ancient Dragon
Achieved Level 70
27,643 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,038 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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.

Member Avatar
mkab
Light Poster
29 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

OK thanks

Question Answered as of 2 Years Ago by Ancient Dragon and D33wakar
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: