matlab2014如何获得hostid,hostid.c/获取主机标识

#include //包含4个本地文件夹里的头文件

#include "system.h"

#include "long-options.h"

#include "error.h"

#include "quote.h"

/* The official name of this program (e.g., no `g' prefix).  */

#define PROGRAM_NAME "hostid" //定义宏PROGRAM_NAME,其值为一个字符串

#define AUTHORS "Jim Meyering" //定义宏AUTHORS,其值也是个字符串,作者,这哥们好像写了不少程序啊

/* The name this program was run with, for error messages. */

char *program_name;//注释里说这个变量的目的是为了错误信息

void

usage (int status)//通用的帮助函数

{

if (status != EXIT_SUCCESS)//如果函数接受到的形参status不等于EXIT_SUCCESS

fprintf (stderr, _("Try `%s --help' for more information.\n"),

program_name);//则向标准错误打印信息,提示用户使用帮助

else //否则打印下面的代码段里的信息

{

printf (_("\

Usage: %s\n\

or:  %s OPTION\n\

Print the numeric identifier (in hexadecimal) for the current host.\n\

\n\

"),

program_name, program_name);//这句话表明了程序的作用,是打印当前主机的十六进制标识

fputs (HELP_OPTION_DESCRIPTION, stdout);

fputs (VERSION_OPTION_DESCRIPTION, stdout);

printf (_("\nReport bugs to .\n"), PACKAGE_BUGREPORT);

}

exit (status);//函数的退出状态是接受到的形参status的值

}

int

main (int argc, char **argv)//标准的main函数

{

unsigned int id;//无符号整型变量 id

initialize_main (&argc, &argv);//初始化命令行参数

program_name = argv[0];//将命令行参数的第一个赋值给变量program_name

setlocale (LC_ALL, "");//以下3句设置本地环境

bindtextdomain (PACKAGE, LOCALEDIR);

textdomain (PACKAGE);

atexit (close_stdout);//登记出口函数

parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,

usage, AUTHORS, (char const *) NULL);//解析命令行选项

if (getopt_long (argc, argv, "", NULL, NULL) != -1)//如果没有正常解析命令行选项

usage (EXIT_FAILURE);//则调用usage函数打印帮助信息

if (optind < argc)//如果调用getopt_long函数后得到的optind值小于argc,命令行的参数个数,则调用error函数

{

error (0, 0, _("extra operand %s"), quote (argv[optind]));

usage (EXIT_FAILURE);//并打印帮助信息

}

id = gethostid ();//核心的地方,通过系统调用gethostid获得当前主机的一个32位标识符

/* POSIX says gethostid returns a "32-bit identifier" but is silent

whether it's sign-extended.  Turn off any sign-extension.  This

is a no-op unless unsigned int is wider than 32 bits.

系统调用gethostid返回的值并没有说是否会溢出sign类型的最大值,程序在这里进行了位运算以避免溢出

*/

id &= 0xffffffff;

printf ("%08x\n", id); //打印格式为十六进制

exit (EXIT_SUCCESS);//程序最终退出状态为EXIT_SUCCESS

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值