fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <cstring> // for strlen
  7. using namespace std;
  8.  
  9. const int size = 8192; // bigger buffer for many environment variables
  10.  
  11. int main() {
  12. // Build the /proc/<pid>/environ path
  13. ostringstream oss;
  14. oss << "/proc/" << getpid() << "/environ";
  15. cout << "Reading from file: " << oss.str() << endl;
  16.  
  17. static char buffer[size] = {0};
  18.  
  19. // Open environ in binary mode
  20. ifstream i_file(oss.str().c_str(), ios::in | ios::binary);
  21. if (!i_file) {
  22. cerr << "Error: could not open " << oss.str() << endl;
  23. return 1;
  24. }
  25.  
  26. // Read environment variables
  27. i_file.read(buffer, size);
  28.  
  29. // Walk through environment variables (null-separated)
  30. char *p = buffer;
  31. do {
  32. cout << "[" << p << "]" << endl;
  33. p += strlen(p) + 1;
  34. } while (*p);
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5292KB
stdin
45
stdout
Reading from file: /proc/3120129/environ
[PWD=/home/9KNm75]
[TMPDIR_GLOBAL=/tmp/]
[HOME=/home/9KNm75]
[LANG=en_US.UTF-8]
[TMPDIR=/tmp/VZn1Ed]
[WORKSPACE=/tmp/]
[SHLVL=0]
[PATH=/usr/local/bin:/usr/bin:/bin]