Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/builtin/dlls.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1992-2012 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* David Korn <[email protected]> *
19
* *
20
***********************************************************************/
21
#pragma prototyped
22
23
/*
24
* Glenn Fowler
25
* AT&T Research
26
*
27
* dll scan interface
28
*/
29
30
static const char usage[] =
31
"[-?\n@(#)$Id: dlls (AT&T Research) 2011-04-22 $\n]"
32
USAGE_LICENSE
33
"[+NAME?dlls - list dlls and shared libraries on $PATH]"
34
"[+DESCRIPTION?\bdlls\b lists the base name and full path, one per line, "
35
"of \adlls\a and \ashared libraries\a found in the directories or "
36
"sibling directories in the \bPATH\b environment variable. The "
37
"\aplugin\a operand matches plugin libraries for the particular "
38
"\aplugin\a. If omitted or \b-\b then no plugins are matched. The "
39
"\aname\a operand is the library base name. All base names are matched "
40
"if omitted or \b-\b. The \aversion\a operand specifies a specific "
41
"library version and is an implementation specific sequence of decimal "
42
"digits and dots. The latest version is matched if omitted or \b-\b. "
43
"Only the first path for each library base name is listed. If no options "
44
"are specified then \b--base\b \b--path\b is assumed.]"
45
"[+?\asymbol\a operands, if specified, are searched for in each matched "
46
"library. The address or lookup error is listed for each library and "
47
"\asymbol\a.]"
48
"[b:base?List the \adll\a or \ashared library\a base names.]"
49
"[c:containing?Only list libraries containing at least one of the "
50
"\asymbol\a operands.]"
51
"[i:info?List native dll naming and location information.]"
52
"[l:long?List the base name, plugin YYYYMMDD version stamp, and full "
53
"path for each shared library. Libraries with no plugin version stamp "
54
"have version stamp 00000000.]"
55
"[p:path?List the \adll\a or \ashared library\a path names.]"
56
57
"\n\n"
58
"[ plugin [ name [ version [ symbol ... ] ] ] ]\n"
59
"\n"
60
61
"[+SEE ALSO?\bfind\b(1), \bdllscan\b(3)]"
62
;
63
64
#include <cmd.h>
65
#include <dlldefs.h>
66
67
#define LIST_BASE 0x01
68
#define LIST_INFO 0x02
69
#define LIST_LONG 0x04
70
#define LIST_ONLY 0x10
71
#define LIST_PATH 0x20
72
73
int
74
b_dlls(int argc, char** argv, Shbltin_t* context)
75
{
76
int i;
77
int r;
78
int flags;
79
int only;
80
unsigned long ver;
81
char** syms;
82
char* arg[3];
83
void* dll;
84
void* sym;
85
Dllscan_t* dls;
86
Dllent_t* dle;
87
Dllinfo_t* dli;
88
89
cmdinit(argc, argv, context, ERROR_CATALOG, 0);
90
flags = 0;
91
only = 0;
92
for (;;)
93
{
94
switch (optget(argv, usage))
95
{
96
case 0:
97
break;
98
case 'b':
99
flags |= LIST_BASE;
100
continue;
101
case 'c':
102
only = 1;
103
continue;
104
case 'i':
105
flags |= LIST_INFO;
106
continue;
107
case 'l':
108
flags |= LIST_LONG;
109
continue;
110
case 'p':
111
flags |= LIST_PATH;
112
continue;
113
case ':':
114
error(2, "%s", opt_info.arg);
115
continue;
116
case '?':
117
error(ERROR_usage(2), "%s", opt_info.arg);
118
continue;
119
}
120
break;
121
}
122
argv += opt_info.index;
123
if (error_info.errors)
124
error(ERROR_usage(2), "%s", optusage(NiL));
125
r = 0;
126
if (flags & LIST_INFO)
127
{
128
if (!(dli = dllinfo()))
129
error(2, "cannot determine native dll info");
130
sfprintf(sfstdout, "sibling=(%s", dli->sibling[0]);
131
for (i = 1; dli->sibling[i]; i++)
132
sfprintf(sfstdout, " %s", dli->sibling[i]);
133
sfprintf(sfstdout, ") env=%s prefix=%s suffix=%s flags=", dli->env, dli->prefix, dli->suffix);
134
i = 0;
135
if (dli->flags & DLL_INFO_PREVER)
136
{
137
i = 1;
138
sfprintf(sfstdout, "PREVER");
139
}
140
if (dli->flags & DLL_INFO_DOTVER)
141
{
142
if (i)
143
sfputc(sfstdout, '|');
144
sfprintf(sfstdout, "DOTVER");
145
}
146
sfputc(sfstdout, '\n');
147
}
148
else if (!(flags & (LIST_BASE|LIST_PATH)))
149
flags |= LIST_BASE|LIST_PATH;
150
if (flags &= (LIST_BASE|LIST_PATH|LIST_LONG))
151
{
152
for (i = 0; i < elementsof(arg); i++)
153
if (arg[i] = *argv)
154
argv++;
155
if (only && !*argv)
156
error(ERROR_usage(2), "%s", optusage(NiL));
157
r = 1;
158
for (;;)
159
{
160
if (dls = dllsopen(arg[0], arg[1], arg[2]))
161
{
162
while (dle = dllsread(dls))
163
{
164
r = 0;
165
if (!only)
166
{
167
if (!(flags & LIST_LONG))
168
dll = 0;
169
else if (dll = dlopen(dle->path, RTLD_LAZY))
170
ver = dllversion(dll, NiL);
171
else
172
ver = 0;
173
switch (flags)
174
{
175
case LIST_BASE:
176
sfprintf(sfstdout, "%s\n", dle->name);
177
break;
178
case LIST_BASE|LIST_LONG:
179
sfprintf(sfstdout, "%14s %08lu\n", dle->name, ver);
180
break;
181
case LIST_PATH|LIST_LONG:
182
sfprintf(sfstdout, "%08lu %s\n", ver, dle->path);
183
break;
184
case LIST_PATH:
185
sfprintf(sfstdout, "%s\n", dle->path);
186
break;
187
case LIST_BASE|LIST_PATH:
188
sfprintf(sfstdout, "%14s %s\n", dle->name, dle->path);
189
break;
190
default:
191
sfprintf(sfstdout, "%14s %08lu %s\n", dle->name, ver, dle->path);
192
break;
193
}
194
if (*(syms = argv))
195
{
196
if (dll || (dll = dlopen(dle->path, RTLD_LAZY)))
197
{
198
do
199
{
200
sfprintf(sfstdout, " %14s ", *syms);
201
if (sym = dlllook(dll, *syms))
202
sfprintf(sfstdout, "%p\n", sym);
203
else
204
sfprintf(sfstdout, "%s\n", dlerror());
205
} while (*++syms);
206
dlclose(dll);
207
}
208
else
209
sfprintf(sfstdout, " %s\n", dlerror());
210
}
211
}
212
else if (dll = dlopen(dle->path, RTLD_LAZY))
213
{
214
i = 1;
215
for (syms = argv; *syms; syms++)
216
if (sym = dlllook(dll, *syms))
217
{
218
if (i)
219
{
220
i = 0;
221
switch (flags)
222
{
223
case LIST_BASE:
224
sfprintf(sfstdout, "%s\n", dle->name);
225
break;
226
case LIST_PATH:
227
sfprintf(sfstdout, "%s\n", dle->path);
228
break;
229
default:
230
sfprintf(sfstdout, "%14s %s\n", dle->name, dle->path);
231
break;
232
}
233
}
234
sfprintf(sfstdout, " %14s %p\n", *syms, sym);
235
}
236
dlclose(dll);
237
}
238
}
239
dllsclose(dls);
240
}
241
if (!r || !arg[0] || streq(arg[0], "-") || !arg[1] || streq(arg[1], "-"))
242
break;
243
arg[0] = 0;
244
}
245
}
246
return r || error_info.errors;
247
}
248
249