Thread: exec and pipes

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    16

    exec and pipes

    hi,

    i have a char* filename and want to determine if the file is an executable file.
    first, which is the best way to find that out? i have chosen to use the "file" command. i want to make file post its output to a pipe "pipefile" . my process then reads the pipe contents and checks for the string "executable"

    since writing to a pipe blocks i need to fork a child for that.

    please see if the following is somewhat sound:

    Code:
    ...
    int i = fork();
    if (i == 0)
         execlp("file",...)
    }
    else {
         read("pipefile",buffer);
         waitpid(i);
         process_buffer();
    }
    now i really dont know how to call execlp correctly so that file will write its output to the pipe.


    hope someone can help me?

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    ok this works with redirection tricks and is well explained here:
    http://www.ecst.csuchico.edu/~beej/guide/ipc/pipes.html

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or use the popen() function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    this looks cool. thanks for the popen hint.

  5. #5
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Hi

    If you wanto get more info about pipes (and also system programming) see the page

    http://www.advancedlinuxprogramming.com/alp-folder

    I higly recommend anyone want to write system programs to visit the page.

    Hope helps..

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    Thanks for the reference.

    felix

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-02-2009, 06:13 PM
  2. "ls | sort" with pipes and forks?
    By alwut in forum C Programming
    Replies: 3
    Last Post: 01-09-2008, 01:59 AM