Plugins under Linux

Source: Internet
Author: User
Tags glob

Linux plug-in use example: by selecting the operation in the plug-in, the list makes (5_3) _2 = = 4 equation set up all combinations of operations.

Op.c

//(5 _ 3) _ 2 = = 4#include<stdio.h>#include<glob.h>#include<string.h>#include<dlfcn.h>#ifndef PATH#definePATH "./plugin/*.plugin"#endiftypedefintopt_t (int,int); typedefstruct {    void*handle; opt_t*op; Charch; intInUse;} oper_t;#defineMAX 10intMainvoid) {oper_t Oparr[max];    glob_t Globbuf; intret, I, J; void*tmp; memset (Oparr,' /',sizeof(Oparr)); RET= Glob (PATH,0, NULL, &AMP;GLOBBUF);//All files that matched pathname is stored in globbuf     for(i =0; i < globbuf.gl_pathc; ++i) {oparr[i].handle=Dlopen (Globbuf.gl_pathv[i], rtld_lazy); if(!oparr[i].handle) {fprintf (stderr,"Dlopen error.\n"); return-1; } Oparr[i].op= Dlsym (Oparr[i].handle,"func"); TMP= Dlsym (Oparr[i].handle,"symbol"); oparr[i].ch= ((Char(*) (void)) () TMP); //oparr[i].ch = ((char (*) (void)) Dlsym (oparr[i].handle, "symbol")) ();Oparr[i].inuse=1; } globfree (&globbuf);  for(i =0; i < MAX; ++i) {if(Oparr[i].inuse = =0) {            Continue; }         for(j =0; J < MAX; ++j) {if(Oparr[j].inuse = =0) {                Continue; }            if(Oparr[j].op (Oparr[i].op (5,3),2) ==4) {printf ("(5%c 3)%c 2 = = 4\n", oparr[i].ch, oparr[j].ch); }        }    }     for(i =0; i < MAX; ++i) {if(Oparr[i].inuse = =0) {            Continue;    } dlclose (Oparr[i].handle); }    return 0;}

The code execution output is as follows when there is no plug-in:

Add the following file in the plugin directory and compile the generated dynamic library file:

Add.c

int func (intint  b) {    return a + b;} char symbol (void) {    return'+';}

Sub.c

int func (intint  b) {    return A- b;} char symbol (void) {    return'-';}

Mul.c

int func (intint  b) {    return A * b;} char symbol (void) {    return'*';}

Div.c

int func (intint  b) {    return A/ b;} char symbol (void) {    return'/';}

Mod.c

int func (intint  b) {    return a% b;} char symbol (void) {    return'%';}

Power.c

int func (intint  b) {    int  i, sum;      for 0 1; I < b; + +I    )            {*= a    ;    } return sum;} char symbol (void) {    return'^' ;}

The output is as follows:

In this example, the program finds all matching files through the Glob () function. The value of the specified symbol is queried one by one from the matching file by Dlsym (). and record the results of the query.

Using the recorded query results, the given numbers are matched in exhaustive, and the matching results are judged. If the result is true, the matching result is printed.

Plugins under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.