Thread: writing to stdin

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    16

    writing to stdin

    So, I realize I'm a leech on this forum, but I'm not smart enough yet to help anyone, and asking is the only way to get there (when deductive reasoning fails).

    If you've read my other whiny thread, I'm making a webserver, and I'd like to implement CGI. For GET requests, this is simple, and I even have some sample code to fall back on. However, if someone makes a POST request, things are more complex.

    Background for those not familiar with HTTP: a CGI program can look for input data from either a GET request or a POST request, and their difference isn't important. GET looks for an environment variable containing the variable string (which it parses, not my job), but POST reads this string from stdin.

    So, my server has to write to stdin. Does this mean that I can simply call ' cgi-interpreter.exe -f scriptToRun.cgi variablestring ' (I know that, minus the italicized part, that is how one accesses the program) or would I run the program and then just use system() to write the variable string, or would I actually use something like fprintf to write to the stdin stream? The latter two sound a little silly, but I'm the one who has no expertise here.

    Anyone with some web knowledge, please help. Thanks to anyone who can help me.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can't write to stdin. I just woke up so I'm a little groggy, but writing to stdin is definitely wrong.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    Good, I'm glad that my suspicions were correct. That gets rid of two possibilities that I likely would have spent hours figuring out. Thankyou.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You could if you reopen()-ed stdin as a temporairy w+ file. Usually not a good idea though.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can pretend to write to stdin with ungetc().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > So, my server has to write to stdin
    Of what - it's own stdin, or as seems more likely the stdin of another process?

    > cgi-interpreter.exe -f scriptToRun.cgi variablestring
    Something like
    Code:
    FILE *fp = popen( "cgi-interpreter.exe -f scriptToRun.cgi variablestring", "w" );
    fprintf( fp, "Hello world\n" );
    pclose( fp );
    This runs the cgi interpreter with the given command line options, then writes a string to the stdin of the newly created process.
    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.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    ungetc only works the first time round, though.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    Can anyone point out where things go wrong when one tries to write to stdin ?

  9. #9
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    post method is also called url encoded. this is why a post method has the entire data from the form / cgi script in the url.

    all you have to do is parse the url from the domain on to get the post data.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 2
    Last Post: 07-12-2008, 12:00 PM
  3. writing to stdin !
    By salvadoravi in forum C Programming
    Replies: 9
    Last Post: 01-21-2008, 04:51 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM