LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-2006, 09:23 PM   #1
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
fork/exec problem


hi,
I'm tryign to get exec to list a directory that isn't in my PWD. for example a directory called /mnt/test.
my code is
Code:
void
ls_directory()
{
        int c_pid, status;
        c_pid = fork();

        if(c_pid == 0) {
                char *args[] = { "/mnt/test", (char *)0 };
                execv("/bin/ls", args);
                exit(1);

        } else {
                while(wait(&status) != c_pid);
        }

}
but for some reason the program still list the directory i currently runnign the program. Is there a way to ls a directory that isn't in my pwd?

thanks,

djgerbavor3
 
Old 04-13-2006, 09:52 PM   #2
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
after doing some reading and hacking i got it to work;
from reading I learned this @ http://www.unix.org.ua/orelly/unix/upt/ch38_02.htm
Quote:
...because a child process can't affect its parent's environment (38.3), some commands don't make sense as separate processes. For example, cd must be built in, or it couldn't change the working directory for the current shell.
here is how i got it
Code:
void
ls_directory()
{
        int c_pid, status;
        c_pid = fork();

        if(c_pid == 0) {
                char *args[] = { "-l","-al", "/mnt/test", (char *)0 };
                execv("/bin/ls", args);
                exit(1);

        } else {
                while(wait(&status) != c_pid);
        }

}
this got me the desire affect

thanks,
djgeravor3
 
Old 04-13-2006, 11:13 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
I think it's actually because the first argument should be the program's name. You args should look like this,
Code:
char *args[] = { "ls", "/mnt/test", (char *)0 };
 
  


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
exec cmd=perl... work but exec cgi doenst crions Slackware 5 12-09-2005 12:17 PM
how to fork/ exec schneidz Programming 10 08-30-2005 11:50 AM
Programming: fork, exec and system memory atienza Programming 2 08-24-2005 07:26 AM
what is the difference between Fork & exec augustus123 Linux - Newbie 1 11-29-2004 07:32 AM
Fork 'n Exec sourceman Linux - General 4 02-14-2002 02:31 AM

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

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