Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/bin/wildcard.c
38767 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Class-Path Wildcards27*28* The syntax for wildcards is a single asterisk. The class path29* foo/"*", e.g., loads all jar files in the directory named foo.30* (This requires careful quotation when used in shell scripts.)31*32* Only files whose names end in .jar or .JAR are matched.33* Files whose names end in .zip, or which have a particular34* magic number, regardless of filename extension, are not35* matched.36*37* Files are considered regardless of whether or not they are38* "hidden" in the UNIX sense, i.e., have names beginning with '.'.39*40* A wildcard only matches jar files, not class files in the same41* directory. If you want to load both class files and jar files from42* a single directory foo then you can say foo:foo/"*", or foo/"*":foo43* if you want the jar files to take precedence.44*45* Subdirectories are not searched recursively, i.e., foo/"*" only46* looks for jar files in foo, not in foo/bar, foo/baz, etc.47*48* Expansion of wildcards is done early, prior to the invocation of a49* program's main method, rather than late, during the class-loading50* process itself. Each element of the input class path containing a51* wildcard is replaced by the (possibly empty) sequence of elements52* generated by enumerating the jar files in the named directory. If53* the directory foo contains a.jar, b.jar, and c.jar,54* e.g., then the class path foo/"*" is expanded into55* foo/a.jar:foo/b.jar:foo/c.jar, and that string would be the value56* of the system property java.class.path.57*58* The order in which the jar files in a directory are enumerated in59* the expanded class path is not specified and may vary from platform60* to platform and even from moment to moment on the same machine. A61* well-constructed application should not depend upon any particular62* order. If a specific order is required then the jar files can be63* enumerated explicitly in the class path.64*65* The CLASSPATH environment variable is not treated any differently66* from the -classpath (equiv. -cp) command-line option,67* i.e. wildcards are honored in all these cases.68*69* Class-path wildcards are not honored in the Class-Path jar-manifest70* header.71*72* Class-path wildcards are honored not only by the Java launcher but73* also by most other command-line tools that accept class paths, and74* in particular by javac and javadoc.75*76* Class-path wildcards are not honored in any other kind of path, and77* especially not in the bootstrap class path, which is a mere78* artifact of our implementation and not something that developers79* should use.80*81* Classpath wildcards are only expanded in the Java launcher code,82* supporting the use of wildcards on the command line and in the83* CLASSPATH environment variable. We do not support the use of84* wildcards by applications that embed the JVM.85*/8687#include <stddef.h>88#include <stdio.h>89#include <stdlib.h>90#include <string.h>91#include <sys/types.h>92#include "java.h" /* Strictly for PATH_SEPARATOR/FILE_SEPARATOR */93#include "jli_util.h"9495#ifdef _WIN3296#include <windows.h>97#else /* Unix */98#include <unistd.h>99#include <dirent.h>100#endif /* Unix */101102static int103exists(const char* filename)104{105#ifdef _WIN32106return _access(filename, 0) == 0;107#else108return access(filename, F_OK) == 0;109#endif110}111112#define NEW_(TYPE) ((TYPE) JLI_MemAlloc(sizeof(struct TYPE##_)))113114/*115* Wildcard directory iteration.116* WildcardIterator_for(wildcard) returns an iterator.117* Each call to that iterator's next() method returns the basename118* of an entry in the wildcard's directory. The basename's memory119* belongs to the iterator. The caller is responsible for prepending120* the directory name and file separator, if necessary.121* When done with the iterator, call the close method to clean up.122*/123typedef struct WildcardIterator_* WildcardIterator;124125#ifdef _WIN32126struct WildcardIterator_127{128HANDLE handle;129char *firstFile; /* Stupid FindFirstFile...FindNextFile */130};131// since this is used repeatedly we keep it here.132static WIN32_FIND_DATA find_data;133static WildcardIterator134WildcardIterator_for(const char *wildcard)135{136WildcardIterator it = NEW_(WildcardIterator);137HANDLE handle = FindFirstFile(wildcard, &find_data);138if (handle == INVALID_HANDLE_VALUE) {139JLI_MemFree(it);140return NULL;141}142it->handle = handle;143it->firstFile = find_data.cFileName;144return it;145}146147static char *148WildcardIterator_next(WildcardIterator it)149{150if (it->firstFile != NULL) {151char *firstFile = it->firstFile;152it->firstFile = NULL;153return firstFile;154}155return FindNextFile(it->handle, &find_data)156? find_data.cFileName : NULL;157}158159static void160WildcardIterator_close(WildcardIterator it)161{162if (it) {163FindClose(it->handle);164JLI_MemFree(it->firstFile);165JLI_MemFree(it);166}167}168169#else /* Unix */170struct WildcardIterator_171{172DIR *dir;173};174175static WildcardIterator176WildcardIterator_for(const char *wildcard)177{178DIR *dir;179int wildlen = JLI_StrLen(wildcard);180if (wildlen < 2) {181dir = opendir(".");182} else {183char *dirname = JLI_StringDup(wildcard);184dirname[wildlen - 1] = '\0';185dir = opendir(dirname);186JLI_MemFree(dirname);187}188if (dir == NULL)189return NULL;190else {191WildcardIterator it = NEW_(WildcardIterator);192it->dir = dir;193return it;194}195}196197static char *198WildcardIterator_next(WildcardIterator it)199{200struct dirent* dirp = readdir(it->dir);201return dirp ? dirp->d_name : NULL;202}203204static void205WildcardIterator_close(WildcardIterator it)206{207if (it) {208closedir(it->dir);209JLI_MemFree(it);210}211}212#endif /* Unix */213214static int215equal(const char *s1, const char *s2)216{217return JLI_StrCmp(s1, s2) == 0;218}219220/*221* FileList ADT - a dynamic list of C filenames222*/223struct FileList_224{225char **files;226int size;227int capacity;228};229typedef struct FileList_ *FileList;230231static FileList232FileList_new(int capacity)233{234FileList fl = NEW_(FileList);235fl->capacity = capacity;236fl->files = (char **) JLI_MemAlloc(capacity * sizeof(fl->files[0]));237fl->size = 0;238return fl;239}240241242243static void244FileList_free(FileList fl)245{246if (fl) {247if (fl->files) {248int i;249for (i = 0; i < fl->size; i++)250JLI_MemFree(fl->files[i]);251JLI_MemFree(fl->files);252}253JLI_MemFree(fl);254}255}256257static void258FileList_ensureCapacity(FileList fl, int capacity)259{260if (fl->capacity < capacity) {261while (fl->capacity < capacity)262fl->capacity *= 2;263fl->files = JLI_MemRealloc(fl->files,264fl->capacity * sizeof(fl->files[0]));265}266}267268static void269FileList_add(FileList fl, char *file)270{271FileList_ensureCapacity(fl, fl->size+1);272fl->files[fl->size++] = file;273}274275static void276FileList_addSubstring(FileList fl, const char *beg, int len)277{278char *filename = (char *) JLI_MemAlloc(len+1);279memcpy(filename, beg, len);280filename[len] = '\0';281FileList_ensureCapacity(fl, fl->size+1);282fl->files[fl->size++] = filename;283}284285static char *286FileList_join(FileList fl, char sep)287{288int i;289int size;290char *path;291char *p;292for (i = 0, size = 1; i < fl->size; i++)293size += (int)JLI_StrLen(fl->files[i]) + 1;294295path = JLI_MemAlloc(size);296297for (i = 0, p = path; i < fl->size; i++) {298int len = (int)JLI_StrLen(fl->files[i]);299if (i > 0) *p++ = sep;300memcpy(p, fl->files[i], len);301p += len;302}303*p = '\0';304305return path;306}307308static FileList309FileList_split(const char *path, char sep)310{311const char *p, *q;312int len = (int)JLI_StrLen(path);313int count;314FileList fl;315for (count = 1, p = path; p < path + len; p++)316count += (*p == sep);317fl = FileList_new(count);318for (p = path;;) {319for (q = p; q <= path + len; q++) {320if (*q == sep || *q == '\0') {321FileList_addSubstring(fl, p, q - p);322if (*q == '\0')323return fl;324p = q + 1;325}326}327}328}329330static int331isJarFileName(const char *filename)332{333int len = (int)JLI_StrLen(filename);334return (len >= 4) &&335(filename[len - 4] == '.') &&336(equal(filename + len - 3, "jar") ||337equal(filename + len - 3, "JAR")) &&338/* Paranoia: Maybe filename is "DIR:foo.jar" */339(JLI_StrChr(filename, PATH_SEPARATOR) == NULL);340}341342static char *343wildcardConcat(const char *wildcard, const char *basename)344{345int wildlen = (int)JLI_StrLen(wildcard);346int baselen = (int)JLI_StrLen(basename);347char *filename = (char *) JLI_MemAlloc(wildlen + baselen);348/* Replace the trailing '*' with basename */349memcpy(filename, wildcard, wildlen-1);350memcpy(filename+wildlen-1, basename, baselen+1);351return filename;352}353354static FileList355wildcardFileList(const char *wildcard)356{357const char *basename;358FileList fl = FileList_new(16);359WildcardIterator it = WildcardIterator_for(wildcard);360361if (it == NULL)362{363FileList_free(fl);364return NULL;365}366367while ((basename = WildcardIterator_next(it)) != NULL)368if (isJarFileName(basename))369FileList_add(fl, wildcardConcat(wildcard, basename));370WildcardIterator_close(it);371return fl;372}373374static int375isWildcard(const char *filename)376{377int len = (int)JLI_StrLen(filename);378return (len > 0) &&379(filename[len - 1] == '*') &&380(len == 1 || IS_FILE_SEPARATOR(filename[len - 2])) &&381(! exists(filename));382}383384static void385FileList_expandWildcards(FileList fl)386{387int i, j;388for (i = 0; i < fl->size; i++) {389if (isWildcard(fl->files[i])) {390FileList expanded = wildcardFileList(fl->files[i]);391if (expanded != NULL && expanded->size > 0) {392JLI_MemFree(fl->files[i]);393FileList_ensureCapacity(fl, fl->size + expanded->size);394for (j = fl->size - 1; j >= i+1; j--)395fl->files[j+expanded->size-1] = fl->files[j];396for (j = 0; j < expanded->size; j++)397fl->files[i+j] = expanded->files[j];398i += expanded->size - 1;399fl->size += expanded->size - 1;400/* fl expropriates expanded's elements. */401expanded->size = 0;402}403FileList_free(expanded);404}405}406}407408const char *409JLI_WildcardExpandClasspath(const char *classpath)410{411char *expanded;412FileList fl;413414if (JLI_StrChr(classpath, '*') == NULL)415return classpath;416fl = FileList_split(classpath, PATH_SEPARATOR);417FileList_expandWildcards(fl);418expanded = FileList_join(fl, PATH_SEPARATOR);419FileList_free(fl);420if (getenv(JLDEBUG_ENV_ENTRY) != 0)421printf("Expanded wildcards:\n"422" before: \"%s\"\n"423" after : \"%s\"\n",424classpath, expanded);425return expanded;426}427428#ifdef DEBUG_WILDCARD429static void430FileList_print(FileList fl)431{432int i;433putchar('[');434for (i = 0; i < fl->size; i++) {435if (i > 0) printf(", ");436printf("\"%s\"",fl->files[i]);437}438putchar(']');439}440441static void442wildcardExpandArgv(const char ***argv)443{444int i;445for (i = 0; (*argv)[i]; i++) {446if (equal((*argv)[i], "-cp") ||447equal((*argv)[i], "-classpath")) {448i++;449(*argv)[i] = wildcardExpandClasspath((*argv)[i]);450}451}452}453454static void455debugPrintArgv(char *argv[])456{457int i;458putchar('[');459for (i = 0; argv[i]; i++) {460if (i > 0) printf(", ");461printf("\"%s\"", argv[i]);462}463printf("]\n");464}465466int467main(int argc, char *argv[])468{469argv[0] = "java";470wildcardExpandArgv((const char***)&argv);471debugPrintArgv(argv);472/* execvp("java", argv); */473return 0;474}475#endif /* DEBUG_WILDCARD */476477/* Cute little perl prototype implementation....478479my $sep = ($^O =~ /^(Windows|cygwin)/) ? ";" : ":";480481sub expand($) {482opendir DIR, $_[0] or return $_[0];483join $sep, map {"$_[0]/$_"} grep {/\.(jar|JAR)$/} readdir DIR;484}485486sub munge($) {487join $sep,488map {(! -r $_ and s/[\/\\]+\*$//) ? expand $_ : $_} split $sep, $_[0];489}490491for (my $i = 0; $i < @ARGV - 1; $i++) {492$ARGV[$i+1] = munge $ARGV[$i+1] if $ARGV[$i] =~ /^-c(p|lasspath)$/;493}494495$ENV{CLASSPATH} = munge $ENV{CLASSPATH} if exists $ENV{CLASSPATH};496@ARGV = ("java", @ARGV);497print "@ARGV\n";498exec @ARGV;499500*/501502503