Execlp Linux Question -- in C

This is a discussion on Execlp Linux Question -- in C within the Linux Programming forums, part of the Platform Specific Boards category; hello, first time posting here. I have a problem with execlp and I can't seem to find the answer using ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    8

    Execlp Linux Question -- in C

    hello, first time posting here.

    I have a problem with execlp and I can't seem to find the answer using google. The solution must be really silly but I can't find it and I'm running out of time!

    A simplified version of what I'm trying to do would be this:

    I need to write a c program that recursively runs the directory given as the first parameter and copy it in the directory given as the second parameter through a pipe using the "cp" command in linux.
    (for example "./progname firstdir seconddir")

    this is the part where a file is found and is sent through the pipe to be copied by cp
    Code:
    sprintf(file_route,"%s/%s",dir_route,dir_entry->d_name);
    write(tube[1],file_route,strlen(file_route));
    and this should be the part where I fork() , redirect tube[0] to STDIN_FILENO , and execlp to "cp"

    Code:
    close(tube[1]);
    dup2(tube[0],STDIN_FILENO);
    close(tube[0]);
    execlp("cp","cp",something something I dunno,NULL);
    so what I can't figure out is what the heck I'm supposed to add as arguments to execlp.
    I hope the code is not too limited to make sense and thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What arguments do you plan to give cp? That's what goes in the other spots.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    By through a pipe, do you perhaps mean using popen and pclose to run the command? Using execlp seems quite the difficult way.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    oh right sorry,
    the command is supposed to be like this
    "cp file_to_be_copied_path directory_where_it_will_be_copied"
    three arguments,
    for example "cp /home/name/document.pdf /home/name/Desktop"
    and document.pdf gets copied to the Desktop
    but I must do that for every file that the initial function finds and sends (its path) through the pipe.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    @anduril462

    pipe as in:
    Code:
    int tube[2];
    pipe(tube);
    I'm not very familiar with all this, I didn't know what a fork() was about 3 months ago so I don't know any other ways to do it.

    It's for an exercise so I don't have a choice, if it doesn't make sense I'll post what the exercise says.

    (I'm not asking you to solve it, I just don't get what the arguments need to be if it's going to be copying one after the other after the execlp)

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I would think you would need to accumulate all the files beforehand -- once you hit execlp your program ceases to exist, unless you were planning to fork a separate copy for every file on the list.

    You can feed cp as many filenames as you want; or for that matter, you can feed cp a wildcard.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    I'm not sure how I could do that,
    I'll translate the exercise in a sec.

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Write a c program that creates two processes so that,
    from two directories given as initial parameters, (again "./progname param1 param2")

    The first process will run through all the files in the directories under the path given by the first parameter and will send their ( the files' ) path through a pipe.

    The second process will create a copy for each file received through the pipe, in the second directory, given as the second parameter.

    So I only have two processes as a limit, the initial and a fork, can't do more, I have the program done the only part missing is the execlp parameters missing in my first post.

    So how would you do the copy thing, it doesn't specifically say that I need to do it with an execlp but I know that's what it means cause that's the only way I've learned till now. So, how would you do the copy thing given the above restrictions.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    All this time and you've never learned about system()?

    Your second copy can, every time a filename comes through the pipe, just do a system call to cp. You'll have to build the command string up from the pieces you've got, but that's not too difficult.

    Or I suppose you could do the whole "read the file and then write it somewhere else" thing, if you want to be slow about it.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Quote Originally Posted by John Albatros View Post
    So I only have two processes as a limit, the initial and a fork, can't do more, I have the program done the only part missing is the execlp parameters missing in my first post.
    Quote Originally Posted by tabstop View Post
    All this time and you've never learned about system()?

    Your second copy can, every time a filename comes through the pipe, just do a system call to cp. You'll have to build the command string up from the pieces you've got, but that's not too difficult.

    Or I suppose you could do the whole "read the file and then write it somewhere else" thing, if you want to be slow about it.
    If he really does have a strict 2 process limit, then he can't use system, since it would create a third process. I would assume the teacher wants him to do his own read/write bit to copy files.

    I'm testing out a few things and will post a little pseudo code when I have a working example.

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Quote Originally Posted by tabstop View Post
    Your second copy can, every time a filename comes through the pipe, just do a system call to cp. You'll have to build the command string up from the pieces you've got, but that's not too difficult.
    but how is that done?
    can you post like an example of a that part of the code that does that?
    and are you sure there isn't a simple way to do it with execlp?
    excuse my lack of knowledge on this subject.

    edit: just read anduril462's post, waiting.
    Last edited by John Albatros; 01-09-2011 at 12:03 PM.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Quote Originally Posted by John Albatros View Post
    but how is that done?
    can you post like an example of a that part of the code that does that?
    and are you sure there isn't a simple way to do it with execlp?
    excuse my lack of knowledge on this subject.
    So you would run a command like: system("cp /path/to/file1 /path/to/file2");

    It would behave almost identical to you running that command from the command line. You would build the string using something like sprintf.

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    I've thought of doing a fork + execlp for each string received but that (as you said) wouldn't be just 2 processes.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Okay, so all you really need is some fork logic, no execlp. Here is some pseudo code:
    Code:
    function main
        int pfd[2];  // to be used with pipe() function
        initialization and error checking
        if (pipe(pfd) == -1)
            exit with error
        // you may want a second call to pipe if you need 2-way communication
        switch on fork()
            case -1:
                handle error and quit
            case 0:
                close(pfd[1]);
                do_child_stuff(argv[2], pfd[0])
                close(pfd[0]);
                break
            default:
                close(pfd[0]);
                do_parent_stuff(argv[1], pfd[1])
                close(pfd[1]);
                break;
    
        return 0
    
    function do_parent_stuff(char *src_path, int pfd)
        opendir(src_path)
        for each entry from readdir
            write(pfd, entry->d_name, strlen(entry->d_name)
            // maybe wait for child to report success here
        closedir
    
    function do_child_stuff(char *src_path, char *dst_path, int pfd)
        while read(pfd, buf, sizeof(buf))
            copy_file(src_path, dst_path, buf)
            // maybe send success back to parent here
    
    function copy_file(char *src, char *dest, char *file_name)
        this function can use system or do it's own set of read/write or
            fread/fwrite calls to copy the contents from src to dest

  15. #15
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    I see,
    thanks a lot for your time writing the code.

    I guess I will make the copies with fwrite instead of using cp so as not to exceed the process limit.

    thanks again everyone for your time and efforts : D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I have a question about hard drives in Linux
    By Overworked_PhD in forum Tech Board
    Replies: 2
    Last Post: 12-29-2007, 09:29 AM
  2. sockets in linux question
    By matrixon in forum Networking/Device Communication
    Replies: 6
    Last Post: 08-27-2006, 01:48 PM
  3. Linux header question
    By invisibleghost in forum Linux Programming
    Replies: 5
    Last Post: 02-17-2005, 09:03 AM
  4. question from linux board ( not os dependant )
    By crypto in forum C Programming
    Replies: 4
    Last Post: 11-15-2002, 01:09 AM
  5. Question about LINUX
    By River21 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-17-2001, 06:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21