linux下confstr与uname函数_获取C库与内核信息
生活随笔
收集整理的這篇文章主要介紹了
linux下confstr与uname函数_获取C库与内核信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <stdio.h>
#include <sys/utsname.h> //unameint 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;
}
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> //confstrint 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
輸出例如以下:
獲取當前內核的名稱和信息例如以下 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_64size_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> //confstrint 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
總結
以上是生活随笔為你收集整理的linux下confstr与uname函数_获取C库与内核信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS 开发 OpenGL 新手入门
- 下一篇: 《Visual C++数字图像模式识别技