LinuxQuestions.org
Visit Jeremy's Blog.
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 10-15-2007, 04:05 PM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
How to understand chdir function?


Hi, I'm trying to do a small C program that changes the current directory.

Code:
#include <unistd.h>

char *directory = ".";

void changedir()
{
  directory = "/tmp";

  if (chdir(directory) != 0)
    perror("chdir() to %s failed\n", directory);

  printf("%s\n", directory);
}

int main()
{
  changedir();
}

The problem is that, I don't know what to do with the variable directory.
Ie, if I want to first, change directory and them do an ls of the new directory ( I know that I don't have the ls part here), I must do an ls with the directory value has an argument?

Ex:
Code:
...
execl("ls", directory, (char *) 0);
...
I must keep the value of the directory?

Thanks,
Pedro
 
Old 10-15-2007, 05:53 PM   #2
duenez
LQ Newbie
 
Registered: Apr 2006
Location: Cambridge, MA
Distribution: SuSE 11.2
Posts: 5

Rep: Reputation: 0
First of all, have you noticed that the original value of directory (".") is lost when you reassign it to "/tmp"?

Second, after calling chdir, every command will be executed as if it were performed in that directory until the program exits. So no need to keep the directory name in a string.
 
  


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
my firewall script: unable to understand its function Niceman2005 Linux - Security 3 11-16-2005 07:21 PM
chdir oldstinkyfish Linux - Software 3 11-14-2004 03:58 AM
about chdir() feetyouwell Programming 3 10-02-2004 12:26 PM
chdir problem Abe_the_Man Linux - Software 4 08-19-2003 11:39 PM
Unicode chdir sourceman Programming 0 11-04-2002 01:50 AM

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

All times are GMT -5. The time now is 09:27 AM.

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