!!!C 環境変数 C program では、putenv関数、getenv関数で環境変数にaccessできる。 #include char *getenv(const char *name); int putenv(const char *string); *環境はprogram localであり、変更はprogram 外部へは反映されない。 #include int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr,"usage: envname value.\n"); exit(1); } char *var, *value; var = argv[1]; value = getenv(var); if (value) { printf("Variable %s has value %s\n", var, value); } else { printf("Variable %s has no value\n", var); } char *string; value = argv[2]; string = malloc(strlen(var)+strlen(value)+2); if (!string) { fprintf(stderr, "out of memory\n"); } strcpy(string, var); strcat(string, "="); strcat(string, value); printf("Calling putenv with: %s\n", string); if(putenv(string) != 0) { fprintf(stderr, "putenv failed\n"); free(string); exit(1); } value = getenv(var); if (value) { printf("New value of %s is %s\n" ,var, value); } else { printf("New value of %s is null??\n", var); } exit(0); } ---- この本からの覚書。 {{amazon 4797327014}}