source: trunk/openjdk/jdk/src/share/bin/wildcard.c

Last change on this file was 278, checked in by dmik, 14 years ago

trunk: Merged in openjdk6 b22 from branches/vendor/oracle.

File size: 13.4 KB
Line 
1/*
2 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/*
27 * Class-Path Wildcards
28 *
29 * The syntax for wildcards is a single asterisk. The class path
30 * foo/"*", e.g., loads all jar files in the directory named foo.
31 * (This requires careful quotation when used in shell scripts.)
32 *
33 * Only files whose names end in .jar or .JAR are matched.
34 * Files whose names end in .zip, or which have a particular
35 * magic number, regardless of filename extension, are not
36 * matched.
37 *
38 * Files are considered regardless of whether or not they are
39 * "hidden" in the UNIX sense, i.e., have names beginning with '.'.
40 *
41 * A wildcard only matches jar files, not class files in the same
42 * directory. If you want to load both class files and jar files from
43 * a single directory foo then you can say foo:foo/"*", or foo/"*":foo
44 * if you want the jar files to take precedence.
45 *
46 * Subdirectories are not searched recursively, i.e., foo/"*" only
47 * looks for jar files in foo, not in foo/bar, foo/baz, etc.
48 *
49 * Expansion of wildcards is done early, prior to the invocation of a
50 * program's main method, rather than late, during the class-loading
51 * process itself. Each element of the input class path containing a
52 * wildcard is replaced by the (possibly empty) sequence of elements
53 * generated by enumerating the jar files in the named directory. If
54 * the directory foo contains a.jar, b.jar, and c.jar,
55 * e.g., then the class path foo/"*" is expanded into
56 * foo/a.jar:foo/b.jar:foo/c.jar, and that string would be the value
57 * of the system property java.class.path.
58 *
59 * The order in which the jar files in a directory are enumerated in
60 * the expanded class path is not specified and may vary from platform
61 * to platform and even from moment to moment on the same machine. A
62 * well-constructed application should not depend upon any particular
63 * order. If a specific order is required then the jar files can be
64 * enumerated explicitly in the class path.
65 *
66 * The CLASSPATH environment variable is not treated any differently
67 * from the -classpath (equiv. -cp) command-line option,
68 * i.e. wildcards are honored in all these cases.
69 *
70 * Class-path wildcards are not honored in the Class-Path jar-manifest
71 * header.
72 *
73 * Class-path wildcards are honored not only by the Java launcher but
74 * also by most other command-line tools that accept class paths, and
75 * in particular by javac and javadoc.
76 *
77 * Class-path wildcards are not honored in any other kind of path, and
78 * especially not in the bootstrap class path, which is a mere
79 * artifact of our implementation and not something that developers
80 * should use.
81 *
82 * Classpath wildcards are only expanded in the Java launcher code,
83 * supporting the use of wildcards on the command line and in the
84 * CLASSPATH environment variable. We do not support the use of
85 * wildcards by applications that embed the JVM.
86 */
87
88#include <stddef.h>
89#include <stdio.h>
90#include <stdlib.h>
91#include <string.h>
92#include <sys/types.h>
93#include "java.h" /* Strictly for PATH_SEPARATOR/FILE_SEPARATOR */
94#include "jli_util.h"
95
96#if defined(_WIN32)
97#include <windows.h>
98#elif defined(__WIN32OS2__)
99#include <windows.h>
100#ifdef __EMX__
101#include <unistd.h>
102#include <dirent.h>
103#endif
104#else /* Unix */
105#include <unistd.h>
106#include <dirent.h>
107#endif /* Unix */
108
109static int
110exists(const char* filename)
111{
112#ifdef _WIN32
113 return _access(filename, 0) == 0;
114#else
115 return access(filename, F_OK) == 0;
116#endif
117}
118
119#define NEW_(TYPE) ((TYPE) JLI_MemAlloc(sizeof(struct TYPE##_)))
120
121/*
122 * Wildcard directory iteration.
123 * WildcardIterator_for(wildcard) returns an iterator.
124 * Each call to that iterator's next() method returns the basename
125 * of an entry in the wildcard's directory. The basename's memory
126 * belongs to the iterator. The caller is responsible for prepending
127 * the directory name and file separator, if necessary.
128 * When done with the iterator, call the close method to clean up.
129 */
130typedef struct WildcardIterator_* WildcardIterator;
131
132#ifdef _WIN32
133struct WildcardIterator_
134{
135 HANDLE handle;
136 char *firstFile; /* Stupid FindFirstFile...FindNextFile */
137};
138
139static WildcardIterator
140WildcardIterator_for(const char *wildcard)
141{
142 WIN32_FIND_DATA find_data;
143 WildcardIterator it = NEW_(WildcardIterator);
144 HANDLE handle = FindFirstFile(wildcard, &find_data);
145 if (handle == INVALID_HANDLE_VALUE)
146 return NULL;
147 it->handle = handle;
148 it->firstFile = find_data.cFileName;
149 return it;
150}
151
152static char *
153WildcardIterator_next(WildcardIterator it)
154{
155 WIN32_FIND_DATA find_data;
156 if (it->firstFile != NULL) {
157 char *firstFile = it->firstFile;
158 it->firstFile = NULL;
159 return firstFile;
160 }
161 return FindNextFile(it->handle, &find_data)
162 ? find_data.cFileName : NULL;
163}
164
165static void
166WildcardIterator_close(WildcardIterator it)
167{
168 if (it) {
169 FindClose(it->handle);
170 JLI_MemFree(it->firstFile);
171 JLI_MemFree(it);
172 }
173}
174
175#else /* Unix */
176struct WildcardIterator_
177{
178 DIR *dir;
179};
180
181static WildcardIterator
182WildcardIterator_for(const char *wildcard)
183{
184 DIR *dir;
185 int wildlen = strlen(wildcard);
186 if (wildlen < 2) {
187 dir = opendir(".");
188 } else {
189 char *dirname = JLI_StringDup(wildcard);
190 dirname[wildlen - 1] = '\0';
191 dir = opendir(dirname);
192 JLI_MemFree(dirname);
193 }
194 if (dir == NULL)
195 return NULL;
196 else {
197 WildcardIterator it = NEW_(WildcardIterator);
198 it->dir = dir;
199 return it;
200 }
201}
202
203static char *
204WildcardIterator_next(WildcardIterator it)
205{
206 struct dirent* dirp = readdir(it->dir);
207 return dirp ? dirp->d_name : NULL;
208}
209
210static void
211WildcardIterator_close(WildcardIterator it)
212{
213 if (it) {
214 closedir(it->dir);
215 JLI_MemFree(it);
216 }
217}
218#endif /* Unix */
219
220static int
221equal(const char *s1, const char *s2)
222{
223 return strcmp(s1, s2) == 0;
224}
225
226/*
227 * FileList ADT - a dynamic list of C filenames
228 */
229struct FileList_
230{
231 char **files;
232 int size;
233 int capacity;
234};
235typedef struct FileList_ *FileList;
236
237static FileList
238FileList_new(int capacity)
239{
240 FileList fl = NEW_(FileList);
241 fl->capacity = capacity;
242 fl->files = (char **) JLI_MemAlloc(capacity * sizeof(fl->files[0]));
243 fl->size = 0;
244 return fl;
245}
246
247#ifdef DEBUG_WILDCARD
248static void
249FileList_print(FileList fl)
250{
251 int i;
252 putchar('[');
253 for (i = 0; i < fl->size; i++) {
254 if (i > 0) printf(", ");
255 printf("\"%s\"",fl->files[i]);
256 }
257 putchar(']');
258}
259#endif
260
261static void
262FileList_free(FileList fl)
263{
264 if (fl) {
265 if (fl->files) {
266 int i;
267 for (i = 0; i < fl->size; i++)
268 JLI_MemFree(fl->files[i]);
269 JLI_MemFree(fl->files);
270 }
271 JLI_MemFree(fl);
272 }
273}
274
275static void
276FileList_ensureCapacity(FileList fl, int capacity)
277{
278 if (fl->capacity < capacity) {
279 while (fl->capacity < capacity)
280 fl->capacity *= 2;
281 fl->files = JLI_MemRealloc(fl->files,
282 fl->capacity * sizeof(fl->files[0]));
283 }
284}
285
286static void
287FileList_add(FileList fl, char *file)
288{
289 FileList_ensureCapacity(fl, fl->size+1);
290 fl->files[fl->size++] = file;
291}
292
293static void
294FileList_addSubstring(FileList fl, const char *beg, int len)
295{
296 char *filename = (char *) JLI_MemAlloc(len+1);
297 memcpy(filename, beg, len);
298 filename[len] = '\0';
299 FileList_ensureCapacity(fl, fl->size+1);
300 fl->files[fl->size++] = filename;
301}
302
303static char *
304FileList_join(FileList fl, char sep)
305{
306 int i;
307 int size;
308 char *path;
309 char *p;
310 for (i = 0, size = 1; i < fl->size; i++)
311 size += strlen(fl->files[i]) + 1;
312
313 path = JLI_MemAlloc(size);
314
315 for (i = 0, p = path; i < fl->size; i++) {
316 int len = strlen(fl->files[i]);
317 if (i > 0) *p++ = sep;
318 memcpy(p, fl->files[i], len);
319 p += len;
320 }
321 *p = '\0';
322
323 return path;
324}
325
326static FileList
327FileList_split(const char *path, char sep)
328{
329 const char *p, *q;
330 int len = strlen(path);
331 int count;
332 FileList fl;
333 for (count = 1, p = path; p < path + len; p++)
334 count += (*p == sep);
335 fl = FileList_new(count);
336 for (p = path;;) {
337 for (q = p; q <= path + len; q++) {
338 if (*q == sep || *q == '\0') {
339 FileList_addSubstring(fl, p, q - p);
340 if (*q == '\0')
341 return fl;
342 p = q + 1;
343 }
344 }
345 }
346}
347
348static int
349isJarFileName(const char *filename)
350{
351 int len = strlen(filename);
352 return (len >= 4) &&
353 (filename[len - 4] == '.') &&
354 (equal(filename + len - 3, "jar") ||
355 equal(filename + len - 3, "JAR")) &&
356 /* Paranoia: Maybe filename is "DIR:foo.jar" */
357 (strchr(filename, PATH_SEPARATOR) == NULL);
358}
359
360static char *
361wildcardConcat(const char *wildcard, const char *basename)
362{
363 int wildlen = strlen(wildcard);
364 int baselen = strlen(basename);
365 char *filename = (char *) JLI_MemAlloc(wildlen + baselen);
366 /* Replace the trailing '*' with basename */
367 memcpy(filename, wildcard, wildlen-1);
368 memcpy(filename+wildlen-1, basename, baselen+1);
369 return filename;
370}
371
372static FileList
373wildcardFileList(const char *wildcard)
374{
375 const char *basename;
376 FileList fl = FileList_new(16);
377 WildcardIterator it = WildcardIterator_for(wildcard);
378 if (it == NULL)
379 return NULL;
380 while ((basename = WildcardIterator_next(it)) != NULL)
381 if (isJarFileName(basename))
382 FileList_add(fl, wildcardConcat(wildcard, basename));
383 WildcardIterator_close(it);
384 return fl;
385}
386
387static int
388isWildcard(const char *filename)
389{
390 int len = strlen(filename);
391 return (len > 0) &&
392 (filename[len - 1] == '*') &&
393 (len == 1 || IS_FILE_SEPARATOR(filename[len - 2])) &&
394 (! exists(filename));
395}
396
397static void
398FileList_expandWildcards(FileList fl)
399{
400 int i, j;
401 for (i = 0; i < fl->size; i++) {
402 if (isWildcard(fl->files[i])) {
403 FileList expanded = wildcardFileList(fl->files[i]);
404 if (expanded != NULL && expanded->size > 0) {
405 JLI_MemFree(fl->files[i]);
406 FileList_ensureCapacity(fl, fl->size + expanded->size);
407 for (j = fl->size - 1; j >= i+1; j--)
408 fl->files[j+expanded->size-1] = fl->files[j];
409 for (j = 0; j < expanded->size; j++)
410 fl->files[i+j] = expanded->files[j];
411 i += expanded->size - 1;
412 fl->size += expanded->size - 1;
413 /* fl expropriates expanded's elements. */
414 expanded->size = 0;
415 }
416 FileList_free(expanded);
417 }
418 }
419}
420
421const char *
422JLI_WildcardExpandClasspath(const char *classpath)
423{
424 char *expanded;
425 FileList fl;
426
427 if (strchr(classpath, '*') == NULL)
428 return classpath;
429 fl = FileList_split(classpath, PATH_SEPARATOR);
430 FileList_expandWildcards(fl);
431 expanded = FileList_join(fl, PATH_SEPARATOR);
432 FileList_free(fl);
433 if (getenv("_JAVA_LAUNCHER_DEBUG") != 0)
434 printf("Expanded wildcards:\n"
435 " before: \"%s\"\n"
436 " after : \"%s\"\n",
437 classpath, expanded);
438 return expanded;
439}
440
441#ifdef DEBUG_WILDCARD
442static void
443wildcardExpandArgv(const char ***argv)
444{
445 int i;
446 for (i = 0; (*argv)[i]; i++) {
447 if (equal((*argv)[i], "-cp") ||
448 equal((*argv)[i], "-classpath")) {
449 i++;
450 (*argv)[i] = wildcardExpandClasspath((*argv)[i]);
451 }
452 }
453}
454
455static void
456debugPrintArgv(char *argv[])
457{
458 int i;
459 putchar('[');
460 for (i = 0; argv[i]; i++) {
461 if (i > 0) printf(", ");
462 printf("\"%s\"", argv[i]);
463 }
464 printf("]\n");
465}
466
467int
468main(int argc, char *argv[])
469{
470 argv[0] = "java";
471 wildcardExpandArgv((const char***)&argv);
472 debugPrintArgv(argv);
473 /* execvp("java", argv); */
474 return 0;
475}
476#endif /* DEBUG_WILDCARD */
477
478/* Cute little perl prototype implementation....
479
480my $sep = ($^O =~ /^(Windows|cygwin)/) ? ";" : ":";
481
482sub expand($) {
483 opendir DIR, $_[0] or return $_[0];
484 join $sep, map {"$_[0]/$_"} grep {/\.(jar|JAR)$/} readdir DIR;
485}
486
487sub munge($) {
488 join $sep,
489 map {(! -r $_ and s/[\/\\]+\*$//) ? expand $_ : $_} split $sep, $_[0];
490}
491
492for (my $i = 0; $i < @ARGV - 1; $i++) {
493 $ARGV[$i+1] = munge $ARGV[$i+1] if $ARGV[$i] =~ /^-c(p|lasspath)$/;
494}
495
496$ENV{CLASSPATH} = munge $ENV{CLASSPATH} if exists $ENV{CLASSPATH};
497@ARGV = ("java", @ARGV);
498print "@ARGV\n";
499exec @ARGV;
500
501*/
Note: See TracBrowser for help on using the repository browser.