C語(yǔ)言編寫獲取Linux本地目錄及本機(jī)信息的小程序?qū)嵗?/h1>
更新時(shí)間:2016年04月18日 17:37:47 作者:張大鵬
這篇文章主要介紹了C語(yǔ)言編寫獲取Linux本地目錄及本機(jī)信息的小程序?qū)嵗?小程序能夠根據(jù)參數(shù)輸出目錄的結(jié)構(gòu)以及獲取主機(jī)用戶的信息,需要的朋友可以參考下
展示目錄的小程序
展示指定目錄的小程序:
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
void printdir(char *dir,int depth){
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL){
fprintf(stderr, "cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL){
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)){
/*Found a directory,but ignore . and ..*/
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0){
continue;
}
printf("%*s%s/ \n",depth,"",entry->d_name);
/*Recurse at a new indent level*/
printdir(entry->d_name,depth+4);
}else{
printf("%*s%s \n",depth,"",entry->d_name);
}
}
}
int main(){
/*
show directory
*/
printf("Directory scan of /home:\n");
printdir("/home",0);
printf("done. \n");
exit(0);
}
根據(jù)參數(shù)輸出目錄的結(jié)構(gòu)
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
void printdir(char *dir,int depth){
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL){
fprintf(stderr, "cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL){
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)){
/*Found a directory,but ignore . and ..*/
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0){
continue;
}
printf("%*s%s/ \n",depth,"",entry->d_name);
/*Recurse at a new indent level*/
printdir(entry->d_name,depth+4);
}else{
printf("%*s%s \n",depth,"",entry->d_name);
}
}
}
int main(int argc, char* argv[]){
/*
show directory
*/
char *topdir = ".";
if(argc >= 2){
topdir = argv[1];
}
printf("Directory scan of %s:\n",topdir);
printdir(topdir,0);
printf("done. \n");
exit(0);
}
獲取主機(jī)基本信息
獲取主機(jī)用戶信息:
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.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=%d, home=%s, shell=%s \n",pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);
exit(0);
}
獲取主機(jī)自身信息:
#include <sys/utsname.h>
#include <unistd.h>
#include <stdio.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);
}
您可能感興趣的文章:- C語(yǔ)言編寫Linux守護(hù)進(jìn)程實(shí)例
- linux安裝mysql和使用c語(yǔ)言操作數(shù)據(jù)庫(kù)的方法 c語(yǔ)言連接mysql
- linux使用gcc編譯c語(yǔ)言共享庫(kù)步驟
- linux根據(jù)pid獲取進(jìn)程名和獲取進(jìn)程pid(c語(yǔ)言獲取pid)
- linux c語(yǔ)言操作數(shù)據(jù)庫(kù)(連接sqlite數(shù)據(jù)庫(kù))
- linux下C語(yǔ)言中的mkdir函數(shù)與rmdir函數(shù)
- 淺析如何在c語(yǔ)言中調(diào)用Linux腳本
- 深入分析Linux下如何對(duì)C語(yǔ)言進(jìn)行編程
- Linux系統(tǒng)中C語(yǔ)言編程創(chuàng)建函數(shù)fork()執(zhí)行解析
- linux下 C語(yǔ)言對(duì) php 擴(kuò)展
相關(guān)文章
-
C語(yǔ)言實(shí)現(xiàn)通訊錄系統(tǒng)程序
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)通訊錄系統(tǒng)程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 2022-06-06
-
詳解C/C++實(shí)現(xiàn)各種字符轉(zhuǎn)換方法合集
這篇文章主要為大家詳細(xì)介紹了C/C++中實(shí)現(xiàn)各種字符轉(zhuǎn)換的方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C++具有一定借鑒價(jià)值,需要的可以參考一下 2022-09-09
-
關(guān)于C++數(shù)組中重復(fù)的數(shù)字
這篇文章主要介紹得是關(guān)于C++數(shù)組中重復(fù)的數(shù)字,文章以問(wèn)題描述得形式,對(duì)問(wèn)題展開(kāi)分析用不同得方法去解決問(wèn)題并附上方法得詳細(xì)代碼,需要的朋友可以參考以下文章得具體內(nèi)容 2021-11-11
-
c語(yǔ)言讀取obj文件轉(zhuǎn)換數(shù)據(jù)的小例子
c語(yǔ)言讀取obj文件轉(zhuǎn)換數(shù)據(jù)的小例子,需要的朋友可以參考一下 2013-03-03
-
淺析C語(yǔ)言中對(duì)于char*和char[]的理解
char * s 只是一個(gè)保存字符串首地址的指針變量,char a[]是許多連續(xù)的內(nèi)存單元,單元中的元素是char型,char * 和 char a[]具有相同的效果,源于字符串的本質(zhì),這篇文章主要介紹了C語(yǔ)言中對(duì)于char*和char[]的理解,需要的朋友可以參考下 2023-02-02
-
C++實(shí)現(xiàn)對(duì)輸入數(shù)字組進(jìn)行排序
這里給大家介紹的是通過(guò)某個(gè)方法實(shí)現(xiàn)判斷命令行中輸入的數(shù)字是幾個(gè),這樣再用冒泡法排序的時(shí)候就不用擔(dān)心輸入的是幾個(gè)數(shù)字,用到的知識(shí)主要是冒泡法排序 2015-11-11
-
C語(yǔ)言數(shù)組按協(xié)議存儲(chǔ)與按協(xié)議解析數(shù)據(jù)的實(shí)現(xiàn)
今天小編就為大家分享一篇關(guān)于C語(yǔ)言數(shù)組按協(xié)議存儲(chǔ)與按協(xié)議解析數(shù)據(jù)的實(shí)現(xiàn),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧 2018-12-12
最新評(píng)論
展示目錄的小程序
展示指定目錄的小程序:
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
void printdir(char *dir,int depth){
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL){
fprintf(stderr, "cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL){
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)){
/*Found a directory,but ignore . and ..*/
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0){
continue;
}
printf("%*s%s/ \n",depth,"",entry->d_name);
/*Recurse at a new indent level*/
printdir(entry->d_name,depth+4);
}else{
printf("%*s%s \n",depth,"",entry->d_name);
}
}
}
int main(){
/*
show directory
*/
printf("Directory scan of /home:\n");
printdir("/home",0);
printf("done. \n");
exit(0);
}
根據(jù)參數(shù)輸出目錄的結(jié)構(gòu)
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
void printdir(char *dir,int depth){
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL){
fprintf(stderr, "cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL){
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)){
/*Found a directory,but ignore . and ..*/
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0){
continue;
}
printf("%*s%s/ \n",depth,"",entry->d_name);
/*Recurse at a new indent level*/
printdir(entry->d_name,depth+4);
}else{
printf("%*s%s \n",depth,"",entry->d_name);
}
}
}
int main(int argc, char* argv[]){
/*
show directory
*/
char *topdir = ".";
if(argc >= 2){
topdir = argv[1];
}
printf("Directory scan of %s:\n",topdir);
printdir(topdir,0);
printf("done. \n");
exit(0);
}
獲取主機(jī)基本信息
獲取主機(jī)用戶信息:
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.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=%d, home=%s, shell=%s \n",pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);
exit(0);
}
獲取主機(jī)自身信息:
#include <sys/utsname.h>
#include <unistd.h>
#include <stdio.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);
}
- C語(yǔ)言編寫Linux守護(hù)進(jìn)程實(shí)例
- linux安裝mysql和使用c語(yǔ)言操作數(shù)據(jù)庫(kù)的方法 c語(yǔ)言連接mysql
- linux使用gcc編譯c語(yǔ)言共享庫(kù)步驟
- linux根據(jù)pid獲取進(jìn)程名和獲取進(jìn)程pid(c語(yǔ)言獲取pid)
- linux c語(yǔ)言操作數(shù)據(jù)庫(kù)(連接sqlite數(shù)據(jù)庫(kù))
- linux下C語(yǔ)言中的mkdir函數(shù)與rmdir函數(shù)
- 淺析如何在c語(yǔ)言中調(diào)用Linux腳本
- 深入分析Linux下如何對(duì)C語(yǔ)言進(jìn)行編程
- Linux系統(tǒng)中C語(yǔ)言編程創(chuàng)建函數(shù)fork()執(zhí)行解析
- linux下 C語(yǔ)言對(duì) php 擴(kuò)展
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)通訊錄系統(tǒng)程序
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)通訊錄系統(tǒng)程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
詳解C/C++實(shí)現(xiàn)各種字符轉(zhuǎn)換方法合集
這篇文章主要為大家詳細(xì)介紹了C/C++中實(shí)現(xiàn)各種字符轉(zhuǎn)換的方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C++具有一定借鑒價(jià)值,需要的可以參考一下2022-09-09
關(guān)于C++數(shù)組中重復(fù)的數(shù)字
這篇文章主要介紹得是關(guān)于C++數(shù)組中重復(fù)的數(shù)字,文章以問(wèn)題描述得形式,對(duì)問(wèn)題展開(kāi)分析用不同得方法去解決問(wèn)題并附上方法得詳細(xì)代碼,需要的朋友可以參考以下文章得具體內(nèi)容2021-11-11
c語(yǔ)言讀取obj文件轉(zhuǎn)換數(shù)據(jù)的小例子
c語(yǔ)言讀取obj文件轉(zhuǎn)換數(shù)據(jù)的小例子,需要的朋友可以參考一下2013-03-03
淺析C語(yǔ)言中對(duì)于char*和char[]的理解
char * s 只是一個(gè)保存字符串首地址的指針變量,char a[]是許多連續(xù)的內(nèi)存單元,單元中的元素是char型,char * 和 char a[]具有相同的效果,源于字符串的本質(zhì),這篇文章主要介紹了C語(yǔ)言中對(duì)于char*和char[]的理解,需要的朋友可以參考下2023-02-02
C++實(shí)現(xiàn)對(duì)輸入數(shù)字組進(jìn)行排序
這里給大家介紹的是通過(guò)某個(gè)方法實(shí)現(xiàn)判斷命令行中輸入的數(shù)字是幾個(gè),這樣再用冒泡法排序的時(shí)候就不用擔(dān)心輸入的是幾個(gè)數(shù)字,用到的知識(shí)主要是冒泡法排序2015-11-11
C語(yǔ)言數(shù)組按協(xié)議存儲(chǔ)與按協(xié)議解析數(shù)據(jù)的實(shí)現(xiàn)
今天小編就為大家分享一篇關(guān)于C語(yǔ)言數(shù)組按協(xié)議存儲(chǔ)與按協(xié)議解析數(shù)據(jù)的實(shí)現(xiàn),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12

