文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>bb_getopt_ulflags与 getopt

bb_getopt_ulflags与 getopt

时间:2010-08-12  来源:s.t_seeyou

在busybox 中常常用到分析命令行参数,这里主要分析一下getopt 与bb_getopt_ulflags   getopt 是库文件 #include<unistd.h>   int getopt(int argc,char *const argv[],const char *optstring) 举例如下:  

while ((ch = getopt(argc, argv, "h?biadswcl:LM")) != EOF) {
        switch (ch) {
        ...
        case 'w':
            cmd = LKT_DEBUG_SET_MODE;
            mode_flags |= LKT_DBG_80211;
            break;
        case 'c':
            cmd = LKT_DEBUG_SET_MODE;
            mode_flags &= LKT_DBG_CLEAN_ALL;
            break;
        case 'l':
            cmd = LKT_DEBUG_SET_LEVEL;
            level = atoi(optarg);  /*optarg 为X:后面带的参数*/
            if (level < 0||level >5)
                bb_error_msg_and_die("level must be [0,5].");
            break;
        ...
        case 'h':
        case '?':
        default:
            bb_show_usage();
        }
    }

另外bb_getopt_ulflags为busybox 自定义的库函数:

unsigned long bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)

参数前二项如上同,applet_opts 也于上同,后面通常是参数带:者所需要的返回值的地址。别外反回值为所有存在applet_opts列表对应的位;

举例如:httpd_main中部分

 

 


 

static const char httpd_opts[]="c:d:h:"
#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
                "e:"
#define OPT_INC_1 1
#else
#define OPT_INC_1 0
#endif
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
                "r:"
# ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
                "m:"
# define OPT_INC_2 2
# else
# define OPT_INC_2 1
#endif
#else
#define OPT_INC_2 0
#endif
#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
                "p:v"
#ifdef CONFIG_FEATURE_HTTPD_SETUID
                "u:"
#endif
#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
                    ;

#define OPT_CONFIG_FILE (1<<0)
#define OPT_DECODE_URL (1<<1)
#define OPT_HOME_HTTPD (1<<2)
#define OPT_ENCODE_URL (1<<(2+OPT_INC_1))
#define OPT_REALM (1<<(3+OPT_INC_1))
#define OPT_MD5 (1<<(4+OPT_INC_1))
#define OPT_PORT (1<<(3+OPT_INC_1+OPT_INC_2))
#define OPT_DEBUG (1<<(4+OPT_INC_1+OPT_INC_2))
#define OPT_SETUID (1<<(5+OPT_INC_1+OPT_INC_2))


#ifdef HTTPD_STANDALONE
int main(int argc, char *argv[])
#else
int httpd_main(int argc, char *argv[])
#endif
{
  unsigned long opt;
  const char *home_httpd = home;
  char *url_for_decode;
#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
  const char *url_for_encode;
#endif
#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
  const char *s_port;
#endif

#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
  int server;
#endif

#ifdef CONFIG_FEATURE_HTTPD_SETUID
  const char *s_uid;
  long uid = -1;
#endif

#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
  const char *pass;
#endif

  config = xcalloc(1, sizeof(*config));
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
  config->realm = "Web Server Authentication";
#endif

#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
  config->port = 80;
#endif

  config->ContentLength = -1;

  opt = bb_getopt_ulflags(argc, argv, httpd_opts,
            &(config->configFile), &url_for_decode, &home_httpd
#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
            , &url_for_encode
#endif
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
            , &(config->realm)
# ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
            , &pass
# endif
#endif
#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
            , &s_port
#ifdef CONFIG_FEATURE_HTTPD_SETUID
            , &s_uid
#endif
#endif
    );

  if(opt & OPT_DECODE_URL) {
      printf("%s", decodeString(url_for_decode, 1));
      return 0;
  }
#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
  if(opt & OPT_ENCODE_URL) {
      printf("%s", encodeString(url_for_encode));
      return 0;
  }
#endif
#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
  if(opt & OPT_MD5) {
      printf("%s\n", pw_encrypt(pass, "$1$"));
      return 0;
  }
#endif
#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
    if(opt & OPT_PORT)
    config->port = bb_xgetlarg(s_port, 10, 1, 0xffff);
    config->debugHttpd = opt & OPT_DEBUG;
#ifdef CONFIG_FEATURE_HTTPD_SETUID
    if(opt & OPT_SETUID) {
    char *e;

    uid = strtol(s_uid, &e, 0);
    if(*e != '\0') {
        /* not integer */
        uid = my_getpwnam(s_uid);
    }
      }
#endif
#endif

}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载