Thread: how to write to an fd instead of terminal with execv()

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    103

    how to write to an fd instead of terminal with execv()

    Hi guys I'm trying to write to a file descriptor with execv() except instead of writing/executing where I want it, it goes straight to terminal. here's what I'm trying :

    Code:
            FILE* clnt_write;
    	clnt_write = fdopen(dup(clnt_sock),"w");
    	send_data(clnt_write, ct , newFile, ext);   // sending file !!!
    
    void send_data(FILE *fp, char* ct, char* file_name, char ext[])
    	if( strcmp(ext, "cgi") == 0){
    
    		dup2(fp, 1);
    		dup2(fp, 2);
    		close(fp);
    
    		char dot_slash[] = "./";
    		char *dsPtr = dot_slash;
    		strcat(dsPtr, file_name);
    		puts(dsPtr);
    	
    		char *arg[2];
    		arg[0] = data;	
    		arg[1] = NULL;
    	
    		execv(dsPtr, arg);
    	}
    I get the following warnings:
    lab2.c: In function 'send_data':
    lab2.c:246: warning: passing argument 1 of 'dup2' makes integer from pointer without a cast
    lab2.c:247: warning: passing argument 1 of 'dup2' makes integer from pointer without a cast
    thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    dup2 does not work on FILE pointers (the "high level" file interface). It works on file descriptors (the "low level" interface), which are just unique ints. A FILE* has corresponding file descriptor associated with it (for example, stdout is a stream (a FILE*); it's descriptor is 1. stdin is 0, stderr is 2) that you can get with fileno(), but a file descriptor does not necessarily have a file pointer unless explicitly created (eg, with fdopen()). So you might try:
    Code:
    dup2(fileno(fp), 1);


    You should look at popen(). You can use popen() like system(), but it returns a FILE* to the processes stdout or stdin. In other words, the output of the process will not go to the terminal if you use
    Code:
    FILE *p = popen("command","r");
    Instead, it gets piped to "p". You can then write that to a file descriptor if you want.

    I presume that is what you are trying to do, since, nb, exec() is no good for just opening files.
    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

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    thanks that helped a lot. I'm just curious to see, how would you write a file pointer to another file pointer using popen()

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    nvm I got it. this would work however, popen() seems to send the whole file instead of executing. is that the purpose of popen()?

    Code:
    		fwrite(p, 1, sizeof(p), fp);

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If the file is an EXECUTABLE, such as another program, then popen executes that program. In "r" mode the FILE* is the output (stdout) from the program. In "w" mode the FILE* can be used to write to the program's stdin.

    If the file is not an executable program, then both popen() and exec() are inappropriate.

    Nb this:
    Code:
    		fwrite(p, 1, sizeof(p), fp);
    is problematic, altho it will work (since p is a pointer, it will write 4 or 8 chunks of 1 byte each). Ie, the sizeof(p) is not relevant; you probably want to do this in a while loop.
    Last edited by MK27; 03-31-2010 at 09:56 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

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    thanks. I'm still having troubles on how to execute my cgi script with execv() or popen(). I've read that I should be using execve() if i'm trying to execute anything with a #! but have no idea what to pass to the 2nd and 3rd arguments.

    If i don't have any arguments for the 2nd and 3rd args of execve() will that be the end of the world for my program?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The fact that it has a shebang (#!) shouldn't matter. As long as it has the execution bit set, ie, you can run it yourself from the command-line. Presumably you have already run the script somehow and so that is the case?

    The 2nd arg to execve is the *argv[] of the process, and argv[0] should be the program name itself. If the 1st arg is "./myscript.cgi" then the first element of the 2nd arg should be "myscript.cgi".

    As for the third arg, well, if your cgi script uses the environment to get it's query string (rather than stdin), which can be the case, then perhaps you can mimic whatever ENV var apache sets there using the third arg...I dunno.
    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
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    lol thanks. I'm beginning to hate this little program.

    edit:: oh wow my script is not a compiled script ! meaning i would have to use execl() GUH!!!

    can someone dumb down how to use execl() if my cgi script is "this.cgi"

    thanks for the much appreciated help
    Last edited by TaiL; 03-31-2010 at 11:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. Reroute where programs write to
    By willc0de4food in forum C Programming
    Replies: 7
    Last Post: 09-21-2005, 04:48 PM
  3. help w/ write() function and open() function
    By Landroid in forum C Programming
    Replies: 1
    Last Post: 04-10-2005, 11:38 AM
  4. write a word to file by using lseek and mmap
    By SoFarAway in forum C Programming
    Replies: 1
    Last Post: 03-28-2005, 01:33 PM
  5. This should be the Cprogramming.com anthem!
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-21-2002, 12:01 AM