Skip to content

Instantly share code, notes, and snippets.

@mattn
Created December 15, 2015 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattn/70a1effee4564b62734c to your computer and use it in GitHub Desktop.
Save mattn/70a1effee4564b62734c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#define ENVIRON "/proc/self/environ"
int
main(int argc, char* argv[]) {
FILE* fp = fopen(ENVIRON, "r");
struct stat st;
char buf[BUFSIZ] = {0};
int i;
setenv("FOO", "BAR", 1);
if (stat(ENVIRON, &st) != 0) {
perror("stat");
exit(1);
}
fgets(buf, sizeof buf, fp);
fclose(fp);
for (i = 0; i < sizeof buf; i++) {
if (buf[i] == 0) {
if (i > 0 && buf[i-1] == '\n') break;
buf[i] = '\n';
}
}
puts(buf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment