「C ホスト情報」の版間の差分
ナビゲーションに移動
検索に移動
| (同じ利用者による、間の2版が非表示) | |||
| 1行目: | 1行目: | ||
| − | ==C ホスト情報== | + | ==[[C ホスト情報]]== |
| − | [[Programming C]] | + | [[Programming C]] | |
====gethostname==== | ====gethostname==== | ||
2020年2月16日 (日) 04:22時点における最新版
C ホスト情報
gethostname
#include <unistd.h> int gethostname(char *name, size_t namelen);
- machine の network名を文字列 name に書き込みます。
- 文字列nameは、少なくとも namelen文字の長さを持つものとして扱われる。
- 成功すると、0、それ以外は、-1を返す。
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char computer[256];
if ( gethostname(computer, 255) != 0 ) {
printf("Can't get hostname.\n");
exit(1);
}
printf("hostname : %s\n", computer);
exit(0);
}
uname
#include <sys/utsname.h> int uname(struct utsname *name);
- host computer に対してさらに詳しい情報を得る。
| member | 説明 |
|---|---|
| char sysname[] | operationg system 名 |
| char nodename[] | host名 |
| char release[] | systemのrelease level |
| char version[] | systemのversion 番号 |
| char machine[] | hardware の種類 |
#include <sys/utsname.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct utsname uts;
if ( uname(&uts) != 0 ) {
printf("Can't get information.\n");
exit(1);
}
printf("os name : %s\n", uts.sysname );
printf("host name : %s\n", uts.nodename );
printf("release : %s\n", uts.release );
printf("version : %s\n", uts.version );
printf("machine : %s\n", uts.machine );
}
gethostid
#include <unistd.h> long gethostid(void);
- host computer が持つ一意の値を返す。
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
long hid = gethostid();
printf("host id : %x\n", hid);
exit(0);
}
この本からの覚書。
© 2006 矢木浩人