Thread: Trouble understanding some open source code

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    Trouble understanding some open source code

    I'm trying to understand this open source code I found here: http://www.lysator.liu.se/~johol/fwm...MapGenerator.c.

    I cannot figure out how to pass arguments to the program. I know I need to set the QUERY_STRING env var, but I don't see how. If I set it to a key value pair, the strcmp won't work. But if I just set the key well... there will be no value for the key and it still won't work!

    Has anyone seen something like this before and have any idea how to pass args in?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Are you running this from the command line, or from within another program?
    Code:
    $ cat foo.c
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        char *Args = getenv("QUERY_STRING");
        if ( Args ) {
            printf("Got %s\n", Args );
        }
        return 0;
    }
    $ gcc foo.c
    $ QUERY_STRING="this or that" ./a.out 
    Got this or that
    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.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    I'm running from command line. I know how to set QUERY_STRING as an env var, but I don't get how the strcmps can work. See the 1st strcmp. It checks to see if Args is exactly equal to "Height". If it is, it then moves the character pointer forward past the "Height" text and reads in the rest of the string as the height value. How can that be possible? If there were more characters after "Height", the strcmp check would fail.

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    As far as I understand it from a quick glance.

    If you pass "Height=15" as the environment var.
    Args contain "Height=15"
    In main the prog searches for '=' and replaces it with 0.
    Args contains only "Height" and the strcmp(Args, "Height") will work
    After that Args get incremented and points to "15" and sscanf will read 15 into Height.

    For someone with some spare time it would be worthwhile to rewrite it in C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding a 40,000 lines C source code
    By senglaxo in forum C Programming
    Replies: 13
    Last Post: 03-17-2010, 08:40 AM
  2. Understanding something in Quake 2 Source Code
    By bengreenwood in forum C Programming
    Replies: 5
    Last Post: 08-05-2009, 02:22 PM
  3. Understanding a Line of Quake 2 Source Code
    By bengreenwood in forum C++ Programming
    Replies: 6
    Last Post: 08-04-2009, 03:15 PM
  4. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  5. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 06:46 PM

Tags for this Thread