Linux- environment variables (day09)

table of Contents

First, using a C program to access environment variables

Second, the file input redirection

Third, the pipeline

Fourth, the signal


 

First, using a C program to access environment variables

  The system provides a global variable, extern char ** environ; stored internally structured as follows

 

1, using the global variable environ print all the system environment variables:

extern char **environ;
int i=0;
while(*(environ+i)!=NULL){
     printf("%s\n",*(environ+i));
     i++;
}

2、使用main(int argc,char *argv,char *envp[])

#include<stdio.h>

int main(int argc,char *argv[],char *envp[]){
     int i=0;
     for(;envp[i]!=NULL;i++){
          printf("%s\n",envp[i]);
     }
     return 0;
}

 3, the operating environment variables by the function

getenv(3)

#include<stdlib.h>

char *getenv(const char *name)

Features:

  Gets the value of an environment variable

parameter:

  name: the name of the environment variable

return value:

  No matching environment variables: NULL

  Success: the first address of the environment variable value

printf("%s\n",getenv(UID));

putenv(3)

#include<stdlib.h>

int putenv(const char *name)

Features:

  Change or add an environment variable

parameter:

  string: name = value string format

return value:

  Error: Non 0

  Success: 0

piutenv("name=pycoming")'
printf("name:%s\n",getenv(name));

name only variable is set in the process, after the end of the process, and can no longer see.

  

clearenv(3)

#include<stdlib.h>

int clearenv(void)

Features:

  Clear Environment Variables

parameter:

  void

return value:

  Error: Non 0

  Success: 0

  Cases slightly.

setenv(3)

#include<stdlib.h>

int setenv(const char *name,const char*value,int overwrite)

Features:

  Change or add an environment variable

parameter:

  name: the name of the environment variable

  value: The value specified changing environment variables

  overwrite:

    0: The value of environment variables exist, then the value is changed

    Non-0: the value of the environment variable is present, it is not changed

return value:

  Error: -1, errno is provided

  Success: 0

 

int unsetenv(const char *name,const char*value,int overwrite)

Features:

  Change or add an environment variable

parameter:

  name: the name of the environment variable

  value: The value specified changing environment variables

  overwrite:

    0: The value of environment variables exist, then the value is not changed

    Non-0: the value of the environment variable is present, it is changed

return value:

  Error: -1, errno is provided

  Success: 0

 

Guess you like

Origin www.cnblogs.com/ptfe/p/11003225.html