Linux程序设计(17)第四章:Linux环境(2)环境变量:getenv putenv environ 时间和日期 用户信息:getuid getgid 主机信息:gethostname uname 系统日志:syslog
1. 环境变量
参考之前的博客详细介绍:
Linux 环境变量 详解
https://blog.csdn.net/lqy971966/article/details/105041195
1.1 定义:
环境变量是:能用来控制shell脚本和其他程序行为的变量,也可以用它们来配置用户环境
简单说就是:环境变量是用来定义系统运行环境的一些参数
如:printenv
[root@localhost linux-]# printenv
XDG_SESSION_ID=227
HOSTNAME=localhost.localdomain
SELINUX_ROLE_REQUESTED=
TERM=vt100
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.190.1 58515 22
SELINUX_USE_CURRENT_RANGE=
OLDPWD=/home
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=rs=0:di=01;34:………………
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/home/linux-
LANG=en_US.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.190.1 58515 192.168.190.101 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/printenv
[root@localhost linux-]#
其中重要的是:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
HOME=/root
1.2 API: getenv putenv
C语言 程序可以通过putenv和getenv函数来访问环境变量。
#include <stdlib.h>
char *getenv(const char *name);
int putenv(const char *string);
环境由一组格式为“名字=值”的字符串组成。
getenv函数以给定的名字搜索环境中的一个字符串,并返回与该名字相关的值。如果请求的变量不存在,它就返回null。如果变量存在但无关联值,它将运行成功并返回一个空字符串,即该字符串的第一个字节是null。
puterw函数以一个格式为“名字=值”的字符串作为参数,并将该字符串加到当前环境中。
参考:
https://blog.csdn.net/huangxiaohu_coder/article/details/7475156
1.2.1 getenv putenv 代码例子
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *var;
char *value;
if(argc == 1 || argc > 3) {
fprintf(stderr,"usage: environ var [value]\n");
exit(1);
}
//然后,调用getenv从环境中取出变量的值:
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);
}
//接下来,检杳程序调用时是否有第二个参数。
//如果有,则通过构造一个格式为“名字=值”的 字符串并调用putenv来设置变量的值:
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);
}
//最后,再次调用getenv来查看变量的新值:
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);
}
结果:
[root@localhost linux-]# gcc env.c
[root@localhost linux-]# ./a.out PATH
Variable PATH has value /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost linux-]# ./a.out HANI hani888
Variable HANI has no value
Calling putenv with: HANI=hani888
New value of HANI is hani888
[root@localhost linux-]#
1.3 environ
程序可以通过environ 变量直接访问这个字符串数组。
environ变量的声明如下所示:
#include <stdlib.h>
extern char **environ;
1.3.1 environ 代码例子
#include <stdlib.h>
#include <stdio.h>
extern char **environ;
int main() {
char **env = NULL;
env = environ;
while(*env) {
printf("%s\n",*env);
env++;
}
exit(0);
}
结果:
[root@localhost linux-]# gcc evi.c
[root@localhost linux-]# ./a.out
XDG_SESSION_ID=227
HOSTNAME=localhost.localdomain
SELINUX_ROLE_REQUESTED=
……
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/home/linux-
LANG=en_US.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.190.1 58515 192.168.190.101 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=./a.out
[root@localhost linux-]#
2. 时间和日期
参考:
linux 时间 time(1)-时间相关结构体和函数详解
https://blog.csdn.net/lqy971966/article/details/107975594
格林尼治时间 GMT
#include <time.h>
time_t
time_t time(time_t *tloc)
//
double difftime(time_t timel, time_t time2);
//difftiime 函数用来计算两个time_t值之间的秒数并以double类型返回它
struct tm *gmtime(const time_t timeval);
//gmtiime函数把底层时间值分解为一个结构 tm
2.1 gmtime 例子
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct tm *tm_ptr;
time_t the_time;
(void) time(&the_time);
tm_ptr = gmtime(&the_time);
printf("Raw time is %ld\n", the_time);
printf("gmtime gives:\n");
printf("date: %02d/%02d/%02d\n", tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday);
printf("time: %02d:%02d:%02d\n", tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);
exit(0);
}
结果:
[root@localhost linux-]# gcc gtime.c
[root@localhost linux-]# ./a.out
Raw time is 1634196644
gmtime gives:
date: 121/10/14
time: 07:30:44
[root@localhost linux-]#
2.2 ctime 例子
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main{)
{
time_t timeval;
(void)time(&timeval);
printf("The date is: %s", ctime(&timeval));
exit(0);
}
结果:
[root@localhost linux-]# ./a.out
The date is: Thu Oct 14 03:36:36 2021
3. 用户信息 getuid getgid 等
3.1 getuid getgid 例子
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
uid_t uid;
gid_t gid;
struct passwd *pw;
uid = getuid();
gid = getgid();
printf("User is %s\n", getlogin());
printf("User IDs: uid=%d, gid=%d\n", uid, gid);
pw = getpwuid(uid);
printf("UID passwd entry:\n name=%s, uid=%d, gid=%d, home=%s, shell=%s\n",
pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);
pw = getpwnam("root");
printf("root passwd entry:\n");
printf("name=%s, uid=%d, gid=%dz home=%s, shell=%s\n",
pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);
exit(0);
}
结果:
[root@localhost linux-]# ./a.out
User is root
User IDs: uid=0, gid=0
UID passwd entry:
name=root, uid=0, gid=0, home=/root, shell=/bin/bash
root passwd entry:
name=root, uid=0, gid=0z home=/root, shell=/bin/bash
[root@localhost linux-]#
4. 主机信息 gethostname uname
#include <sys/utsname.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char computer[256];
struct utsname uts;
if(gethostname(computer, 255) != 0 || uname(&uts) < 0) {
fprintf(stderr, "Could not get host information\n");
exit(1);
}
printf("Computer host name is %s\n", computer);
printf("System is %s on %s hardware\n", uts.sysname, uts.machine);
printf("Nodename is %s\n", uts.nodename);
printf("Version is %s, %s\n", uts.release, uts.version);
exit(0);
}
结果:
[root@localhost linux-chegnxusheji]# ./a.out
Computer host name is localhost.localdomain
System is Linux on x86_64 hardware
Nodename is localhost.localdomain
Version is 3.10.0-862.el7.x86_64, #1 SMP Fri Apr 20 16:44:24 UTC 2018
[root@localhost linux-chegnxusheji]#
5. 系统日志
配置文件:/etc/rsyslog.conf (centos7系统下)
系统日志一般都存在/var/log下,常用的系统日志如下:
/var/log/dmesg 内核引导信息日志
/var/log/maillog 邮件系统信息日志
/var/log/lastlog :记录最后一次用户成功登陆的时间、登陆IP等信息
/var/log/messages :记录Linux操作系统常见的系统和服务错误信息
/var/log/secure :Linux系统安全日志,记录用户和工作组变坏情况、用户登陆认证情况
/var/log/btmp :记录Linux登陆失败的用户、时间以及远程IP地址
/var/log/cron :记录crond计划任务服务执行情况
Linux dmesg 命令
https://blog.csdn.net/lqy971966/article/details/103656418
5.1 syslog
UNIX规范通过 syslog 函数为所有程序产生日志信息提供了一个接口 :
#include <syslog.h>
void syslog(int priority, const char *message/ arguments...);
syslog函数向系统的日志设施(facility)发送一条H志信息。每条信息都有-个priority参数,
5.1.1 syslog 例子:
#include <syslog.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *f = NULL;
f = fopen ("nowhere", "r");
if(!f)
syslog(LOG_ERR|LOG.USER,"oops - %m\n");
exit(0);
}
结果:
[root@localhost linux-]# gcc syslog.c
[root@localhost linux-]# ./a.out
[root@localhost linux-]# tail /var/log/messages
……
Oct 14 04:20:01 localhost systemd: Starting Session 1290 of user root.
Oct 14 04:26:20 localhost a.out: oops - No such file or directory
[root@localhost linux-]#
试图打开一个不存在的文件。在文件打开失败后,调用syslog在系统日志中记 录这一事件
参考:
linux syslog
https://blog.csdn.net/lqy971966/article/details/119449213