📄 loader.c.svn-base
字号:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <dirent.h>#include <errno.h>#include <dlfcn.h>#include "loader.h"#include "plugin.h"intmain(int argc, char **argv) { PluginList *plugin_head = NULL; if (argc < 2) { show_help(argv[0]); return -1; } plugin_head = get_plugin_list(argv[1]); if (plugin_head == NULL) { printf("No Plugins found\n"); return -1; } PluginList *plugin_list; /* Init Funcs */ plugin_list = plugin_head; while (plugin_list != NULL) { printf(" [ %s ] \n", plugin_list->plugin->name); plugin_list->plugin->init(); plugin_list = plugin_list->next; } /* CleanUp Funcs */ plugin_list = plugin_head; while (plugin_list != NULL) { printf(" [ %s ] \n", plugin_list->plugin->name); plugin_list->plugin->cleanup(); plugin_list = plugin_list->next; } return 0;}voidshow_help(char *bin) { printf("%s <plugin_dir>\n", bin);} char *process_dir(char *path) { static char pwd[FILENAME_MAX]; char tail; if (!(strncmp("/", path, 1))) { strncpy(pwd, path, strlen(path)); goto tail; } getcwd(pwd, FILENAME_MAX); // FILENAME_MAX = 4096 (stdio.h) strcat(pwd, "/"); strcat(pwd, path); tail: tail = pwd[strlen(pwd) - 1]; if (strncmp("/", &tail, 1)) strcat(pwd, "/"); return pwd;}PluginList *get_plugin_list(char *path) { char *err; DIR *dp; struct dirent *ds; char *pname; void *module; PluginList *head = NULL; PluginList *list = NULL; path = process_dir(path); dp = opendir(path); printf(">> %s <<\n", path); if (dp == NULL) { perror("opendir()"); return NULL; } ds = readdir(dp); while(ds) { if (!strncmp("lib", ds->d_name, 3)) { pname = malloc( strlen(path) + strlen(ds->d_name) ); sprintf(pname, "%s%s", path, ds->d_name); module = dlopen(pname, RTLD_NOW); err = dlerror(); if (err) { // printf ("%03d:%s\n", __LINE__, err); if (module) dlclose (module); goto next; } plugin_t func = dlsym(module, "get_plugin_info"); err = dlerror(); if (err) { // printf("%03d:%s\n", __LINE__, err); if (module) dlclose (module); goto next; } list = (PluginList *)malloc(sizeof(PluginList)); list->plugin = func(); list->next = head; head = list; printf("Added: %s\n", pname); } else goto next; next: ds = readdir(dp); } return head; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -