Chinaunix首页 | 论坛 | 博客
  • 博客访问: 385718
  • 博文数量: 124
  • 博客积分: 2911
  • 博客等级: 少校
  • 技术积分: 1050
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-15 15:57
文章分类

全部博文(124)

文章存档

2012年(6)

2011年(26)

2010年(92)

我的朋友

分类: C/C++

2010-07-24 17:07:49

=======应用软件的用法提示=========
#include
#include

#define OPTARGS "dkMrRsvC:"
const char *cmdname = "heartbeat";

void
usage(void)
{    
    const char *    optionargs = OPTARGS;    //#define OPTARGS        "dkMrRsvC:"
    const char *    thislet;                //

    fprintf(stderr, "\nUsage: %s [-", cmdname);
    for (thislet=optionargs; *thislet; ++thislet) {
        if (thislet[0] != ':' &&  thislet[1] != ':') {
            fputc(*thislet, stderr);                    //output one by one
        }
    }
    fputc(']', stderr);
    for (thislet=optionargs; *thislet; ++thislet) {
        if (thislet[1] == ':') {
            const char *    desc = "unknown-flag-argument";

            /* Put a switch statement here eventually... */
            switch(thislet[0]) {
            case 'C':    desc = "Current-resource-state";
                    break;
            }

            fprintf(stderr, " [-%c %s]", *thislet, desc);
        }
    }
    fprintf(stderr, "\n");
    fprintf(stderr, "\t-C only valid with -R\n");
    fprintf(stderr, "\t-r is mutually exclusive with -R\n");
    exit(1);
}

int main(int argc, char ** argv){

    usage();

}
=====================================================================================================================
console: gcc usage -o xxx
console:

Usage: heartbeat [-dkMrRsv] [-C Current-resource-state]
    -C only valid with -R
    -r is mutually exclusive with -R

/********************************************************************************************************/
=== 处理可执行文件的参数:[exec] [-parameter]====
=== getopt的使用 ===

int getopt(int argc, char * const *argv, const char *optstring);

#include
extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;
int getopt(int argc, char * const *argv, const char *optstring);

//参数argc和argv是由main()传递的参数个数和内容
//参数optstring 则代表欲处理的选项字符串。
//如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。
//如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
//返回值:如果找到符合的参数则返回此参数字母,如果参数不包含在参数optstring 的选项字母则返回“?”字符,分析结束则返回-1。

直接看一个例子:
/* getopt.c */
#include
#include

int main(int argc ,char ** argv){
    int flag;
    while((flag = getopt(argc, argv, "abcde")) != EOF)
    {
        switch(flag)
        {
            case 'a':
                printf("a\n");
                break;
            case 'b':
                printf("b\n");
                break;
            case 'c':
                printf("c\n");
                break;
            default:
                printf("hi~\n");
                break;
        }
    }
    printf("如果没有参数则输出此行\n"); //可执行程序没有参数时直接跳出了getopt,到此处
}
/*******************/
console: gcc [src] -o xxx
console: ./xxx -b
console: b
console: ./xxx -z -b -a
console: ./xxx: invalid option -- 'z'
console: hi~
console: b
console: a

/********************************************************************************************************/
===用scandir获取.so文件的数目===

#include
#include
#include
#define COMM_MODULE_DIR    "/usr/lib/heartbeat/modules/comm"

static int so_select (const struct dirent *dire)  //dire is significant
{
    const char *end = &dire->d_name[strlen(dire->d_name) - 3];
    const char *obj_end = ".so";

    if (strcmp(end, obj_end) == 0)
        return 1;
    return 0;
}


int main(){
    int n;
    struct dirent **namelist;   
    n = scandir(COMM_MODULE_DIR, &namelist,&so_select, 0);
    printf("num = %d\n", n);
}
/*****更详细的分析*****//*****更详细的分析*****//*****更详细的分析*****//*****更详细的分析*****//*****更详细的分析*****/

#include
#include
#include
#include
#define COMM_MODULE_DIR    "/usr/lib/heartbeat/modules/comm"

//static int so_select (const struct dirent *dire)

    /*        struct dirent {
              ino_t          d_ino;       // inode number
              off_t           d_off;       // offset to the next dirent
              unsigned   short d_reclen;    // length of this record
              unsigned char d_type;      // type of file
              char           d_name[256];  //filename
          };

       According to POSIX, the dirent structure contains a field char d_name[] of unspecified size,
       with at most NAME_MAX characters preceding the terminating null byte

       */
int so_select (const struct dirent *dire)
{
    printf("dire->d_ino = %d\n",dire->d_ino);
    printf("dire->d_off = %d\n",dire->d_off);
    printf("dire->d_reclen = %d\n",dire->d_reclen);
    printf("dire->d_type = %c\n",dire->d_type);
    printf("dire->d_name = %s\n",dire->d_name);
    printf("sizeof d_name = %d\n",sizeof dire->d_name);

    printf("*************************************************\n");
    const char *end = &dire->d_name[strlen(dire->d_name) - 3];
    const char *obj_end = ".so";

    if (strcmp(end, obj_end) == 0)
        return 1;
    return 0;
}


int main(){
    int n, i;
    struct dirent **namelist;   
    char * obj_path;
    n = scandir(COMM_MODULE_DIR, &namelist, &so_select, 0);
    printf("num = %d\n", n);

    for(i = 0; i < n; i++)
    {
        printf("namelist[%d]->d_name:%s\n,obj_path_size = %d\n", i , namelist[i]->d_name, (strlen(COMM_MODULE_DIR) + strlen(namelist[i]->d_name) + 2) * sizeof(char));        //print: {namelist[i]->d_name , obj_path_alloc_size}

    /* seize : full_directory_path */
        obj_path = (char *)malloc( (strlen(COMM_MODULE_DIR) + strlen(namelist[i]->d_name) + 2) * sizeof(char) );    //'2' is the compensation in 2 times strlen(***) which ignores '\0'.
        sprintf(obj_path, "%s/%s", COMM_MODULE_DIR, namelist[i]->d_name);

        printf("obj_path[%d] = %s\n", i , obj_path);    //print Obj_path.
    }
}

/********************************************************************************************************/
Malloc简洁用法

#define MALLOCT(t)    (t*)(malloc(sizeof(t)))
fns = MALLOCT(struct hb_media_fns);

#define CALLOC(t,n) (t*)(calloc(sizeof(t),n))
tmp_matrix = CALLOC(int, radius);

/********************************************************************************************************/
获得系统时间
#include
#include

#define TIME_T unsigned long

char * ha_timestamp(void)
{
    static char ts[64];
    struct tm*    ttm;
    TIME_T        now;

    now = time((time_t *)NULL);
    ttm = (struct tm *)localtime((const time_t *)&now);

    snprintf(ts, sizeof(ts), "%04d/%02d/%02d_%02d:%02d:%02d"
    ,    ttm->tm_year+1900, ttm->tm_mon+1, ttm->tm_mday
    ,    ttm->tm_hour, ttm->tm_min, ttm->tm_sec);
    return(ts);
}

int main(){
    printf("%s\n",ha_timestamp());

}
/********************************************************************************************************/
获得执行路径上的程序名 Dir/cmdname

#include
int main (int argc, char ** argv)
{
    char * cmdname;
    if((cmdname = strrchr(argv[0],'/')) != NULL)
    {
        printf("argv[0] = %s\n", argv[0]);
        cmdname++;
        argv[0] = cmdname;
    }else{
        cmdname = argv[0];
    }
    printf("cmdname = %s, argv[0] = %s\n",cmdname,argv[0]);
}

/********************************************************************************************************/
获得本地的服务信息
#include
#include

int main (int argc , char ** argv)
{
    if(argc != 3)
    {
        printf("Usage: [exec] [service] [protocal]\n");
        return 0;    
    }

    char ** aliase;
    struct servent * service;
    if((service = getservbyname(argv[1],argv[2])) == NULL);
    {
        printf("No such service or protocal\n");
        return 0;
    }
    printf("severname = %s\n", service->s_name);
    for(aliase = service->s_aliases ; *aliase != NULL; aliase++)
        printf("%d: %s\n",aliase-service->s_aliases, *aliase);
    printf("port:%d\n", ntohs(service->s_port));
}
/********************************************************************************************************/
获取进程的进程号
pid_t get_running_hb_pid()
{
    pid_t    pid;
    FILE *    lockfd;       
//    lockfd = fopen(PIDFILE, "r");   //PidFile :/var/run/heartbeat.pid
    if ((lockfd = fopen(PIDFILE, "r")) != NULL && fscanf(lockfd, "%d", &pid) == 1 && pid > 0) {    //got the pid from lockfd
        if (kill(pid, 0) >= 0 || errno != ESRCH) {   //SIG==0: detecting the exist of process.
            fclose(lockfd);
            return(pid);
        }
    }
    if (lockfd != NULL) {
        fclose(lockfd);
    }
    return(-1);
}
/********************************************************************************************************/
读取配置文件内容
#define File_Path "/root/Desktop/test"
#define WHITESPACE "\t\n\r\f"
#define COMMENTCHAR '#'
#define EOS '\0'
#define MAXLINE 1024

int main(){
    char buf[MAXLINE];
    char * cp;
    FILE * file;
    if( (file =fopen(File_Path, "r")) == NULL){
        perror("Can't open file");
        return 0;
    }

    while (fgets(buf, MAXLINE, file) != NULL) {    //get one line in buf everytime.
        char *  bp = buf;
        /* Skip over white space */
        bp += strspn(bp, WHITESPACE);

        /* Zap comments on the line */
        if ((cp = strchr(bp, COMMENTCHAR)) != NULL)  {
            *cp = EOS;
        }

        /* Ignore blank (and comment) lines */
        if (*bp == EOS) {
            continue;
        }
    }
}

=======dlopen/dlsym/dlerrer/rdynamic...参看dlxxx_Family_Functions=========
阅读(873) | 评论(0) | 转发(0) |
0

上一篇:numbers_in_man-page

下一篇:POJ_2299&Merge_Sort

给主人留下些什么吧!~~