1 /* $Id$ */
2 /* Copyright (c) 2013 Pierre Pronchery <khorben@defora.org> */
3 /* This file is part of DeforaOS Desktop Phone */
4 /* This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 3 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>. */
15 
16 
17 
18 #include <dirent.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <dlfcn.h>
23 #include "Phone/modem.h"
24 
25 
26 /* private */
27 /* prototypes */
28 static int _modems(void);
29 
30 static int _dlerror(char const * message, int ret);
31 static int _error(char const * message, char const * error, int ret);
32 static int _perror(char const * message, int ret);
33 
34 
35 /* functions */
36 /* modems */
_modems(void)37 static int _modems(void)
38 {
39 	int ret = 0;
40 	const char path[] = "../src/modems";
41 #ifdef __APPLE__
42 	const char ext[] = ".dylib";
43 #else
44 	const char ext[] = ".so";
45 #endif
46 	DIR * dir;
47 	struct dirent * de;
48 	size_t len;
49 	char * s;
50 	void * p;
51 	ModemPluginDefinition * mpd;
52 
53 	if((dir = opendir(path)) == NULL)
54 		return -_perror(path, 1);
55 	while((de = readdir(dir)) != NULL)
56 	{
57 		if((len = strlen(de->d_name)) < sizeof(ext))
58 			continue;
59 		if(strcmp(&de->d_name[len - sizeof(ext) + 1], ext) != 0)
60 			continue;
61 		if((s = malloc(sizeof(path) + len + 1)) == NULL)
62 		{
63 			ret += _perror(de->d_name, 1);
64 			continue;
65 		}
66 		snprintf(s, sizeof(path) + len + 1, "%s/%s", path, de->d_name);
67 		if((p = dlopen(s, RTLD_LAZY)) == NULL)
68 		{
69 			ret += _dlerror(s, 1);
70 			free(s);
71 			continue;
72 		}
73 		if((mpd = dlsym(p, "plugin")) == NULL)
74 			ret += _dlerror(s, 1);
75 		else if(mpd->icon == NULL)
76 			ret += _error(s, "No icon defined", 1);
77 		else
78 			printf("\n%s: %s (%s)\n", de->d_name, mpd->name,
79 					mpd->icon);
80 		free(s);
81 		dlclose(p);
82 	}
83 	closedir(dir);
84 	return ret;
85 }
86 
87 
88 /* dlerror */
_dlerror(char const * message,int ret)89 static int _dlerror(char const * message, int ret)
90 {
91 	fputs("modems: ", stderr);
92 	fprintf(stderr, "%s: %s\n", message, dlerror());
93 	return ret;
94 }
95 
96 
97 /* error */
_error(char const * message,char const * error,int ret)98 static int _error(char const * message, char const * error, int ret)
99 {
100 	fputs("modems: ", stderr);
101 	fprintf(stderr, "%s: %s\n", message, error);
102 	return ret;
103 }
104 
105 
106 /* perror */
_perror(char const * message,int ret)107 static int _perror(char const * message, int ret)
108 {
109 	fputs("modems: ", stderr);
110 	perror(message);
111 	return ret;
112 }
113 
114 
115 /* public */
116 /* functions */
117 /* main */
main(void)118 int main(void)
119 {
120 	return (_modems() == 0) ? 0 : 2;
121 }
122