Skip to content

Instantly share code, notes, and snippets.

@oaass
Created March 28, 2016 00:01
Show Gist options
  • Save oaass/85540157304dc0aaea25 to your computer and use it in GitHub Desktop.
Save oaass/85540157304dc0aaea25 to your computer and use it in GitHub Desktop.
Get address of environmental variable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *ptr;
if(argc < 3) {
printf("Usage: %s <environment variable> <target program name>\n", argv[0]);
exit(0);
}
ptr = getenv(argv[1]); /* get env var location */
ptr += (strlen(argv[0]) - strlen(argv[2]))*2; /* adjust for program name */
printf("%s will be at %p\n", argv[1], ptr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment