现在的位置: 首页 > 综合 > 正文

关于getopt函数的用法分析

2013年08月17日 ⁄ 综合 ⁄ 共 3733字 ⁄ 字号 评论关闭
最近在看代码的时候碰到了getopt函数,具体怎么用一直是模模糊糊的。现在结合VTUN的代码测试一下它的具体用法。
看看getopt到底是何方神圣。废话不多言,现在开始。
程序由于简单,就没有注释,将就看看啊。

/************************************
* filename: getopt.c
* author: GolenSoldier
* date: 2008.12.15
* version: 1.0
*************************************/

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

extern int optind,opterr,optopt;
extern char *optarg;

void usage(void)
{
     printf("Usage: /n");

     printf("  Server:/n");
     printf("/tvtund <-s> [-f file] [-P port] [-L local address]/n");

     printf("  Client:/n");
     printf("/tvtund [-f file] " /* [-P port] [-L local address] */
        "[-p] [-m] [-t timeout] <host profile> <server address>/n");
}

int main(int argc, char *argv[])
{
    int opt;
    int svr = 0;
    int svr_type = 0;
    int daemon = 1;
    int port = 5000;
    int persist = 1;
    int timeout = 30;
    char *svr_addr = NULL;
    char *cfg_file = NULL;
        char *svr_name = NULL;
    char *host;

    while( (opt=getopt(argc, argv, "isf:P:L:t:np")) != EOF )
     {
        switch(opt)
        {
        case 'i':
            svr_type = 1;
        case 's':
            svr = 1;
            break;
        case 'L':
            svr_addr = strdup(optarg);
            break;
        case 'P':
            port = atoi(optarg);
            break;
        case 'f':
            cfg_file = strdup(optarg);
            break;
        case 'n':
            daemon = 0;
            break;
        case 'p':
            persist = 1;
            break;
        case 't':
            timeout = atoi(optarg);    
            break;
        default:
            usage();
            exit(1);
        }
    }

    printf("optind is:%d./n", optind);
    printf("optarg is:%s./n", optarg);
        
    if (!svr)
    {
        if( argc - optind < 2 )
        {
            printf("arg - optind < 2!/n");
            usage();
            exit(1);
        }
        
        host = argv[optind++];
        printf("client. host is %s./n", host);
                svr_name = strdup(argv[optind]);
                printf("srv_name is %s./n",svr_name);
    }
    else
        printf("is server./n");

    printf("svr is:%d./t"
        "svr_type is:%d./t"
        "daemon is:%d./t"
        "port is:%d./t"
        "persist is:%d./t"
        "timeout is:%d./t"
        "svr_addr is:%s./t"
        "cfg_file is:%s./n",
 svr, svr_type, daemon, port, persist, timeout, svr_addr, cfg_file);
  exit(0);
}

RedHat 9 编译一下:
[root@root home]#gcc -Wall getopt.c -o getopt

下面是输入的测试的结果:
1
[root@root home]#./getopt
optind is:1.
optarg is:(null).
arg - optind < 2!
Usage:
  Server:
        vtund <-s> [-f file] [-P port] [-L local address]
  Client:
        vtund [-f file] [-p] [-m] [-t timeout] <host profile> <server address>

2
[root@root home]#./getopt -s cli
optind is:2.
optarg is:(null).
is server.
svr is:1.    svr_type is:0.    daemon is:1.    port is:5000.    persist is:1.    timeout is:30.    svr_addr is:(null).    cfg_file is:(null).


3
[root@root home]#./getopt -s -i -f -P 110 -t 120
optind is:7.
optarg is:(null).
is server.
svr is:1.    svr_type is:1.    daemon is:1.    port is:5000.    persist is:1.    timeout is:30.    svr_addr is:(null).    cfg_file is:-P.

4
[root@root home]#./getopt -s -f ab.c -p 110 -L 1.1.1.1

optind is:7.
optarg is:(null).
is server.
svr is:1.    svr_type is:0.    daemon is:1.    port is:5000.    persist is:1.    timeout is:30.    svr_addr is:1.1.1.1.    cfg_file is:ab.c.

5
[root@root home]#./getopt cli
optind is:1.
optarg is:(null).
arg - optind < 2!
Usage:
  Server:
        vtund <-s> [-f file] [-P port] [-L local address]
  Client:
        vtund [-f file] [-p] [-m] [-t timeout] <host profile> <server address>

6
[root@root home]#./getopt cli 1.1.1.1
optind is:1.
optarg is:(null).
client. host is cli.
srv_name is 1.1.1.1.
svr is:0.    svr_type is:0.    daemon is:1.    port is:5000.    persist is:1.    timeout is:30.    svr_addr is:(null).    cfg_file is:(null).

7
[root@root home]#./getopt cli -f ab.c -L 1.1.1.1 1.1.1.2
optind is:5.
optarg is:(null).
client. host is cli.
srv_name is 1.1.1.2.
svr is:0.    svr_type is:0.    daemon is:1.    port is:5000.    persist is:1.    timeout is:30.    svr_addr is:1.1.1.1.    cfg_file is:ab.c.

总结:
关于 argc - optind < 2 语句就是为了客户端的<host profile> <server address>配置。换句话说,在客户端,只要是CASE中没有的,就会被认为是客户端的<host profile> <server address>配置,而在服务器端,就会被认为是输入差错。

抱歉!评论已关闭.