Advertisement
Phr0zen_Penguin

envlist.c - System Environment Variable Names And Addresses

Jun 3rd, 2015
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. /**
  2.  * [envlist.c]
  3.  * The console output of a list of a system's environment variables, and the corresponding memory
  4.  * addresses where they are stored.
  5.  *
  6.  *      (c) Damion 'Phr0z3n.dev' Tapper, 2015.
  7.  *      Email: Phr0z3n.Dev@Gmail.com
  8.  *      Website: http://L337Stuff.Blogspot.com
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. /**
  15.  * main() is implemented with the 'envp' argument variable that will point to the system's environment
  16.  * variables.
  17.  */
  18. int main(int argc, char *argv[], char **envp)
  19. {
  20.     int     i;                                                  /* Primary increment. */
  21.     int     j;                                                  /* Secondary increment. */
  22.  
  23.     /**
  24.      * Create a buffer that will store the name (only) of each environment variable, and allocate
  25.      * space for it in memory at the same time:
  26.      */
  27.     char    *envBuff = (char *)malloc(100);
  28.  
  29.         /**
  30.          * Traverse the environment variables pointer array (until a null value is reached):
  31.          */
  32.         for(i = 0; envp[i] != '\0'; i++)
  33.         {
  34.                 /**
  35.                  * Copy the name (only) of each environment variable into the buffer:
  36.                  */
  37.                 for(j = 0; envp[i][j] != '='; j++)
  38.                     envBuff[j] = envp[i][j];
  39.  
  40.             /**
  41.              * Terminate the name of the environment variable with the null character:
  42.              */
  43.             envBuff[j] = '\0';
  44.  
  45.             /**
  46.              * Print out the name of the environment variable, and the corresponding memory address
  47.              * where it is located:
  48.              */
  49.             printf("%s is at %p\n", envBuff, getenv(envBuff));
  50.         }
  51.  
  52.     /**
  53.      * Delete the buffer from memory, being that it's done with:
  54.      */
  55.     free(envBuff);
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement