还是一般的套路,看完hostid.c后,了解了一个系统调用: gethostid() ,ok,先来自己粗燥地实现一下^_^
[root@bjxdurs235 20091028]# cat my_hostid.c
#include
int main(void)
{
int id;
id = gethostid();
printf("%0x\n",id);
}
[root@bjxdurs235 20091028]# ./a.out
6aca27c7
[root@bjxdurs235 20091028]# ./a.out
6aca1ec3
为了避免看完一个程序后,就把系统调用或者库函数给忘记了,我觉得有必要从今天开始维护一个日记页面,总结从现在到以后学习中遇到的系统调用或者库函数,为将来总结这一段时间的学习所用,简单明了即可。
下面再罗列一下代码注释:
/* print the hexadecimal identifier for the current host
Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004 Free
Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/* Written by Jim Meyering. */
//包含进来4个标准头文件
#include
#include
#include
#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 <%s>.\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
}
阅读(1556) | 评论(0) | 转发(0) |