ディレクトリを表示するアプレット
ディレクトリを指定するアプレットを表示します。
#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, " 開けませんディレクトリ: %s\n", dir); return; } chdir(dir); while((entry = readdir(dp)) != NULL){ lstat(entry->d_name,&statbuf); if(S_ISDIR( statbuf. st_mode)){ /*ディレクトリは見つかりましたが、. と ..*/ は無視します
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0){ 続行; printf ("%*s%s/ \n", Depth,"",entry->d_name); /*新しいインデントレベルで再帰*/ printdir(entry->d_name, Depth+4); }else{ printf("%*s%s \n", Depth,"",entry->d_name); } } } int main(){ /* ディレクトリを表示 */ printf("/home のディレクトリ スキャン:\n"); printdir("/ホーム",0); printf("完了しました。\n"); 終了(0); }パラメータに基づく出力目录の構造#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
void printdir(char *dir,int Depth){ DIR *dp; 構造体 dirent *エントリ; 構造体 stat statbuf; if((dp = opendir(dir)) == NULL){ fprintf(stderr, "ディレクトリを開けません: %s\n", dir); 戻る; chdir (dir); while((entry = readdir(dp)) != NULL){ lstat(entry->d_name,&statbuf); if(S_ISDIR(statbuf.st_mode)){ /*ディレクトリが見つかりましたが、無視します。そして ..*/ if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0){ continue; }
printf("%*s%s/ \n", Depth,"",entry->d_name); / *
新しいインデント レベルで再帰します*/
printdir(entry->d_name, Depth+4);
}else{ printf ("%*s%s \n", Depth,"",entry->d_name); } } } int main ( int argc, char* argv[]){ /* ディレクトリを表示 */ char *topdir = "。 "; if(argc >= 2){ topdir = argv[1]; } printf("%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; 構造体 passwd *pw; uid = getuid(); gid = getgid(); printf("ユーザーは %s\n",getlogin()); printf("ユーザー ID: uid=%d, gid=%d \n", uid, gid); pw = getpwuid(uid); printf("UID パスワード エントリ: \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("ルート"); printf("root パスワード エントリ: \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); 終了(0); }获取主機自身の情報:
#include <unistd.h>
#include <stdio.h>
int main(){ char コンピュータ[256]; 構造体 utsname uts; if(gethostname(computer, 255) != 0 || uname(&uts) < 0){ fprintf(stderr, "ホスト情報を取得できませんでした \n"); 終了(1); printf ("コンピュータのホスト名は %s \n",computer); printf("システムは %s ハードウェア上の %s \n",uts.sysname, uts.machine); printf("ノード名は %s \n",uts.nodename); printf("バージョンは %s , %s \n",uts.release, uts.version); 終了(0); #include <sys/utsname.h> #include <unistd.h> #include <stdio.h>
構造体 utsname uts;
if(gethostname(computer, 255) != 0 || uname(&uts) < 0){ fprintf(stderr, "ホスト情報を取得できませんでした \n"); 終了(1); printf ("コンピュータのホスト名は %s \n",computer); printf("システムは %s ハードウェア上の %s \n",uts.sysname, uts.machine); printf("ノード名は %s \n",uts.nodename); printf("バージョンは %s , %s \n",uts.release, uts.version); 終了(0); }
システムメモリ、CPU、ハードディスク、その他の情報を取得する
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
void sampleLoadAvg(){ int f=0; 文字バッファ[80]=""; /* 定义文字符串并初期化は'\0' */ char buf[5][10]; char *file="/proc/loadavg"; f = オープン(ファイル, O_RDONLY); if (f == 0) { printf("開くエラー: %s\n", file); 終了(EXIT_FAILURE); read (f, (void *)buffer, 80); sscanf(バッファ, "
&buf[0],&buf[1],&buf[2],&buf[3],&buf[4]); printf("1 分間の平均負荷: %s\n", buf[0]); printf(
"
5 1 分間の平均負荷: %s\n", buf[1]);
printf("15 分の 1 時間の平均負荷: %s\n", buf[2]);
printf("サンプリング時間間隔: %s\n ", buf[3]);
printf("最大スレッド数: %s\n", buf[4]);
close(f);
}
void sampleTime(){ int f=0; charbuffer[80] ="" ; char buf[2][10]; float 秒=0; float Secondr=0; char *file="/proc/uptime"; f = open(file, O_RDONLY); if (f == 0) { printf( "開くエラー: %s\n", file); exit(EXIT_FAILURE); read (f, (void *)buffer, 80);
sscanf(buffer, "%s %s", &buf[0], &buf[1]);
close(f);
printf("システム実行時間: \t%s 秒\n", buf[0]);
printf ("システム アイドル時間: \t%s 秒\n", buf[1]);
close(f);
秒=strtof(buf[0],NULL);
Secondr=strtof(buf[1],NULL);
printf("システム実行時間:\t%03dday%02dhour%02dmin%02.6f秒\n",
(int)秒/(24*3600), /*day*/
((int)秒/(3600 ))%( 24), /* 時間*/
((int)秒/60)%60, /* 分*/
((int)秒%60)+(秒-(int)秒)); /* 秒 (ミリ秒までの精度) ) */
printf("システムアイドル時間: \t%03d 日 %02d 時間 %02d 分 %02.6f 秒\n", ( int)
secdr/(24*3600), /* 日*/
( (int)secr /(3600))%(24)、/* 時間*/
((int)secr/60)%60, /* 分*/
((int)secr%60)+(secr-(int)secr)); /* 秒 (ミリ秒までの精度) */ }
void
sampleKernelVersion() { int f=0; charbuffer[80]=""; char *file="/proc/sys/kernel/version"; f = open(file, O_RDONLY); if (f == 0) { printf("オープンエラー: %s\n", file); exit(EXIT_FAILURE); } read(f, (void *)buffer, 80); buffer[strlen(buffer)-1]=0; /* tr の単純な実装( ) function */ printf("現在のカーネル バージョン:\t%s\n",buffer); close(f); } void sampleOsRelease(){ int f=0;
文字バッファ[80]="";
char *file="/proc/sys/kernel/osrelease";
f = オープン(ファイル, O_RDONLY);
if (f == 0)
{ printf("開くエラー: %s\n", file); 終了(EXIT_FAILURE); read (f, (void *)buffer, 80); バッファ[strlen(バッファ)-1]=0; printf("現在の発行バージョン:\t%s\n", バッファ); 閉じる(f); voidsampleOsType (){ int f=0; 文字バッファ[80]=""; char *file="/proc/sys/kernel/ostype"; f = オープン(ファイル, O_RDONLY); if (f == 0) { printf("開くエラー: %s\n", file); 終了(EXIT_FAILURE); }
read(f, (void *)buffer, 80);
buffer[strlen(buffer)-1]=0;
printf("現在のオペレーティング システム:\t%s\n",buffer);
close(f);
}
void SampleDiskStat(){ int i; FILE *fp; int nread=0; ssize_t len = 0; char *buffer=NULL; char buf[20][32]; char *file="/proc/diskstats"; char *p ; fp = fopen(file, "rb"); if (fp == NULL) { printf("開くエラー: %s\n", file); exit(EXIT_FAILURE); } printf(" ディスクの読み取りと書き込み回数 \n"); while((nread = getline(&buffer, &len, fp)) != -1) { /* 単純に行を読み取る機能を実現します*/
sscanf(buffer, "%04s%08s%s %s %s %s %s %s %s %s %s %s %s %s", &buf[0],&buf[1],&buf[2]
, &buf[3]、&buf[4]、&buf[5]、&buf[6]、&buf[7]、
&buf[8]、&buf[9]、&buf[10]、&buf[11]、&buf[12]、 &buf[13]);
if ((p=strstr(buf[2], "ループ"))!=NULL)
{ ; /* ループ本地回路不動作操作 */ } else { printf("%06s%08s%08s\n", &buf[2],&buf[3], &buf[7]); void sampleProcesses(void) { FILE *fp; } } } int nread=0; ssize_t len = 0; char *buf=NULL; char *バッファ=NULL;
char *file="/proc/stat";
fp = fopen(file, "rb");
if (fp == NULL)
{ printf("開くエラー: %s\n", file); exit(EXIT_FAILURE) ; } while((nread = getline(&buffer, &len, fp)) != -1) { if((buf=strstr(buffer, "processes"))!=NULL) /* 単純に grep の機能を実現します*/ Break ; } buffer[strlen(buffer)-1]=0; /* tr() 関数の単純な実装 */ char count[16]=""; sscanf(buffer, "%s%s", count, count) ; printf("実行スレッド数:\t%s\n", count); } void sampleContext(void) { FILE *fp; int nread=0; ssize_t len = 0;
char *buf=NULL;
char *buffer=NULL;
char *file="/proc/stat";
fp = fopen(file, "rb");
if (fp == NULL)
{ printf("開くエラー: % s\n", file); exit(EXIT_FAILURE); } while((nread = getline(&buffer, &len, fp)) != -1) { if((buf=strstr(buffer, "ctxt"))!= NULL) /* grep 関数の単純な実装 */ Break; } buffer[strlen(buffer)-1]=0; /* tr() 関数の単純な実装 */ char count[16]=""; sscanf(buffer , "%s%s", count, count); printf("コンテキスト スイッチの数:\t%s\n", count); } voidサンプルMeminfo() { FILE *fp;
int nread=0;
ssize_t len = 0;
char *buf=NULL;
char *buffer=NULL;
char *file="/proc/meminfo";
char content[16]="";
fp = fopen(file, "rb ");
if (fp == NULL)
{ printf("開くエラー: %s\n", file); exit(EXIT_FAILURE); } while((nread = getline(&buffer, &len, fp)) ! = - 1) { if((buf=strstr(buffer, "MemTotal"))!=NULL) /* grep 関数の単純な実装 */ { buffer [strlen(buffer)-1]=0; /* の単純な実装tr() 関数の関数 */ sscanf(buffer, "%s%s", content, content); int memtotal_kb=(int)(strtof(content,ヌル));
printf("総メモリ容量:\t%dG%4dM %4dK\n",
memtotal_kb/(1024*1024), /* Gb */
(memtotal_kb/(1024))%1024, /* Mb */
(memtotal_kb% (1024*1024))%1024); /* Kb */
}
if((buf=strstr(buffer, "MemFree"))!=NULL) /* 単純な grep 関数*/ {
buffer [strlen(buffer) -1 ]=0; /* tr() 関数の単純な実装*/ sscanf(buffer, "%s%s", content, content); int memfree_kb=(int)(strtof(content, NULL)); printf ("Memory利用可能な容量:\t%dG%4dM %4dK\n", memfree_kb/(1024*1024), /* Gb */ (memfree_kb/(1024))%1024, /* Mb */
(memfree_kb%(1024*1024))%1024); /* Kb */
}
if((buf=strstr(buffer, "SwapTotal"))!=NULL) /* grep の機能を単純に実現します */ {
buffer [ strlen (buffer)-1]=0; /* tr() 関数の機能を単純に実現します*/ sscanf(buffer, "%s%s", content, content); int swaptotal_kb=(int)(strtof(content, NULL) ); printf("合計 SWAP 容量:\t%dG%4dM %4dK\n", swaptotal_kb/(1024*1024), /* Gb */ (swaptotal_kb/(1024))%1024, /* Mb * / ( swaptotal_kb%(1024*1024))%1024); /* Kb */ } if((buf=strstr(buffer, "SwapFree"))!=NULL) /* grepの機能を単純に実現する*/ {
buffer[strlen(buffer)-1]=0; /* tr() 関数の単純な実装*/
sscanf(buffer, "%s%s", content, content);
int swapfree_kb=(int)(strtof(content , NULL));
printf("SWAP 利用可能な容量:\t%dG%4dM %4dK\n",
swapfree_kb/(1024*1024), /* Gb */
(swapfree_kb/(1024))%1024, /* Mb * /
(swapfree_kb%(1024*1024))%1024); /* Kb */
Break; /* 必要な情報が満たされたため、ループを終了します */ }
}
}
/
*
* === FUNCTION ===== ================================================= == ================
* 名前: main
* 説明:
* ================================================ === =========================================
*/
int main ( int argc, char *argv[] )
{ printf(">>>システム ロード<<<\n"); sampleLoadAvg(); printf("------ -------- --------------\n"); printf("\n"); printf(">>>runtime<<<\n"); sampleTime(); printf(" \n"); printf(">>>バージョン情報<<<\n"); sampleOsType(); sampleOsRelease(); sampleKernelVersion(); printf("------ ------ ------------------------\n"); printf("\n"); printf(">>>ディスク情報<<<\n"); sampleDiskStat();
printf("----------------------------------------\n");
printf( "\n");
printf(">>>コンテキスト切り替え<<<\n");
sampleContext();
printf("--------------------- -------------------\n");
printf("\n");
printf(">>>スレッド番号<<<\n");
sampleProcesses( );
printf("------------------------------------------\n ");
printf("\n");
printf(">>>メモリ情報<<<\n");
sampleMeminfo();
printf("---------------- --- ---------------------\n");
EXIT_SUCCESS を返します;
}