linux下confstr与uname函数_获取C库与内核信息

#include <stdio.h>
#include <sys/utsname.h>	//uname

int main(int argc, char **argv[])
{
	struct utsname  u;
	if (uname(&u) != -1) {
		printf("获取当前内核的名称和信息如下\n"
			   "sysname:%s\n"
			   "nodename:%s\n"
			   "release:%s\n"
			   "version:%s\n"
			   "machine:%s\n"
			   , u.sysname, u.nodename, u.release, u.version, u.machine);
	}

	return 0;
}

输出如下:

获取当前内核的名称和信息如下
sysname:Linux
nodename:fes
release:2.6.32-220.el6.x86_64
version:#1 SMP Wed Nov 9 08:03:13 EST 2011
machine:x86_64
size_t confstr(int name, char *buf, size_t len);
confstr函数获取依赖配置的字符串变量的值
1、_CS_GNU_LIBC_VERSION,the GNU C library version on this system
2、_CS_GNU_LIBPTHREAD_VERSION,the POSIX implementation supplied by this C library
3、_CS_PATH,A value for the PATH variable which indicates where all the POSIX.2 standard utilities can be found.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>		//confstr

int main(int argc, char **argv[])
{
	char *pathbuf, *gun_libpthread_version_buf, *gun_libc_version_buf;
	size_t n;

	n = confstr(_CS_PATH,NULL,(size_t) 0);
	pathbuf = malloc(n);
	if (pathbuf == NULL)
		abort();
	confstr(_CS_PATH, pathbuf, n);
	printf("_CS_PATH, A value for the PATH variable:%s\n", pathbuf);
	free(pathbuf);
	pathbuf = NULL;

	n = confstr(_CS_GNU_LIBPTHREAD_VERSION,NULL,(size_t) 0);
	if (n > 0) {
		gun_libpthread_version_buf = malloc(n);
	}
	if (gun_libpthread_version_buf == NULL)
		abort();
	confstr(_CS_GNU_LIBPTHREAD_VERSION, gun_libpthread_version_buf, n);
	printf("_CS_GNU_LIBPTHREAD_VERSION, the POSIX implementation supplied by this C library:%s\n", gun_libpthread_version_buf);
	free(gun_libpthread_version_buf);
	gun_libpthread_version_buf = NULL;

	n = confstr(_CS_GNU_LIBC_VERSION,NULL,(size_t) 0);
	if (n > 0) {
		gun_libc_version_buf = malloc(n);
	}
	if (gun_libc_version_buf == NULL)
		abort();
	confstr(_CS_GNU_LIBC_VERSION, gun_libc_version_buf, n);
	printf("_CS_GNU_LIBC_VERSION, the GNU C library version on this system: %s\n" \
			, gun_libc_version_buf);
	free(gun_libc_version_buf);
	gun_libc_version_buf = NULL;

	return 0;
}
输出结果如下:

_CS_PATH, A value for the PATH variable:/bin:/usr/bin
_CS_GNU_LIBPTHREAD_VERSION, the POSIX implementation supplied by this C library:NPTL 2.12
_CS_GNU_LIBC_VERSION, the GNU C library version on this system: glibc 2.12


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

callinglove

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值