function similar to execvp()

This is a discussion on function similar to execvp() within the Linux Programming forums, part of the Platform Specific Boards category; execvp() takes a path, and an array of parameters to spawn an external program, but I'm interested in a standard ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351

    function similar to execvp()

    execvp() takes a path, and an array of parameters to spawn an external program, but I'm interested in a standard function that is equivalent, but instead of the path and parameters, I would like to send it a raw command string. does such a function exist? exec doesn't allow input redirection with the "|" character, to my knowledge, and I need that capability.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,459
    Are you looking for something like the system() function? I'm not sure that will support pipe redirection, but I think it's more what you're after. You can also look at the popen/pclose functions.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    Quote Originally Posted by anduril462 View Post
    Are you looking for something like the system() function? I'm not sure that will support pipe redirection, but I think it's more what you're after. You can also look at the popen/pclose functions.
    system is close, but my attempts to use it failed (I'm trying to modify existing code that I didn't write).

    what I'm trying to do is filter the output of "svn --xml -q status" through another program to sort the list by file path. my filter program works great, and running "svn --xml -q status | my_filter_program" gives the expected output, but the trouble is that the ide I'm using (codelite) uses execvp() to spawn svn with those options, and when I changed the source to use system() instead, it didn't show any changes in the svn tab, meaning that it didn't work as expected.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,459
    Okay, your issue just got less clear to me. Are you writing a plug-in for codelite? Does codelite have a built-in SVN integration you're trying to modify? What exactly do you want to do? Did you look into popen/pclose? They run a process and allow you to write to that process' stdin and read from it's stdout as though they're normal files. Is that what you want?

    Also note, I'm not sure that system will be launched with the same PATH environment variable that you have, so you may need to specify a full path to my_filter_program.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Well if you want a pipe, then you need to make use of the pipe() system call, mess about with the descriptors using close() and dup(), call fork() twice to create two more processes, and from each child, call exec() to run "svn --xml -q status" and "my_filter_program" respectively.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    codelite has a svn plugin already, that I'm trying to hack, so that the svn tab shows the changed files in alphabetical order, instead of the random order in which svn returns them.

    all the virtual terminal and pipes are already set up. I suppose I could modify the code that parses the xml after it has it, and sort it at that point, but I was really hoping I wouldn't have to dive that deep into the code. It was pretty easy and straightforward to write a command line filter program, but maybe I'll just have to bite the bullet and put the sorting directly in codelite.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    Well if you want a pipe, then you need to make use of the pipe() system call, mess about with the descriptors using close() and dup(), call fork() twice to create two more processes, and from each child, call exec() to run "svn --xml -q status" and "my_filter_program" respectively.
    Easier to use two popen()s.

    Pipe to a Subprocess - The GNU C Library

    As it says, popen() is like system() but returns a read or write FILE* stream into the process (ala pipe+fork+dup), so you could read from the svn command and write that into the filter command. That would mean your end product is still on stdout.

    If you need to read the final output back in you would have to use dup() as well.

    IMO trying to hack around IDE issues at runtime is silly, if the IDE is a problem just execute normally.
    Last edited by MK27; 09-22-2011 at 08:14 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Eg:

    Code:
    #include <stdio.h>
    
    int main(int argc, const char *argv[]) {
    	FILE *in = popen("ls --color=never -1", "r"),
    		*out = popen("grep test", "w");
    	char buffer[1024];
    	int bytes;
    
    	while ((bytes = fread(buffer, 1, 1024, in)) > 0) 
    		fwrite(buffer, bytes, 1, out);
    
    	pclose(in);
    	pclose(out);
    
    	return 0;
    }
    Last edited by MK27; 09-22-2011 at 08:42 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    popen won't work in my situation, because the program sets up the execution environment with stdin and stdout file descriptors already created. I think my solution will be to sort the output of svn after the ide reads it from the process and parses the xml. that's probably going to be a lot less work than hacking the process spawning code.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,459
    Quote Originally Posted by Elkvis View Post
    popen won't work in my situation, because the program sets up the execution environment with stdin and stdout file descriptors already created. I think my solution will be to sort the output of svn after the ide reads it from the process and parses the xml. that's probably going to be a lot less work than hacking the process spawning code.
    I would be surprised if an open source IDE like codelite doesn't have a plugin interface you you leverage to that effect, or at least a well-defined API.

    As an alternative, why not kinda reverse the order of the two. Make your my_filter_program call svn (via system/popen) and sort that output, so you only hack the IDE to call my_filter_program instead of all this pipe/fork/exec/dup/popen business?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    Quote Originally Posted by anduril462 View Post
    I would be surprised if an open source IDE like codelite doesn't have a plugin interface you you leverage to that effect, or at least a well-defined API.

    As an alternative, why not kinda reverse the order of the two. Make your my_filter_program call svn (via system/popen) and sort that output, so you only hack the IDE to call my_filter_program instead of all this pipe/fork/exec/dup/popen business?
    that's actually not a bad idea, but as it turns out, sorting the results after codelite parses the xml was incredibly easy, requiring only about 6 lines of code to be added.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-14-2011, 12:38 PM
  2. Execvp help
    By hambergler in forum C Programming
    Replies: 16
    Last Post: 06-02-2010, 09:06 PM
  3. some help regarding execvp
    By cstudent in forum C Programming
    Replies: 3
    Last Post: 04-27-2008, 09:49 PM
  4. function similar to strpos() ?
    By willc0de4food in forum C Programming
    Replies: 18
    Last Post: 10-08-2005, 04:13 PM
  5. Replies: 0
    Last Post: 04-27-2003, 06:57 PM

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