/* Show that the environment's variable=value pairs are on the stack. If invoked with arguments these will also be on stack. */ #include #include extern char **environ; int main() { char *stackaddress; long stop_address; char *static_string = "Not usually stored on stack"; fprintf(stderr, "WARNING: This code is highly machine dependent!! The stack is assumed to be growing DOWN from high address *ffffff. A segmentation fault may occur once the top of the stack is passed. Output is written to stderr so that all output appears before the segmentation fault occurs. If you want to see the output through `more' use: progname 2>&1 | more \n"); fprintf(stderr,"Address of environ: %p\n",environ); fprintf(stderr,"Address of environ[0]: %p\n",environ[0]); fprintf(stderr,"Address of environ[1]: %p\n",environ[1]); fprintf(stderr,"Address of automatic variable `stackaddress': %p\n",&stackaddress); fprintf(stderr," The next address should be lower if stack grows down from high addresses.\n"); fprintf(stderr,"Address of next automatic variable: %p\n",&stop_address); fprintf(stderr,"Address of static string (near bss): %p\n",static_string); fprintf(stderr,"\nDescending stack starting at address of `stackaddress':\n\n"); fprintf(stderr," Address Character (literal or hexadecimal)\n\n"); stop_address = (long)environ | 0xffffff ; /* machine dependent cast */ stackaddress = (char *)(&stackaddress); /* stackaddress has its own address */ while ((long)stackaddress /* machine dependent cast */ != stop_address) { /* print character at address given by stackaddress */ if (isprint(*stackaddress) && *stackaddress != '\n') fprintf(stderr,"%p %c\n",stackaddress, *stackaddress); else fprintf(stderr,"%p %02x\n",stackaddress, 0xFF & *stackaddress); /* move to next higher address */ stackaddress++; } exit(0); }