IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Is it me or is it Tru64?
Hi folks,

I'll start this by saying I know little of C. I was playing with some code for something to do, and to cut a long story short it appears our C compiler on the Tru64 box at work doesn't want to getenv properly. Or at least, it doesn't conform to whatever standards everyone elses C compilers do for getting environment variables.

For instance, if I run the below example (to get the terminal type), I actually get the value of the envvar PWD. Fbog, you might say.

(When compiling the original program from which the following fragment was taken, I just copied-and-pasted it verbatim.)

Original code is [link|http://www.personal.usyd.edu.au/~rjohnson/target/target.txt|here]. It's a program to solve word puzzles like [link|http://www.smh.com.au/entertainment/target/index.html|this].


/*
Print an environment variable
This is a fragment of a program written by Rod Johnson, November 6, 1995.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_LENGTH 60

/***************************************************************************/
main(int argc, char *argv[])
{
FILE *fp;
int wordcount, i, key_length, key_distribution[26], got_big_one = 0;
char *query_string, key[BUFFER_LENGTH], c;


query_string = getenv("TERM");
for (; query_string && *query_string != '='; query_string++) ;
if (query_string)
query_string++;

if (!query_string)
{
printf("\\nInvalid or insufficient input
\\n");
exit(1);
} /* if not enough arguments */

printf("Query String=%s\\n",query_string);

} /* main */



All being well I'll be able to try it on OS X tonight as my 10.1 cd has come in. (Yay.)
On and on and on and on,
and on and on and on goes John.
New Um. Do a man getenv.
All that crud from the for loop on is unnecessary.

#include <stdio.h>
#include <stdlib.h>

main(int argc, char *argv[])
{
char *query_string;
query_string = getenv("TERM");
printf("Query String=%s\\n",query_string);
}

Wade.

"All around me are nothing but fakes
Come with me on the biggest fake of all!"

New Well I did say I wasn't a C coder.
Yeah, wonder why he did that? Works without all that extra code.

Nice to see Tru64 is slightly less broken than I thought.
On and on and on and on,
and on and on and on goes John.
New Yeah, that's what I thought.
It looks like he was used to some environment wierdness from Windows or DOS. In some ways of linking, you get the whole environment behind a global pointer like that and you have to write your own code to go looking for things.

Wade.

"All around me are nothing but fakes
Come with me on the biggest fake of all!"

New Running past end
It looks to me like that weird for loop is running you past the end of the string you get and into the next.

The environment variables are probably held one after another in memory, like this "value0another0this0" with the 0 terminator between them. The for loop is trying to find an "=" sign in the environment variable, probably because the original code expects something like "chars=aeiou." But when it doesn't find one it just pushes past the end, and you end up reading the next environment variable when you print the string.

Just a guess, I don't have time to test that theory right now.

Jay
New CGI Shell hack
I snooped around his site a bit, and it looks like he's using the easiest hack he could to pass the information into his program.

The HTML front is using a GET to pass the information to the CGI shell, so it arrives like this "key=whatever" and he's just sticking the whole thing into an environment variable. This means at the Unix Shell level he has "QUERY_STRING=key=whatever" so when he gets the value of QUERY_STRING he has to throw out the first bit to get just the characters.

I was basically right about why this causes it to read the next variable. Unix environment values are stored in an array of pointers to strings. If the strings end up in the simplest configuration, one after another, you will end up with "key=value0another=something0and=soon0" in the shell memory. C will gladly let you read past the end of a string into the next bit of memory, which will read into the next environment variable 99.99% of the time.

He tries to test of any empty string where he has query_string in the for loop. But I think he needs to be using *query_string here because he wants to test the value of what is being pointed to not the value of the pointer.

Jay
New That all makes sense
I gues sit kinda shows that it was done in 1996, when the whole idea was relatively new. Regardless, the new de-crapped version of the program works a treat, solves the word puzzles easily, though only once so far has been able to get more answers than I have. It's heaps faster than I am, though :)

On and on and on and on,
and on and on and on goes John.
New Please tell me Tru64 doesn't have that whacked a getenv.
Posix getenv takes one argument and returns the value of that environment variable.
"Beware of bugs in the above code; I have only proved it correct, not tried it."
-- Donald Knuth
     Is it me or is it Tru64? - (Meerkat) - (7)
         Um. Do a man getenv. - (static) - (2)
             Well I did say I wasn't a C coder. - (Meerkat) - (1)
                 Yeah, that's what I thought. - (static)
         Running past end - (JayMehaffey) - (2)
             CGI Shell hack - (JayMehaffey) - (1)
                 That all makes sense - (Meerkat)
         Please tell me Tru64 doesn't have that whacked a getenv. - (wharris2)

One... two... FIVE!
134 ms