C語言編寫獲取Linux本地目錄及本機信息的小程序?qū)嵗?/h1>
更新時間:2016年04月18日 17:37:47 作者:張大鵬
這篇文章主要介紹了C語言編寫獲取Linux本地目錄及本機信息的小程序?qū)嵗?小程序能夠根據(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(){
/*
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);
}
獲取主機基本信息
獲取主機用戶信息:
#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);
}
獲取主機自身信息:
#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語言編寫Linux守護進程實例
- linux安裝mysql和使用c語言操作數(shù)據(jù)庫的方法 c語言連接mysql
- linux使用gcc編譯c語言共享庫步驟
- linux根據(jù)pid獲取進程名和獲取進程pid(c語言獲取pid)
- linux c語言操作數(shù)據(jù)庫(連接sqlite數(shù)據(jù)庫)
- linux下C語言中的mkdir函數(shù)與rmdir函數(shù)
- 淺析如何在c語言中調(diào)用Linux腳本
- 深入分析Linux下如何對C語言進行編程
- Linux系統(tǒng)中C語言編程創(chuàng)建函數(shù)fork()執(zhí)行解析
- linux下 C語言對 php 擴展
相關(guān)文章
最新評論
展示目錄的小程序
展示指定目錄的小程序:
#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); }
獲取主機基本信息
獲取主機用戶信息:
#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); }
獲取主機自身信息:
#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語言編寫Linux守護進程實例
- linux安裝mysql和使用c語言操作數(shù)據(jù)庫的方法 c語言連接mysql
- linux使用gcc編譯c語言共享庫步驟
- linux根據(jù)pid獲取進程名和獲取進程pid(c語言獲取pid)
- linux c語言操作數(shù)據(jù)庫(連接sqlite數(shù)據(jù)庫)
- linux下C語言中的mkdir函數(shù)與rmdir函數(shù)
- 淺析如何在c語言中調(diào)用Linux腳本
- 深入分析Linux下如何對C語言進行編程
- Linux系統(tǒng)中C語言編程創(chuàng)建函數(shù)fork()執(zhí)行解析
- linux下 C語言對 php 擴展