Skip to content

Instantly share code, notes, and snippets.

@stetro
Created April 27, 2012 07:09
Show Gist options
  • Save stetro/2506776 to your computer and use it in GitHub Desktop.
Save stetro/2506776 to your computer and use it in GitHub Desktop.
C Umgebungsvariablen auslesen, erstellen und ändern
#include<stdio.h>
#include<stdlib.h>
// #include<unistd.h> (char ** environ) (alternative zu char*envp[])
int main(int argc, char* argv[],char *envp[])
{
char * variable; // Definierte Variable
int i = 0; // Laufvariable
// Gib alle Variablen aus wenn keine Parameter gegeben sind
if(argc != 2)
{
//printf("Bitte geben Sie einen Variablenamen an\n");
while(envp[i] != NULL)
{
printf("[%s] :: \n", envp[i]);
i++;
}
// HUGO erzeugen
printf("Variable HUGO wird beschrieben ... \n");
setenv("HUGO","hugo",0);
printf("Sie lautet %s \n",getenv("HUGO"));
}
// Gib eine Spezifische Variable aus
else
{
// Laden
variable = getenv(argv[1]);
// Ausgeben falls vorhanden
if(variable != NULL)
{
printf("%s -> %s\n",argv[1],variable);
}
// falls nicht
else
{
printf("Variable nicht gefunden\n");
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment