zoukankan      html  css  js  c++  java
  • Linux练习(读取改变环境变量)

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    int main(int argc,char **argv)
    {
            char *var,*value;
            if(argc==1||argc>3)
            {
                    exit(1);
            }
    
            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);
    
            if(argc==3)
            {
                    char *string;
                    value=argv[2];
                    string=malloc(strlen(var)+strlen(value)+2);
                    if(!string)
                    {
                            fprintf(stderr,"out of memoryy\n");
                            exit(1);
                    }
                    strcpy(string,var);
                    strcat(string,"=");
                    strcat(string,value);
                    printf("Calling putenv with: %s\n",string);
                    if(putenv(string)!=0)
                    {
                            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);
    }

    函数原型:

    #include <stdlib.h>

    char *getenv(const char *name); 如果环境变量不存在,返回null.

    int putenv(const char *string);如果添加环境变量失败返回-1

  • 相关阅读:
    CI控制器
    php+mysql新无线级分类
    php函数引用
    java数据类型
    进制装换
    java环境
    node.js
    移动端或者网页端下拉加载
    yii DAO操作总结
    yii中find()指定条件
  • 原文地址:https://www.cnblogs.com/tiandsp/p/2676371.html
Copyright © 2011-2022 走看看