summaryrefslogtreecommitdiff
path: root/devel/pkgconf/files/patch-pkg.c
blob: 128a5ef69123b202452e47d55a61ad4aeea8636b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
--- ./pkg.c.orig	2012-07-29 11:35:32.856917000 +0200
+++ ./pkg.c	2012-07-29 11:31:53.000000000 +0200
@@ -142,11 +142,18 @@
 {
 	pkg_t *pkg;
 	char readbuf[PKG_BUFSIZE];
+	char *idptr;
 
 	pkg = calloc(sizeof(pkg_t), 1);
 	pkg->filename = strdup(filename);
 	pkg->vars = pkg_tuple_add(pkg->vars, "pcfiledir", pkg_get_parent_dir(pkg));
 
+	/* make module id */
+	pkg->id = strdup(basename(pkg->filename));
+	idptr = strrchr(pkg->id, '.');
+	if (idptr)
+		*idptr = '\0';
+
 	while (pkg_fgetline(readbuf, PKG_BUFSIZE, f) != NULL)
 	{
 		char op, *p, *key = NULL, *value = NULL;
@@ -274,6 +281,79 @@
 	return pkg;
 }
 
+static void
+pkg_scan_dir(const char *path, pkg_iteration_func_t func)
+{
+	DIR *dir;
+	struct dirent *dirent;
+
+	dir = opendir(path);
+	if (dir == NULL)
+		return;
+
+	for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir))
+	{
+		static char filebuf[PKG_BUFSIZE];
+		pkg_t *pkg;
+		FILE *f;
+		struct stat st;
+
+		strlcpy(filebuf, path, sizeof filebuf);
+		strlcat(filebuf, "/", sizeof filebuf);
+		strlcat(filebuf, dirent->d_name, sizeof filebuf);
+
+		stat(filebuf, &st);
+		if (!(S_ISREG(st.st_mode)))
+			continue;
+
+		f = fopen(filebuf, "r");
+		if (f == NULL)
+			continue;
+
+		pkg = pkg_new_from_file(filebuf, f);
+		if (pkg != NULL)
+		{
+			func(pkg);
+			pkg_free(pkg);
+		}
+	}
+
+	closedir(dir);
+}
+
+void
+pkg_scan(const char *search_path, pkg_iteration_func_t func)
+{
+	char **path = NULL;
+	size_t count = 0, iter = 0;
+
+	/* PKG_CONFIG_PATH has to take precedence */
+	if (search_path == NULL)
+		return;
+
+	count = path_split(search_path, &path);
+
+	for (iter = 0; iter < count; iter++)
+		pkg_scan_dir(path[iter], func);
+
+	path_free(path, count);
+}
+
+void
+pkg_scan_all(pkg_iteration_func_t func)
+{
+	char *path;
+
+	path = getenv("PKG_CONFIG_PATH");
+	if (path)
+	{
+		pkg_scan(path, func);
+		return;
+	}
+
+	pkg_scan(get_pkgconfig_path(), func);
+}
+
 #ifdef _WIN32
 pkg_t *
 pkg_find_in_registry_key(HKEY hkey, const char *name, unsigned int flags)