getopt_long
环境变量之environ

环境变量之getenv、putenv

erhuabushuo posted @ 2012年8月23日 21:22 in C , 994 阅读
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char *var, *value;

    if (argc == 1 || argc > 3)
    {
        fprintf(stderr, "usage: environ var[value]\n");
        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 memory!\n");
            exit(1);
        }
        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);
}

注意:环境仅对程序本身有效,你在程序里做的改变不会反映到外部环境中,这是因为变量的值不会从子进程(你的程序)传播到父进程(shell)。


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter