getopt_long(argc, argv, "", OPTIONS, NULL)分析

本文分析了Android系统recovery.c模块中getopt_long函数的使用,该函数用于解析命令行参数。通过static const struct option OPTIONS数组定义选项,如'factory_test', 'send_intent', 'update_image'等。当接收到参数时,getopt_long会在OPTIONS中查找匹配项,返回对应的短选项。argc和argv分别表示参数总数和具体参数列表。getopt_long函数需要包含<getopt.h>头文件。最终,相关代码会编译生成recovery应用,并整合到固件recovery.img中。" 126123873,13817787,BGP与MPLS网络配置及路由实验,"['网络', 'BGP', 'MPLS', 'OSPF', '路由']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

static const struct option OPTIONS[] = {

{ "factory_test", no_argument, NULL, 'f'+'t'},

{ "send_intent", required_argument, NULL, 's' },

{ "update_image", required_argument, NULL, 'u' },

{ "recover_image", required_argument, NULL, 'r' }, 

{ "wipe_all", no_argument, NULL, 'w'+'a' },

{ "wipe_data", no_argument, NULL, 'w' },

}


while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1)
{

switch (arg) {

case 'p': previous_runs = atoi(optarg); break;

case 's': send_intent = optarg; break;

case 'u': update_image = optarg; break;

case 'r': recover_image = optarg; break;

case 'w': wipe_flags |= default_factory_reset;break;

case 'w'+'a': wipe_flags |= WIPE_ALL; break;

case '?':

LOGE("Invalid command argument\n");

continue;

}

}


上面贴的是android在recovery.c中的代码,当接收到参数时,在OPTION中寻找和参数相同的一项,之后getopt_long返回短选项,如"send_intent",返回“s”。

argc, argv
第一个参数是总共参数的数目,包括可执行文件本身
第二个参数是具体的参数 

getopt_long是用来解析参数的使用getopt_long(),需要引入头文件#include <getopt.h>

/gingerbread/bootable/recovery下有Android.mk,里面LOCAL_MODULE := recovery编译后生成recovery应用,

这个可执行文件会被最后编译成固件,如recovery.img烧写进板子

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值