coreutils5.0 uname命令和源码分析
uname /* 默认输出内核名。和uname -s一样 */
-a, --all /* 按以下顺序打印所有信息 */
-s /* 打印内核名称 */
-n /* 打印主机名 */
-r /* 打印内核版本发行号 */
-v /* 打印内核版本 */
-m /* 打印机器硬件名称 */
-p /* 打印CPU类型 */
-i /* 打印硬件平台 */
-o /* 打印操作系统 */
/* uname -- print system information
Copyright 1989, 1992, 1993, 1996, 1997, 1999, 2000, 2001, 2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <getopt.h>
#if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H
# include <sys/systeminfo.h>
#endif
#if HAVE_SYSCTL && HAVE_SYS_SYSCTL_H
# include <sys/param.h> /* needed for OpenBSD 3.0 */
# include <sys/sysctl.h>
# ifdef HW_MODEL
# ifdef HW_MACHINE_ARCH
/* E.g., FreeBSD 4.5, NetBSD 1.5.2 */
# define UNAME_HARDWARE_PLATFORM HW_MODEL
# define UNAME_PROCESSOR HW_MACHINE_ARCH
# else
/* E.g., OpenBSD 3.0 */
# define UNAME_PROCESSOR HW_MODEL
# endif
# endif
#endif
#include "system.h"
#include "error.h"
#include "closeout.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "uname"
#define AUTHORS "David MacKenzie"
/* 用于修改toprint的值 */
/* 内核名称 */
#define PRINT_KERNEL_NAME 1
/* 主机名 */
#define PRINT_NODENAME 2
/* 内核发行号 */
#define PRINT_KERNEL_RELEASE 4
/* 内核版本 */
#define PRINT_KERNEL_VERSION 8
/* 硬件名 */
#define PRINT_MACHINE 16
/* CPU类型 */
#define PRINT_PROCESSOR 32
/* 硬件平台 */
#define PRINT_HARDWARE_PLATFORM 64
/* 操作系统 */
#define PRINT_OPERATING_SYSTEM 128
/* 保存程序名 = argv[0] */
char *program_name;
/* 长选项结构体 */
static struct option const long_options[] =
{
{"all", no_argument, NULL, 'a'}, /* "长选项名称","选项:no_argument代表该长选项无需参数","元素三为NULL时,getopt将直接返回元素四,不为空时参数三将保存元素4" */
{"kernel-name", no_argument, NULL, 's'},
{"sysname", no_argument, NULL, 's'}, /* 即将弃用,该源码是5.0,不知道9.0是否以弃用. */
{"nodename", no_argument, NULL, 'n'},
{"kernel-release", no_argument, NULL, 'r'},
{"release", no_argument, NULL, 'r'}, /* 即将弃用,该源码是5.0,不知道9.0是否以弃用. */
{"kernel-version", no_argument, NULL, 'v'},
{"machine", no_argument, NULL, 'm'},
{"processor", no_argument, NULL, 'p'},
{"hardware-platform", no_argument, NULL, 'i'},
{"operating-system", no_argument, NULL, 'o'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0} /* 没有参数时返回0 */
};
/* 传入的命令行参数的错误函数,传入不支持的参数就调用该函数 */
void
usage (int status)
{
if (status != 0)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
{
/* 调用错误函数时,提示该命令用法,命令没有参数时将和-s一样 */
printf (_("Usage: %s [OPTION]...\n"), program_name);
fputs (_("\
Print certain system information. With no OPTION, same as -s.\n\
\n\
/* 按以下顺序打印所有信息 */
-a, --all print all information, in the following order:\n\
/* 打印内核名称 */
-s, --kernel-name print the kernel name\n\
/* 打印主机名 */
-n, --nodename print the network node hostname\n\
/* 打印内核版本发行号 */
-r, --kernel-release print the kernel release\n\
"), stdout);
fputs (_("\
/* 打印内核版本 */
-v, --kernel-version print the kernel version\n\
/* 打印机器硬件名称 */
-m, --machine print the machine hardware name\n\
/* 打印CPU类型 */
-p, --processor print the processor type\n\
/* 打印硬件平台 */
-i, --hardware-platform print the hardware platform\n\
/* 打印操作系统 */
-o, --operating-system print the operating system\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
}
exit (status);
}
/* Print ELEMENT, preceded by a space if something has already been
printed. */
static void
print_element (char const *element)
{
static int printed;
if (printed++)
putchar (' ');
fputs (element, stdout);
}
int
main (int argc, char **argv)
{
int c;
static char const unknown[] = "unknown";
/* Mask indicating which elements to print. */
/* 指示要打印哪些元素的掩码(最后用该值判断用户需要输出什么信息) */
unsigned toprint = 0;
/* 程序初始化 */
program_name = argv[0]; /* 程序名 */
setlocale (LC_ALL, ""); /* 获取系统编码、地区代码等信息 */
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);/* 出口函数 */
/* 解析命令行参数 */
while ((c = getopt_long (argc, argv, "asnrvmpio", long_options, NULL)) != -1) /* c保存用户传入的参数 */
{
/* 判断用户传入的函数来修改toprint的值 */
switch (c)
{
case 0:
break;
case 'a':
toprint = -1;
break;
/* 内核名称,值=1 */
case 's':
toprint |= PRINT_KERNEL_NAME;
break;
/* 主机名称,值=2 */
case 'n':
toprint |= PRINT_NODENAME;
break;
/* 内核发行号,值=4 */
case 'r':
toprint |= PRINT_KERNEL_RELEASE;
break;
/* 内核版本,值=8 */
case 'v':
toprint |= PRINT_KERNEL_VERSION;
break;
/* 硬件名,值=16 */
case 'm':
toprint |= PRINT_MACHINE;
break;
/* CPU类型,值=32 */
case 'p':
toprint |= PRINT_PROCESSOR;
break;
/* 硬件平台,值=64 */
case 'i':
toprint |= PRINT_HARDWARE_PLATFORM;
break;
/* 操作系统,值=128 */
case 'o':
toprint |= PRINT_OPERATING_SYSTEM;
break;
/* 在system.h内定义 在这case内会exit退出程序 */
case_GETOPT_HELP_CHAR;
/* 在system.h内定义 在这case内会exit退出程序 */
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_FAILURE); /* 如果以上case都没有触发的话,调用usage帮助函数,退出程序,EXIT_FAILURE的值为1,定义在stdlib */
}
}
/* getopt调用的次数不等于main传入的参数就报错。用来测试传入的参数是否全部都getopt成功。因为每次调用一次getopt都会使optind +1 */
if (optind != argc)
usage (EXIT_FAILURE);
/* 不带参数默认转成内核名称 */
if (toprint == 0)
toprint = PRINT_KERNEL_NAME;
/* 参数值==1 | 2 | 4 | 8 | 16 进入该if */
if (toprint
& (PRINT_KERNEL_NAME | PRINT_NODENAME | PRINT_KERNEL_RELEASE
| PRINT_KERNEL_VERSION | PRINT_MACHINE))
{
struct utsname name; /* 获取系统相关信息的结构体 */
if (uname (&name) == -1) /* 填充ustname结构 */
error (EXIT_FAILURE, errno, _("cannot get system name")); /* 无法获取系统名称 */
if (toprint & PRINT_KERNEL_NAME)
print_element (name.sysname);
if (toprint & PRINT_NODENAME)
print_element (name.nodename);
if (toprint & PRINT_KERNEL_RELEASE)
print_element (name.release);
if (toprint & PRINT_KERNEL_VERSION)
print_element (name.version);
if (toprint & PRINT_MACHINE)
print_element (name.machine);
}
/* 参数值==32 */
if (toprint & PRINT_PROCESSOR)
{
char const *element = unknown;
#if HAVE_SYSINFO && defined SI_ARCHITECTURE
{
static char processor[257];
if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
element = processor;
}
#endif
#ifdef UNAME_PROCESSOR
if (element == unknown)
{
static char processor[257];
size_t s = sizeof processor;
static int mib[] = { CTL_HW, UNAME_PROCESSOR };
if (sysctl (mib, 2, processor, &s, 0, 0) >= 0)
element = processor;
}
#endif
print_element (element);
}
/* 参数值==64 */
if (toprint & PRINT_HARDWARE_PLATFORM)
{
char const *element = unknown;
#if HAVE_SYSINFO && defined SI_PLATFORM
{
static char hardware_platform[257];
if (0 <= sysinfo (SI_PLATFORM,
hardware_platform, sizeof hardware_platform))
element = hardware_platform;
}
#endif
#ifdef UNAME_HARDWARE_PLATFORM
if (element == unknown)
{
static char hardware_platform[257];
size_t s = sizeof hardware_platform;
static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
element = hardware_platform;
}
#endif
print_element (element);
}
/* 参数值==128 */
if (toprint & PRINT_OPERATING_SYSTEM)
print_element (HOST_OPERATING_SYSTEM);
putchar ('\n');
exit (EXIT_SUCCESS);
}
用getopt_long解析用户传入参数后并保存在toprint,调用uname填充ustname结构体,判断torint输出对应的信息
问1:-a = -1时是怎么输出所有。是不是-1在&别的常量时if判断为true所以能输出所有。
因为发现5.0和新的源码差距太大了,所以man shuf翻到最下面查看了我linux的coreutils是8.32,所以去看8.32了,这个问题等8.32的时候在解决了