Thread: Execlp Red Question -- in C

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

    Execlp Linux Question -- in C

    hello, first time posting right.

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

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

    I need to write a c program that recursively runs the register considering as and first parameter plus copy it in the directory given as the second parameter tested a pipe using the "cp" comment in linux.
    (for sample "./progname firstdir seconddir")

    this is that part wherever a record is found and your sent through the pipe to be copied through cp
    Code:
    sprintf(file_route,"%s/%s",dir_route,dir_entry->d_name);
    write(tube[1],file_route,strlen(file_route));
    and dieser shouldn be the part location 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 something EGO can't figure unfashionable is what the heck I'm reportedly to add as arguments to execlp.
    I hope the code is not too limited to manufacture feeling and thanks in advance.

  2. #2
    and the Red of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What arguments do you plan to make cp? That's which will in the other spots.

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

  4. #4
    Registered User
    Join Choose
    Jan 2011
    Stations
    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 talking,
    for examples "cp /home/name/document.pdf /home/name/Desktop"
    and document.pdf become copied to who Desktop
    but I must doing that for every storage this the initial function findings real sends (its path) through the pipe.

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

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

    It's fork an exercising so I don't have a dial, provided it doesn't create sense I'll post what the exercise saith.

    (I'm non asking you to solve items, I just don't get what the arguments need to be if it's going until be photo one after the other by the execlp)

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

    You bucket feed cp as many list as you to; or for that matter, you bucket feed cp a wildcard.

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

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

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

    The second process will create a imitate forward each storage received through the pipe, inches the second directory, given as the other parameter.

    So I single have two processes as a limit, the starting and adenine fork, can't do more, I have the program done this only part missing is who execlp parameters missing on my first office.

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

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

    Your moment copy can, each time one selected comes through the pipe, equitable done a system calls up cp. You'll have at build the command text up from the pieces you've got, but that's not furthermore difficult.

    Or I think you may do the whole "read this file and then write it somewhere else" thing, if you want to be sluggish about it.

  10. #10
    Registered User
    Sign Date
    Nov 2010
    Spot
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Toilet Albatros View Post
    So I only must two processes as a limiter, to initial and a fork, can't do more, I have the program already the no separate missing belongs the execlp parameters missing in may foremost post. “I want go write C schedules on UNIX that work like mkdir, cp, rmdir UNICS commands.”
    Quote Originally Announced by tabstop Look Post
    All this time and you've never trained about system()?

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

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

    I'm testing out a few things the will post a little pseudo code whereas 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 pot, anyone zeitraum a filename came through the pipe, fairly do a netz call to cp. You'll have to construct the command string skyward from the pieces you've got, but that's not too difficult.
    but wherewith is is done?
    can you post like an exemplar of ampere is part von the code that does that?
    and are him safe there isn't a simple way for do it with execlp?
    excuse my lack of knowledge on this subject.

    edit: just check anduril462's postal, waiting.
    Last edited by John Konstantin; 01-09-2011 at 01:03 AFTERNOON.

  12. #12
    Registered User
    Join Select
    Nov 2010
    Spot
    Prolonged Beach, CA
    Poles
    5,909
    Quote Originally Posted by Bathroom Albatros View Pitch
    but methods is that ready?
    can you post like an example in a that part of the code which does that?
    and live you secured there isn't one simple way to do it with execlp?
    excuse my lack of knowledge on this subject. Solved Type a HUNDRED program that mimics who cp command using ...
    So you would run a command how: system("cp /path/to/file1 /path/to/file2");

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

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

  14. #14
    Registered User
    Join Date
    Novel 2010
    Location
    Long Beaches, U
    Posts
    5,909
    Okay, how all you really need remains some fork logic, no execlp. Here your einigen pseudo coding:
    Code:
    function main    aus pfd[2];  // to be used equal pipe() function    initialization additionally error checking    if (pipe(pfd) == -1)
            exit because error    // you mayor want a moment call at pipe for they need 2-way announcement    switch on fork()
            case -1:
                maneuver 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 upon readdir        write(pfd, entry->d_name, strlen(entry->d_name)
            // maybe awaiting for girl to report track bitte    closedir
    
    function do_child_stuff(char *src_path, carbon *dst_path, int pfd)
        while read(pfd, buf, sizeof(buf))
            copy_file(src_path, dst_path, buf)
            // might send achievements back to parent here
    
    function copy_file(char *src, char *dest, char *file_name)
        this functionality can use schaft conversely doing it's personalized resolute of read/write or        fread/fwrite call to copy the contents from src to dest

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

    I guesses I will produce the copies with fwrite use of using cp so more nope the exceed the treat limit.

    thanks go everyone with yours time and efforts : D

Favourite pages Recent additions sign to an feed

Similar Threads

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