GNU hello代码分析

本文深入分析GNU hello程序,重点探讨其如何遵循GNU标准,并讲解getopt_long函数在解析命令行参数中的关键作用。
摘要由CSDN通过智能技术生成
GNU中的一个软件包,包名叫hello,呵呵,看来很简单啦。

今天就来分析一下,GNU的目的是通过这个说明一下,GNU标准软件包所应遵守的规则,以及一些必须的测试,编译脚本文件等。

俺就不分析其他的了,直接上最具价值的代码,其他忽略,这个程序实在是太小了,最重要的是一个getopt_long函数的应用,

用于解析命令行参数的。

#include <config.h>
#include "system.h"
#include "errno.h"
#include "error.h"
#include "progname.h"
#include "xalloc.h"

static const struct option longopts[] = {
  {"greeting", required_argument, NULL, 'g'}, 需要参数
  {"help", no_argument, NULL, 'h'},   不需要参数
  {"traditional", no_argument, NULL, 't'},
  {"version", no_argument, NULL, 'v'},
  {NULL, 0, NULL, 0}    通过man getopt_long发现,这个结构体需要以NULL结束。
};

/* Forward declarations.  */ 前向声明,估计调用在实现之前。
static void print_help (void);
static void print_version (void);



int
main (int argc, char *argv[])  关键的入口函数在这里啦。
{
  int optc;
  int lose = 0;
  const char *greeting_msg;
  wchar_t *mb_greeting;
  size_t len;

  set_program_name (argv[0]);

  /* Set locale
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值