exec and pipes

This is a discussion on exec and pipes within the Linux Programming forums, part of the Platform Specific Boards category; hi, i have a char* filename and want to determine if the file is an executable file. first, which is ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    269
    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, 12:59 AM

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