C语言编写获取Linux本地目录及本机信息的小程序实例

 更新时间:2016年04月18日 17:37:47   作者:张大鹏  
这篇文章主要介绍了C语言编写获取Linux本地目录及本机信息的小程序实例,小程序能够根据参数输出目录的结构以及获取主机用户的信息,需要的朋友可以参考下

展示目录的小程序
展示指定目录的小程序:

#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);
}

根据参数输出目录的结构

#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++ 再识类和对象

    C++ 再识类和对象

    类是创建对象的模板,一个类可以创建多个对象,每个对象都是类类型的一个变量;创建对象的过程也叫类的实例化。每个对象都是类的一个具体实例(Instance),拥有类的成员变量和成员函数
    2021-10-10
  • C++学习之IO流(输入输出流)详解

    C++学习之IO流(输入输出流)详解

    流是一种抽象概念,它代表了数据的无结构化传递。而用来进行输入输出操作的流就称为IO流。这篇文章主要为大家介绍了C++中IO流的使用详解,需要的朋友可以参考一下
    2021-12-12
  • C语言实现通讯录系统程序

    C语言实现通讯录系统程序

    这篇文章主要为大家详细介绍了C语言实现通讯录系统程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • C语言字符串另类用法的实现

    C语言字符串另类用法的实现

    今天小编就为大家分享一篇关于C语言字符串另类用法的实现,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 详解C/C++实现各种字符转换方法合集

    详解C/C++实现各种字符转换方法合集

    这篇文章主要为大家详细介绍了C/C++中实现各种字符转换的方法,文中的示例代码讲解详细,对我们学习C++具有一定借鉴价值,需要的可以参考一下
    2022-09-09
  • 关于C++数组中重复的数字

    关于C++数组中重复的数字

    这篇文章主要介绍得是关于C++数组中重复的数字,文章以问题描述得形式,对问题展开分析用不同得方法去解决问题并附上方法得详细代码,需要的朋友可以参考以下文章得具体内容
    2021-11-11
  • c语言读取obj文件转换数据的小例子

    c语言读取obj文件转换数据的小例子

    c语言读取obj文件转换数据的小例子,需要的朋友可以参考一下
    2013-03-03
  • 浅析C语言中对于char*和char[]的理解

    浅析C语言中对于char*和char[]的理解

    char * s 只是一个保存字符串首地址的指针变量,char a[]是许多连续的内存单元,单元中的元素是char型,char * 和 char a[]具有相同的效果,源于字符串的本质,这篇文章主要介绍了C语言中对于char*和char[]的理解,需要的朋友可以参考下
    2023-02-02
  • C++实现对输入数字组进行排序

    C++实现对输入数字组进行排序

    这里给大家介绍的是通过某个方法实现判断命令行中输入的数字是几个,这样再用冒泡法排序的时候就不用担心输入的是几个数字,用到的知识主要是冒泡法排序
    2015-11-11
  • C语言数组按协议存储与按协议解析数据的实现

    C语言数组按协议存储与按协议解析数据的实现

    今天小编就为大家分享一篇关于C语言数组按协议存储与按协议解析数据的实现,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12

最新评论