sla@2327: /* sla@2327: * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. sla@2327: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. sla@2327: * sla@2327: * This code is free software; you can redistribute it and/or modify it sla@2327: * under the terms of the GNU General Public License version 2 only, as sla@2327: * published by the Free Software Foundation. sla@2327: * sla@2327: * This code is distributed in the hope that it will be useful, but WITHOUT sla@2327: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or sla@2327: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License sla@2327: * version 2 for more details (a copy is included in the LICENSE file that sla@2327: * accompanied this code). sla@2327: * sla@2327: * You should have received a copy of the GNU General Public License version sla@2327: * 2 along with this work; if not, write to the Free Software Foundation, sla@2327: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. sla@2327: * sla@2327: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA sla@2327: * or visit www.oracle.com if you need additional information or have any sla@2327: * questions. sla@2327: * sla@2327: */ sla@2327: sla@2327: /* sla@2327: * Class-Path Wildcards sla@2327: * sla@2327: * The syntax for wildcards is a single asterisk. The class path sla@2327: * foo/"*", e.g., loads all jar files in the directory named foo. sla@2327: * (This requires careful quotation when used in shell scripts.) sla@2327: * sla@2327: * Only files whose names end in .jar or .JAR are matched. sla@2327: * Files whose names end in .zip, or which have a particular sla@2327: * magic number, regardless of filename extension, are not sla@2327: * matched. sla@2327: * sla@2327: * Files are considered regardless of whether or not they are sla@2327: * "hidden" in the UNIX sense, i.e., have names beginning with '.'. sla@2327: * sla@2327: * A wildcard only matches jar files, not class files in the same sla@2327: * directory. If you want to load both class files and jar files from sla@2327: * a single directory foo then you can say foo:foo/"*", or foo/"*":foo sla@2327: * if you want the jar files to take precedence. sla@2327: * sla@2327: * Subdirectories are not searched recursively, i.e., foo/"*" only sla@2327: * looks for jar files in foo, not in foo/bar, foo/baz, etc. sla@2327: * sla@2327: * Expansion of wildcards is done early, prior to the invocation of a sla@2327: * program's main method, rather than late, during the class-loading sla@2327: * process itself. Each element of the input class path containing a sla@2327: * wildcard is replaced by the (possibly empty) sequence of elements sla@2327: * generated by enumerating the jar files in the named directory. If sla@2327: * the directory foo contains a.jar, b.jar, and c.jar, sla@2327: * e.g., then the class path foo/"*" is expanded into sla@2327: * foo/a.jar:foo/b.jar:foo/c.jar, and that string would be the value sla@2327: * of the system property java.class.path. sla@2327: * sla@2327: * The order in which the jar files in a directory are enumerated in sla@2327: * the expanded class path is not specified and may vary from platform sla@2327: * to platform and even from moment to moment on the same machine. A sla@2327: * well-constructed application should not depend upon any particular sla@2327: * order. If a specific order is required then the jar files can be sla@2327: * enumerated explicitly in the class path. sla@2327: * sla@2327: * The CLASSPATH environment variable is not treated any differently sla@2327: * from the -classpath (equiv. -cp) command-line option, sla@2327: * i.e. wildcards are honored in all these cases. sla@2327: * sla@2327: * Class-path wildcards are not honored in the Class-Path jar-manifest sla@2327: * header. sla@2327: * sla@2327: * Class-path wildcards are honored not only by the Java launcher but sla@2327: * also by most other command-line tools that accept class paths, and sla@2327: * in particular by javac and javadoc. sla@2327: * sla@2327: * Class-path wildcards are not honored in any other kind of path, and sla@2327: * especially not in the bootstrap class path, which is a mere sla@2327: * artifact of our implementation and not something that developers sla@2327: * should use. sla@2327: * sla@2327: * Classpath wildcards are only expanded in the Java launcher code, sla@2327: * supporting the use of wildcards on the command line and in the sla@2327: * CLASSPATH environment variable. We do not support the use of sla@2327: * wildcards by applications that embed the JVM. sla@2327: */ sla@2327: sla@2327: #include sla@2327: #include sla@2327: #include sla@2327: #include sla@2327: #include sla@2327: #include "java.h" /* Strictly for PATH_SEPARATOR/FILE_SEPARATOR */ sla@2327: #include "jli_util.h" sla@2327: sla@2327: #ifdef _WIN32 sla@2327: #include sla@2327: #else /* Unix */ sla@2327: #include sla@2327: #include sla@2327: #endif /* Unix */ sla@2327: sla@2327: static int sla@2327: exists(const char* filename) sla@2327: { sla@2327: #ifdef _WIN32 sla@2327: return _access(filename, 0) == 0; sla@2327: #else sla@2327: return access(filename, F_OK) == 0; sla@2327: #endif sla@2327: } sla@2327: sla@2327: #define NEW_(TYPE) ((TYPE) JLI_MemAlloc(sizeof(struct TYPE##_))) sla@2327: sla@2327: /* sla@2327: * Wildcard directory iteration. sla@2327: * WildcardIterator_for(wildcard) returns an iterator. sla@2327: * Each call to that iterator's next() method returns the basename sla@2327: * of an entry in the wildcard's directory. The basename's memory sla@2327: * belongs to the iterator. The caller is responsible for prepending sla@2327: * the directory name and file separator, if necessary. sla@2327: * When done with the iterator, call the close method to clean up. sla@2327: */ sla@2327: typedef struct WildcardIterator_* WildcardIterator; sla@2327: sla@2327: #ifdef _WIN32 sla@2327: struct WildcardIterator_ sla@2327: { sla@2327: HANDLE handle; sla@2327: char *firstFile; /* Stupid FindFirstFile...FindNextFile */ sla@2327: }; sla@2327: sla@2327: static WildcardIterator sla@2327: WildcardIterator_for(const char *wildcard) sla@2327: { sla@2327: WIN32_FIND_DATA find_data; sla@2327: WildcardIterator it = NEW_(WildcardIterator); sla@2327: HANDLE handle = FindFirstFile(wildcard, &find_data); sla@2327: if (handle == INVALID_HANDLE_VALUE) sla@2327: return NULL; sla@2327: it->handle = handle; sla@2327: it->firstFile = find_data.cFileName; sla@2327: return it; sla@2327: } sla@2327: sla@2327: static char * sla@2327: WildcardIterator_next(WildcardIterator it) sla@2327: { sla@2327: WIN32_FIND_DATA find_data; sla@2327: if (it->firstFile != NULL) { sla@2327: char *firstFile = it->firstFile; sla@2327: it->firstFile = NULL; sla@2327: return firstFile; sla@2327: } sla@2327: return FindNextFile(it->handle, &find_data) sla@2327: ? find_data.cFileName : NULL; sla@2327: } sla@2327: sla@2327: static void sla@2327: WildcardIterator_close(WildcardIterator it) sla@2327: { sla@2327: if (it) { sla@2327: FindClose(it->handle); sla@2327: JLI_MemFree(it->firstFile); sla@2327: JLI_MemFree(it); sla@2327: } sla@2327: } sla@2327: sla@2327: #else /* Unix */ sla@2327: struct WildcardIterator_ sla@2327: { sla@2327: DIR *dir; sla@2327: }; sla@2327: sla@2327: static WildcardIterator sla@2327: WildcardIterator_for(const char *wildcard) sla@2327: { sla@2327: DIR *dir; sla@2327: int wildlen = strlen(wildcard); sla@2327: if (wildlen < 2) { sla@2327: dir = opendir("."); sla@2327: } else { sla@2327: char *dirname = JLI_StringDup(wildcard); sla@2327: dirname[wildlen - 1] = '\0'; sla@2327: dir = opendir(dirname); sla@2327: JLI_MemFree(dirname); sla@2327: } sla@2327: if (dir == NULL) sla@2327: return NULL; sla@2327: else { sla@2327: WildcardIterator it = NEW_(WildcardIterator); sla@2327: it->dir = dir; sla@2327: return it; sla@2327: } sla@2327: } sla@2327: sla@2327: static char * sla@2327: WildcardIterator_next(WildcardIterator it) sla@2327: { sla@2327: struct dirent* dirp = readdir(it->dir); sla@2327: return dirp ? dirp->d_name : NULL; sla@2327: } sla@2327: sla@2327: static void sla@2327: WildcardIterator_close(WildcardIterator it) sla@2327: { sla@2327: if (it) { sla@2327: closedir(it->dir); sla@2327: JLI_MemFree(it); sla@2327: } sla@2327: } sla@2327: #endif /* Unix */ sla@2327: sla@2327: static int sla@2327: equal(const char *s1, const char *s2) sla@2327: { sla@2327: return strcmp(s1, s2) == 0; sla@2327: } sla@2327: sla@2327: /* sla@2327: * FileList ADT - a dynamic list of C filenames sla@2327: */ sla@2327: struct FileList_ sla@2327: { sla@2327: char **files; sla@2327: int size; sla@2327: int capacity; sla@2327: }; sla@2327: typedef struct FileList_ *FileList; sla@2327: sla@2327: static FileList sla@2327: FileList_new(int capacity) sla@2327: { sla@2327: FileList fl = NEW_(FileList); sla@2327: fl->capacity = capacity; sla@2327: fl->files = (char **) JLI_MemAlloc(capacity * sizeof(fl->files[0])); sla@2327: fl->size = 0; sla@2327: return fl; sla@2327: } sla@2327: sla@2327: #ifdef DEBUG_WILDCARD sla@2327: static void sla@2327: FileList_print(FileList fl) sla@2327: { sla@2327: int i; sla@2327: putchar('['); sla@2327: for (i = 0; i < fl->size; i++) { sla@2327: if (i > 0) printf(", "); sla@2327: printf("\"%s\"",fl->files[i]); sla@2327: } sla@2327: putchar(']'); sla@2327: } sla@2327: #endif sla@2327: sla@2327: static void sla@2327: FileList_free(FileList fl) sla@2327: { sla@2327: if (fl) { sla@2327: if (fl->files) { sla@2327: int i; sla@2327: for (i = 0; i < fl->size; i++) sla@2327: JLI_MemFree(fl->files[i]); sla@2327: JLI_MemFree(fl->files); sla@2327: } sla@2327: JLI_MemFree(fl); sla@2327: } sla@2327: } sla@2327: sla@2327: static void sla@2327: FileList_ensureCapacity(FileList fl, int capacity) sla@2327: { sla@2327: if (fl->capacity < capacity) { sla@2327: while (fl->capacity < capacity) sla@2327: fl->capacity *= 2; sla@2327: fl->files = JLI_MemRealloc(fl->files, sla@2327: fl->capacity * sizeof(fl->files[0])); sla@2327: } sla@2327: } sla@2327: sla@2327: static void sla@2327: FileList_add(FileList fl, char *file) sla@2327: { sla@2327: FileList_ensureCapacity(fl, fl->size+1); sla@2327: fl->files[fl->size++] = file; sla@2327: } sla@2327: sla@2327: static void sla@2327: FileList_addSubstring(FileList fl, const char *beg, int len) sla@2327: { sla@2327: char *filename = (char *) JLI_MemAlloc(len+1); sla@2327: memcpy(filename, beg, len); sla@2327: filename[len] = '\0'; sla@2327: FileList_ensureCapacity(fl, fl->size+1); sla@2327: fl->files[fl->size++] = filename; sla@2327: } sla@2327: sla@2327: static char * sla@2327: FileList_join(FileList fl, char sep) sla@2327: { sla@2327: int i; sla@2327: int size; sla@2327: char *path; sla@2327: char *p; sla@2327: for (i = 0, size = 1; i < fl->size; i++) sla@2327: size += strlen(fl->files[i]) + 1; sla@2327: sla@2327: path = JLI_MemAlloc(size); sla@2327: sla@2327: for (i = 0, p = path; i < fl->size; i++) { sla@2327: int len = strlen(fl->files[i]); sla@2327: if (i > 0) *p++ = sep; sla@2327: memcpy(p, fl->files[i], len); sla@2327: p += len; sla@2327: } sla@2327: *p = '\0'; sla@2327: sla@2327: return path; sla@2327: } sla@2327: sla@2327: static FileList sla@2327: FileList_split(const char *path, char sep) sla@2327: { sla@2327: const char *p, *q; sla@2327: int len = strlen(path); sla@2327: int count; sla@2327: FileList fl; sla@2327: for (count = 1, p = path; p < path + len; p++) sla@2327: count += (*p == sep); sla@2327: fl = FileList_new(count); sla@2327: for (p = path;;) { sla@2327: for (q = p; q <= path + len; q++) { sla@2327: if (*q == sep || *q == '\0') { sla@2327: FileList_addSubstring(fl, p, q - p); sla@2327: if (*q == '\0') sla@2327: return fl; sla@2327: p = q + 1; sla@2327: } sla@2327: } sla@2327: } sla@2327: } sla@2327: sla@2327: static int sla@2327: isJarFileName(const char *filename) sla@2327: { sla@2327: int len = strlen(filename); sla@2327: return (len >= 4) && sla@2327: (filename[len - 4] == '.') && sla@2327: (equal(filename + len - 3, "jar") || sla@2327: equal(filename + len - 3, "JAR")) && sla@2327: /* Paranoia: Maybe filename is "DIR:foo.jar" */ sla@2327: (strchr(filename, PATH_SEPARATOR) == NULL); sla@2327: } sla@2327: sla@2327: static char * sla@2327: wildcardConcat(const char *wildcard, const char *basename) sla@2327: { sla@2327: int wildlen = strlen(wildcard); sla@2327: int baselen = strlen(basename); sla@2327: char *filename = (char *) JLI_MemAlloc(wildlen + baselen); sla@2327: /* Replace the trailing '*' with basename */ sla@2327: memcpy(filename, wildcard, wildlen-1); sla@2327: memcpy(filename+wildlen-1, basename, baselen+1); sla@2327: return filename; sla@2327: } sla@2327: sla@2327: static FileList sla@2327: wildcardFileList(const char *wildcard) sla@2327: { sla@2327: const char *basename; sla@2327: FileList fl = FileList_new(16); sla@2327: WildcardIterator it = WildcardIterator_for(wildcard); ccheung@4887: if (it == NULL) { ccheung@4887: FileList_free(fl); sla@2327: return NULL; ccheung@4887: } sla@2327: while ((basename = WildcardIterator_next(it)) != NULL) sla@2327: if (isJarFileName(basename)) sla@2327: FileList_add(fl, wildcardConcat(wildcard, basename)); sla@2327: WildcardIterator_close(it); sla@2327: return fl; sla@2327: } sla@2327: sla@2327: static int sla@2327: isWildcard(const char *filename) sla@2327: { sla@2327: int len = strlen(filename); sla@2327: return (len > 0) && sla@2327: (filename[len - 1] == '*') && sla@2327: (len == 1 || IS_FILE_SEPARATOR(filename[len - 2])) && sla@2327: (! exists(filename)); sla@2327: } sla@2327: sla@2327: static void sla@2327: FileList_expandWildcards(FileList fl) sla@2327: { sla@2327: int i, j; sla@2327: for (i = 0; i < fl->size; i++) { sla@2327: if (isWildcard(fl->files[i])) { sla@2327: FileList expanded = wildcardFileList(fl->files[i]); sla@2327: if (expanded != NULL && expanded->size > 0) { sla@2327: JLI_MemFree(fl->files[i]); sla@2327: FileList_ensureCapacity(fl, fl->size + expanded->size); sla@2327: for (j = fl->size - 1; j >= i+1; j--) sla@2327: fl->files[j+expanded->size-1] = fl->files[j]; sla@2327: for (j = 0; j < expanded->size; j++) sla@2327: fl->files[i+j] = expanded->files[j]; sla@2327: i += expanded->size - 1; sla@2327: fl->size += expanded->size - 1; sla@2327: /* fl expropriates expanded's elements. */ sla@2327: expanded->size = 0; sla@2327: } sla@2327: FileList_free(expanded); sla@2327: } sla@2327: } sla@2327: } sla@2327: sla@2327: const char * sla@2327: JLI_WildcardExpandClasspath(const char *classpath) sla@2327: { sla@2327: char *expanded; sla@2327: FileList fl; sla@2327: sla@2327: if (strchr(classpath, '*') == NULL) sla@2327: return classpath; sla@2327: fl = FileList_split(classpath, PATH_SEPARATOR); sla@2327: FileList_expandWildcards(fl); sla@2327: expanded = FileList_join(fl, PATH_SEPARATOR); sla@2327: FileList_free(fl); sla@2327: if (getenv("_JAVA_LAUNCHER_DEBUG") != 0) sla@2327: printf("Expanded wildcards:\n" sla@2327: " before: \"%s\"\n" sla@2327: " after : \"%s\"\n", sla@2327: classpath, expanded); sla@2327: return expanded; sla@2327: } sla@2327: sla@2327: #ifdef DEBUG_WILDCARD sla@2327: static void sla@2327: wildcardExpandArgv(const char ***argv) sla@2327: { sla@2327: int i; sla@2327: for (i = 0; (*argv)[i]; i++) { sla@2327: if (equal((*argv)[i], "-cp") || sla@2327: equal((*argv)[i], "-classpath")) { sla@2327: i++; sla@2327: (*argv)[i] = wildcardExpandClasspath((*argv)[i]); sla@2327: } sla@2327: } sla@2327: } sla@2327: sla@2327: static void sla@2327: debugPrintArgv(char *argv[]) sla@2327: { sla@2327: int i; sla@2327: putchar('['); sla@2327: for (i = 0; argv[i]; i++) { sla@2327: if (i > 0) printf(", "); sla@2327: printf("\"%s\"", argv[i]); sla@2327: } sla@2327: printf("]\n"); sla@2327: } sla@2327: sla@2327: int sla@2327: main(int argc, char *argv[]) sla@2327: { sla@2327: argv[0] = "java"; sla@2327: wildcardExpandArgv((const char***)&argv); sla@2327: debugPrintArgv(argv); sla@2327: /* execvp("java", argv); */ sla@2327: return 0; sla@2327: } sla@2327: #endif /* DEBUG_WILDCARD */ sla@2327: sla@2327: /* Cute little perl prototype implementation.... sla@2327: sla@2327: my $sep = ($^O =~ /^(Windows|cygwin)/) ? ";" : ":"; sla@2327: sla@2327: sub expand($) { sla@2327: opendir DIR, $_[0] or return $_[0]; sla@2327: join $sep, map {"$_[0]/$_"} grep {/\.(jar|JAR)$/} readdir DIR; sla@2327: } sla@2327: sla@2327: sub munge($) { sla@2327: join $sep, sla@2327: map {(! -r $_ and s/[\/\\]+\*$//) ? expand $_ : $_} split $sep, $_[0]; sla@2327: } sla@2327: sla@2327: for (my $i = 0; $i < @ARGV - 1; $i++) { sla@2327: $ARGV[$i+1] = munge $ARGV[$i+1] if $ARGV[$i] =~ /^-c(p|lasspath)$/; sla@2327: } sla@2327: sla@2327: $ENV{CLASSPATH} = munge $ENV{CLASSPATH} if exists $ENV{CLASSPATH}; sla@2327: @ARGV = ("java", @ARGV); sla@2327: print "@ARGV\n"; sla@2327: exec @ARGV; sla@2327: sla@2327: */