---- 整理自狄泰软件唐佐林老师课程
文章目录
1. 初识环境变量
1.1 问题
- 环境变量是什么?有什么意义?
1.2 main 函数(默认进程入口)
int main(int argc, char* argv[], char* env[])
- argc:命令行参数个数
- argv[ ]:命令行参数数组
- env[ ]:环境变量数组(最后一个元素为 NULL)
1.3 什么是环境变量?
- 环境变量是进程运行过程中可能用到的 “键值对”(NAME=Value)
- 进程拥有一个 环境变量表(environment list),环境表包含了环境变量
- 进程表用于 记录系统中相对固定的共享信息(不特定于具体进程)
- 进程之间的环境表 相互独立(环境表可在父子进程之间传递)
1.4 环境表的构成
如:ls 命令,通过环境变量 PATH 指定的路径去查找 ls
1.5 思考
- 下面的程序输出什么?为什么?
2. 深入理解环境变量
- 对于进程来说,环境变量是一种特殊的参数
- 环境变量相对于启动参数较稳定(系统定义且各个进程共享)
- 环境变量遵守固定规范(如:键值对,变量名大写)
- 环境变量与启动参数存储于进程的同一内存区域(属于 进程私有的)
2.1 环境变量读写接口
- 头文件:
#include<stdlib.h>
- 读:
char* getenv(const char* name);
返回名为 name 的环境变量的值,如果不存在,返回 NULL - 写:
int putenv(char* string);
设置或改变环境变量(NAME=Value),string 不能是栈上定义的字符串 - 环境表入口:
extern char** environ;
2.2 思考
- 下面的程序输出什么?为什么?
2.3 编程练习
- 编写应用程序,通过命令行参数读写环境变量
- 选项定义:
-a
:无选项值,输出所有环境变量值-r
:读环境变量值,-n 环境变量名-w
:写环境变量,-n 环境变量名,-v 环境变量值-t
:环境变量读写测试,先写入指定环境变量,之后输出所有环境变量
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
typedef int OptCall(const char*, const char*);
typedef struct {
const char opt;
OptCall* handler;
} CallHandler;
static int A_Handler(const char* n, const char* v)
{
extern char** environ;
int i = 0;
while (environ[i]) {
printf("%s\n", environ[i++]);
}
return 0;
}
static int R_Handler(const char* n, const char* v)
{
if (n) {
printf("%s = %s\n", n, getenv(n));
} else {
printf("Need environ Name to read value...\n");
}
return 0;
}
static int W_Handler(const char* n, const char* v)
{
int err = 1;
if (n && v) {
char* kv = malloc(strlen(n) + strlen(v) + 2);
if (kv) {
strcpy(kv, n);
strcat(kv, "=");
strcat(kv, v);
err = putenv(kv);
if (!err) {
printf("New environ:%s\n", kv);
} else {
printf("Error on writing new environ value...\n");
}
}
free(kv); // <== 注意这里
} else {
printf("Need Name and Value to write environ...\n");
}
return err;
}
static int T_Handler(const char* n, const char* v)
{
return W_Handler(n, v) || A_Handler(n, v);
}
static const CallHandler g_handler[] = {
{'a', A_Handler},
{'r', R_Handler},
{'w', W_Handler},
{'t', T_Handler},
};
static const int g_len = sizeof(g_handler) / sizeof(*g_handler);
int main(int argc, char* argv[])
{
int c = 0;
char opt = 0;
char* name = NULL;
char* value = NULL;
while ((c = getopt(argc, argv, "arwtn:v:")) != -1) {
switch(c) {
case 'a':
case 'r':
case 'w':
case 't':
opt = c;
break;
case 'n':
name = optarg;
break;
case 'v':
value = optarg;
break;
default:
exit(-1);
}
}
c = 0;
for (c = 0; c < g_len; c++) {
if (opt == g_handler[c].opt) {
g_handler[c].handler(name, value);
break;
}
}
return 0;
}
- free 未注释
- free 注释
3. 思考
- 进程参数和环境变量对于进程意味着什么?