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 04-13-2007, 09:57 AM   #1
haydari
LQ Newbie
 
Registered: Apr 2007
Posts: 13

Rep: Reputation: 0
how to use "cd" command within a exec call?


I need to be able to change directories in my shell terminal program.

But since the "cd" is not a program itself, I can't call it with execve.

I also tried execl and execlp functions but it didn't work. Do you have any idea about how can I accomplish this?
 
Old 04-13-2007, 10:13 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

I'ts C? I think what you're looking for is the "chdir" function.
 
Old 04-13-2007, 11:16 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
To explain the behavior that the original poster is seeing, and why his approach will never work, the exec() call spawns a new process, and any actions performed within the context of that process are completely independent of the parent process. If it were any other way, then all processes would have the same working directory, as all processes are descendants of the original init process. The same effect applies to modification of environment variables, which is a similar thing that people trip over. bash uses the source command to work around this.
Hope this clears things up.

--- rod.
 
Old 04-13-2007, 01:30 PM   #4
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
the exec() call spawns a new process
No, not really

if exec is able to spawn a new process then why we would be in a need to have fork system call


fork system call would create a new process while
exec system call would overlay the process image of a process with the code and data of a new process copied
 
Old 04-13-2007, 04:34 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Sorry, you're right. However, since exec() never returns, the original process becomes effectively terminated. I was thinking of the system() function, which does a fork internally.
On re-reading the original post, I answered a question not even asked. My double-oops. The first reply answered the whole question. mea culpa.
--- rod.
 
  


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
No UTMPX entry, You must EXEC "login" for the lowest "shell" ooihc Solaris / OpenSolaris 7 03-12-2007 02:09 PM
does "find -exec" not recognize built-in command? yaru22 Linux - General 1 06-30-2006 10:35 AM
Any way to get "Alice"; "Call of Duty" series and "Descent 3" to work? JBailey742 Linux - Games 13 06-23-2006 01:34 PM
where is command "cd" located? or is it a command/ system call? feetyouwell Linux - Software 5 10-01-2004 08:01 PM
Benoit's Alcatel - pppd call adsl "command not found" GM287 Linux - Networking 5 04-04-2004 09:56 AM

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

All times are GMT -5. The time now is 05:15 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