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, &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