⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 init.cpp

📁 包含客户和服务器的升级程序,在linux下可以运行的.
💻 CPP
字号:
/* * Copyright (C) 2006, Binary Ma * Licence: GNU GPL 1991 - version 2 * Bug report: binary@eniak.org*/#include <string.h>#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include "init.h"#include "parse.h"#include "usage.h"#include "version.h"static const char* YES             = "yes";static const char* NO              = "no";static const int SIZE_PATH         = 1024;static const int SIZE_NAME         = 256;static int parse_switch( const char* conf, const char* key ){    if( NULL == conf || NULL == key )        return -__LINE__;        char* point = parse( conf, key );    if( NULL == point )        return -__LINE__;        int retval = -1;    if( 0 == strcasecmp( NO, point ) )        retval = false;    else    if( 0 == strcasecmp( YES, point ) )        retval = true;        return retval;}static int parse_number( const char* conf, const char* key ){    if( NULL == conf || NULL == key )        return -__LINE__;        char* point = parse( conf, key );    if( NULL == point )        return -__LINE__;        return atoi( point );}static char* parse_string( const char* conf, const char* key ){    if( NULL == conf || NULL == key )        return NULL;        char* point = parse( conf, key );    if( NULL == point )        return NULL;        return strdup( point );}void init( struct option* opt ){    if( NULL == opt )        return;        bzero( opt, sizeof( struct option ) );        // host must init to -1, NULL is valid, default.    opt->host           = (char*)-1;    opt->server_mode    = -1;    opt->timeout        = -1;    opt->idle           = -1;    opt->backlog        = -1;    opt->log_enable     = -1;    opt->overlap        = -1;}int parse_config( struct option* opt ){    if( NULL == opt )        exit( 0 );        // config file    if( NULL == opt->conf_file )        opt->conf_file = "/etc/netkite.conf";        // server or client mode    if( -1 == opt->server_mode )    {        opt->server_mode = parse_switch( opt->conf_file, "server_mode" );        if( opt->server_mode < 0 )            opt->server_mode = false;    }        // public options    if( -1 == opt->timeout )    {        opt->timeout = parse_number( opt->conf_file, "timeout" );        if( opt->timeout < 0 )            opt->timeout = 75;    }        if( -1 == opt->log_enable )    {        opt->log_enable = parse_switch( opt->conf_file, "log_enable" );        if( opt->log_enable < 0 )            opt->log_enable = true;    }        if( (char*)-1 == opt->host )    {        const char* key = "address";        if( opt->server_mode )            key = "interface";                // if null, that all right        opt->host = parse_string( opt->conf_file, key );    }        if( NULL == opt->port )    {        opt->port = parse_string( opt->conf_file, "port" );        if( NULL == opt->port )            opt->port = "7002";    }        if( NULL == opt->log_file )    {        opt->log_file = parse_string( opt->conf_file, "log_file" );        if( NULL == opt->log_file )            opt->log_file = "/var/log/netkite.log";    }    // private options    if( opt->server_mode )    {        if( NULL == opt->root )        {            opt->root = parse_string( opt->conf_file, "root" );            if( NULL == opt->root )                opt->root = "/var/netkite";        }                if( -1 == opt->backlog )        {            opt->backlog = parse_number( opt->conf_file, "backlog" );            if( opt->backlog < 0 )                opt->backlog = 1024;        }    }    else    {        if( -1 == opt->idle )        {            opt->idle = parse_number( opt->conf_file, "idle" );            if( opt->idle < 0 )                opt->idle = 0;        }        if( NULL == opt->module )        {            opt->module = parse_string( opt->conf_file, "module" );            if( NULL == opt->module )                opt->module = "test";        }        if( NULL == opt->tmp_path )        {            opt->tmp_path = parse_string( opt->conf_file, "tmp_path" );            if( NULL == opt->tmp_path )                opt->tmp_path = "/tmp";        }        if( -1 == opt->overlap )        {            opt->overlap = parse_switch( opt->conf_file, "overlap" );            if( opt->overlap < 0 )                opt->overlap = false;        }    }    return 0;}const char* itoa( int i ){    static char a[32];    snprintf( a, sizeof( a ), "%i", i );    return a;}int parse_test( struct option* opt ){    if( NULL == opt )        return -__LINE__;    const char* name = opt->server_mode?"interface":"address  ";    const char* addr = opt->server_mode?"0.0.0.0":"127.0.0.1";    printf( "display available configuration\n" );    if( NULL == opt->host )        printf( "%s        : %s\n", name, addr );    else        printf( "%s        : %s\n", name, opt->host );    printf( "server_mode      : %s\n", opt->server_mode? YES : NO );    printf( "log_enable       : %s\n", opt->log_enable? YES : NO );    printf( "port             : %s\n", opt->port );    printf( "timeout          : %i\n", opt->timeout );    printf( "log_file         : %s\n", opt->log_file );    printf( "conf_file        : %s\n", opt->conf_file );    if( opt->server_mode )    {        printf( "root             : %s\n", opt->root );        printf( "backlog          : %s\n", itoa( opt->backlog ) );    }    else    {        printf( "idle             : %s\n", itoa( opt->idle ) );        printf( "module           : %s\n", opt->module );        printf( "tmp_path         : %s\n", opt->tmp_path );        printf( "overlap          : %s\n", opt->overlap? YES : NO );    }        return 0;}int parse_option( int argc, char* argv[], struct option* opt ){    if( NULL == opt )        return -__LINE__;        char c = 0;    bool test = false;    const char* optstr = "vhHoscgGuUp:t:m:d:b:r:x:X:f:";    while( -1 != ( c = getopt( argc, argv, optstr ) ) )    {        switch( c )        {        case 'v':            printf( "netkite: %s\n", VERSION );            exit( 0 );        case 'h':            usage();            exit( 0 );        case 'H':            usage_conf();            exit( 0 );        case 'o':            test = true;            break;        case 's':            opt->server_mode = true;            break;        case 'c':            opt->server_mode = false;            break;                    case 'g':            opt->log_enable = false;            break;        case 'G':            opt->log_enable = true;            break;        case 'u':            opt->overlap = true;            break;                case 'U':            opt->overlap = false;            break;                    case 'p':            opt->port = strdup( optarg );            break;		        case 't':            opt->timeout = atoi( optarg );            break;        case 'm':            opt->module = strdup( optarg );            break;			        case 'd':            opt->idle = atoi( optarg );            break;        case 'b':            opt->backlog = atoi( optarg );            break;			        case 'r':            opt->root = strdup( optarg );            break;        case 'x':            opt->log_file = strdup( optarg );            break;                    case 'X':            opt->tmp_path = strdup( optarg );            break;                case 'f':            opt->conf_file = strdup( optarg );            break;        default:            usage();            exit( 0 );        }    }    if( NULL != argv[optind] )        opt->host = strdup( argv[optind] );        if( test )    {        parse_config( opt );        parse_test( opt );        exit( 0 );    }    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -