We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,289 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

3
Contributors
4
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
6
Views
Question
Answered
mkab
Light Poster
29 posts since Feb 2011
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
int main( int argc, char **argv[] ) {//Wrong
int main( int argc, char *argv[] ) {//right
D33wakar
Posting Whiz in Training
206 posts since Nov 2008
Reputation Points: 48
Solved Threads: 31
Skill Endorsements: 1

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
Skill Endorsements: 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.

Ancient Dragon
Achieved Level 70
Team Colleague
32,145 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

OK thanks

mkab
Light Poster
29 posts since Feb 2011
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Ancient Dragon and D33wakar

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0678 seconds using 2.69MB