トップ 差分 一覧 ping ソース 検索 ヘルプ PDF RSS ログイン

C ホスト情報



目次



記事一覧

キーワード

C ホスト情報

[Programming 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);
}     

この本からの覚書。



YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto

Copyright© 矢木 浩人 All Rights Reserved.