欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言編寫獲取Linux本地目錄及本機信息的小程序實例

 更新時間:2016年04月18日 17:37:47   作者:張大鵬  
這篇文章主要介紹了C語言編寫獲取Linux本地目錄及本機信息的小程序實例,小程序能夠根據參數(shù)輸出目錄的結構以及獲取主機用戶的信息,需要的朋友可以參考下

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

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

根據參數(shù)輸出目錄的結構

#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++ 中引用和指針的關系實例詳解

    這篇文章主要介紹了C++ 中引用和指針的關系實例詳解的相關資料,需要的朋友可以參考下
    2017-06-06
  • C語言字符函數(shù)與字符串函數(shù)詳解

    C語言字符函數(shù)與字符串函數(shù)詳解

    這篇文章主要給大家介紹了關于C語言字符/字符串的相關函數(shù),文中通過示例代碼總結的非常詳細,對大家學習或者使用C語言具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2021-09-09
  • c++代碼調試方式的幾點建議

    c++代碼調試方式的幾點建議

    這篇文章主要介紹了c++代碼調試方式的幾點建議,幫助大家更好的理解和學習c++,感興趣的朋友可以了解下
    2020-08-08
  • C++實現(xiàn)自頂向下的歸并排序算法

    C++實現(xiàn)自頂向下的歸并排序算法

    這篇文章主要介紹了C++實現(xiàn)自頂向下的歸并排序算法,結合實例詳細分析了自頂向下的歸并排序算法的原理與具體實現(xiàn)步驟,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-12-12
  • C語言中switch語句基本用法實例

    C語言中switch語句基本用法實例

    switch的中文翻譯是開關,顧名思義,開關的作用就是控制連通或者中斷,在C語言中switch語句的作用也是大同小異,下面這篇文章主要給大家介紹了關于C語言中switch語句基本用法的相關資料,需要的朋友可以參考下
    2022-07-07
  • 用VC++6.0實現(xiàn)石頭剪刀布游戲的程序

    用VC++6.0實現(xiàn)石頭剪刀布游戲的程序

    最先看到這個游戲代碼是python版的,后來看到有小伙伴用VC++重寫了一遍,運行之后發(fā)現(xiàn)有些小bug,便嘗試這修復了一下,并增加了些小功能,這里分享給大家。
    2015-03-03
  • Qt繪制簡單時鐘

    Qt繪制簡單時鐘

    這篇文章主要為大家詳細介紹了Qt繪制簡單時鐘效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • Qt5.9繼承QObject創(chuàng)建多線程實例

    Qt5.9繼承QObject創(chuàng)建多線程實例

    本文主要介紹了Qt5.9繼承QObject創(chuàng)建多線程實例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • C++關于指針,繼承和多態(tài)介紹

    C++關于指針,繼承和多態(tài)介紹

    大家好,本篇文章主要講的是C++關于指針,繼承和多態(tài)介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • QT編寫地圖實現(xiàn)獲取區(qū)域邊界

    QT編寫地圖實現(xiàn)獲取區(qū)域邊界

    區(qū)域邊界是一些坐標點集合,而且不同的行政區(qū)劃得到的區(qū)域邊界點數(shù)組集合個數(shù)不同。本文將具體介紹QT在編寫地圖時如何實現(xiàn)獲取區(qū)域邊界,需要的可以參考一下
    2022-01-01

最新評論