1 /* 2 * Copyright (c) 2005, 2012, 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 #ifdef _WIN32 97 #include <windows.h> 98 #else /* Unix */ 99 #include <unistd.h> 100 #include <dirent.h> 101 #endif /* Unix */ 102 103 static int 104 exists(const char* filename) 105 { 106 #ifdef _WIN32 107 return _access(filename, 0) == 0; 108 #else 109 return access(filename, F_OK) == 0; 110 #endif 111 } 112 113 #define NEW_(TYPE) ((TYPE) JLI_MemAlloc(sizeof(struct TYPE##_))) 114 115 /* 116 * Wildcard directory iteration. 117 * WildcardIterator_for(wildcard) returns an iterator. 118 * Each call to that iterator's next() method returns the basename 119 * of an entry in the wildcard's directory. The basename's memory 120 * belongs to the iterator. The caller is responsible for prepending 121 * the directory name and file separator, if necessary. 122 * When done with the iterator, call the close method to clean up. 123 */ 124 typedef struct WildcardIterator_* WildcardIterator; 125 126 #ifdef _WIN32 127 struct WildcardIterator_ 128 { 129 HANDLE handle; 130 char *firstFile; /* Stupid FindFirstFile...FindNextFile */ 131 }; 132 // since this is used repeatedly we keep it here. 133 static WIN32_FIND_DATA find_data; 134 static WildcardIterator 135 WildcardIterator_for(const char *wildcard) 136 { 137 WildcardIterator it = NEW_(WildcardIterator); 138 HANDLE handle = FindFirstFile(wildcard, &find_data); 139 if (handle == INVALID_HANDLE_VALUE) 140 return NULL; 141 it->handle = handle; 142 it->firstFile = find_data.cFileName; 143 return it; 144 } 145 146 static char * 147 WildcardIterator_next(WildcardIterator it) 148 { 149 if (it->firstFile != NULL) { 150 char *firstFile = it->firstFile; 151 it->firstFile = NULL; 152 return firstFile; 153 } 154 return FindNextFile(it->handle, &find_data) 155 ? find_data.cFileName : NULL; 156 } 157 158 static void 159 WildcardIterator_close(WildcardIterator it) 160 { 161 if (it) { 162 FindClose(it->handle); 163 JLI_MemFree(it->firstFile); 164 JLI_MemFree(it); 165 } 166 } 167 168 #else /* Unix */ 169 struct WildcardIterator_ 170 { 171 DIR *dir; 172 }; 173 174 static WildcardIterator 175 WildcardIterator_for(const char *wildcard) 176 { 177 DIR *dir; 178 int wildlen = JLI_StrLen(wildcard); 179 if (wildlen < 2) { 180 dir = opendir("."); 181 } else { 182 char *dirname = JLI_StringDup(wildcard); 183 dirname[wildlen - 1] = '\0'; 184 dir = opendir(dirname); 185 JLI_MemFree(dirname); 186 } 187 if (dir == NULL) 188 return NULL; 189 else { 190 WildcardIterator it = NEW_(WildcardIterator); 191 it->dir = dir; 192 return it; 193 } 194 } 195 196 static char * 197 WildcardIterator_next(WildcardIterator it) 198 { 199 struct dirent* dirp = readdir(it->dir); 200 return dirp ? dirp->d_name : NULL; 201 } 202 203 static void 204 WildcardIterator_close(WildcardIterator it) 205 { 206 if (it) { 207 closedir(it->dir); 208 JLI_MemFree(it); 209 } 210 } 211 #endif /* Unix */ 212 213 static int 214 equal(const char *s1, const char *s2) 215 { 216 return JLI_StrCmp(s1, s2) == 0; 217 } 218 219 /* 220 * FileList ADT - a dynamic list of C filenames 221 */ 222 struct FileList_ 223 { 224 char **files; 225 int size; 226 int capacity; 227 }; 228 typedef struct FileList_ *FileList; 229 230 static FileList 231 FileList_new(int capacity) 232 { 233 FileList fl = NEW_(FileList); 234 fl->capacity = capacity; 235 fl->files = (char **) JLI_MemAlloc(capacity * sizeof(fl->files[0])); 236 fl->size = 0; 237 return fl; 238 } 239 240 241 242 static void 243 FileList_free(FileList fl) 244 { 245 if (fl) { 246 if (fl->files) { 247 int i; 248 for (i = 0; i < fl->size; i++) 249 JLI_MemFree(fl->files[i]); 250 JLI_MemFree(fl->files); 251 } 252 JLI_MemFree(fl); 253 } 254 } 255 256 static void 257 FileList_ensureCapacity(FileList fl, int capacity) 258 { 259 if (fl->capacity < capacity) { 260 while (fl->capacity < capacity) 261 fl->capacity *= 2; 262 fl->files = JLI_MemRealloc(fl->files, 263 fl->capacity * sizeof(fl->files[0])); 264 } 265 } 266 267 static void 268 FileList_add(FileList fl, char *file) 269 { 270 FileList_ensureCapacity(fl, fl->size+1); 271 fl->files[fl->size++] = file; 272 } 273 274 static void 275 FileList_addSubstring(FileList fl, const char *beg, int len) 276 { 277 char *filename = (char *) JLI_MemAlloc(len+1); 278 memcpy(filename, beg, len); 279 filename[len] = '\0'; 280 FileList_ensureCapacity(fl, fl->size+1); 281 fl->files[fl->size++] = filename; 282 } 283 284 static char * 285 FileList_join(FileList fl, char sep) 286 { 287 int i; 288 int size; 289 char *path; 290 char *p; 291 for (i = 0, size = 1; i < fl->size; i++) 292 size += (int)JLI_StrLen(fl->files[i]) + 1; 293 294 path = JLI_MemAlloc(size); 295 296 for (i = 0, p = path; i < fl->size; i++) { 297 int len = (int)JLI_StrLen(fl->files[i]); 298 if (i > 0) *p++ = sep; 299 memcpy(p, fl->files[i], len); 300 p += len; 301 } 302 *p = '\0'; 303 304 return path; 305 } 306 307 static FileList 308 FileList_split(const char *path, char sep) 309 { 310 const char *p, *q; 311 int len = (int)JLI_StrLen(path); 312 int count; 313 FileList fl; 314 for (count = 1, p = path; p < path + len; p++) 315 count += (*p == sep); 316 fl = FileList_new(count); 317 for (p = path;;) { 318 for (q = p; q <= path + len; q++) { 319 if (*q == sep || *q == '\0') { 320 FileList_addSubstring(fl, p, q - p); 321 if (*q == '\0') 322 return fl; 323 p = q + 1; 324 } 325 } 326 } 327 } 328 329 static int 330 isJarFileName(const char *filename) 331 { 332 int len = (int)JLI_StrLen(filename); 333 return (len >= 4) && 334 (filename[len - 4] == '.') && 335 (equal(filename + len - 3, "jar") || 336 equal(filename + len - 3, "JAR")) && 337 /* Paranoia: Maybe filename is "DIR:foo.jar" */ 338 (JLI_StrChr(filename, PATH_SEPARATOR) == NULL); 339 } 340 341 static char * 342 wildcardConcat(const char *wildcard, const char *basename) 343 { 344 int wildlen = (int)JLI_StrLen(wildcard); 345 int baselen = (int)JLI_StrLen(basename); 346 char *filename = (char *) JLI_MemAlloc(wildlen + baselen); 347 /* Replace the trailing '*' with basename */ 348 memcpy(filename, wildcard, wildlen-1); 349 memcpy(filename+wildlen-1, basename, baselen+1); 350 return filename; 351 } 352 353 static FileList 354 wildcardFileList(const char *wildcard) 355 { 356 const char *basename; 357 FileList fl = FileList_new(16); 358 WildcardIterator it = WildcardIterator_for(wildcard); 359 360 if (it == NULL) 361 { 362 FileList_free(fl); 363 return NULL; 364 } 365 366 while ((basename = WildcardIterator_next(it)) != NULL) 367 if (isJarFileName(basename)) 368 FileList_add(fl, wildcardConcat(wildcard, basename)); 369 WildcardIterator_close(it); 370 return fl; 371 } 372 373 static int 374 isWildcard(const char *filename) 375 { 376 int len = (int)JLI_StrLen(filename); 377 return (len > 0) && 378 (filename[len - 1] == '*') && 379 (len == 1 || IS_FILE_SEPARATOR(filename[len - 2])) && 380 (! exists(filename)); 381 } 382 383 static void 384 FileList_expandWildcards(FileList fl) 385 { 386 int i, j; 387 for (i = 0; i < fl->size; i++) { 388 if (isWildcard(fl->files[i])) { 389 FileList expanded = wildcardFileList(fl->files[i]); 390 if (expanded != NULL && expanded->size > 0) { 391 JLI_MemFree(fl->files[i]); 392 FileList_ensureCapacity(fl, fl->size + expanded->size); 393 for (j = fl->size - 1; j >= i+1; j--) 394 fl->files[j+expanded->size-1] = fl->files[j]; 395 for (j = 0; j < expanded->size; j++) 396 fl->files[i+j] = expanded->files[j]; 397 i += expanded->size - 1; 398 fl->size += expanded->size - 1; 399 /* fl expropriates expanded's elements. */ 400 expanded->size = 0; 401 } 402 FileList_free(expanded); 403 } 404 } 405 } 406 407 const char * 408 JLI_WildcardExpandClasspath(const char *classpath) 409 { 410 char *expanded; 411 FileList fl; 412 413 if (JLI_StrChr(classpath, '*') == NULL) 414 return classpath; 415 fl = FileList_split(classpath, PATH_SEPARATOR); 416 FileList_expandWildcards(fl); 417 expanded = FileList_join(fl, PATH_SEPARATOR); 418 FileList_free(fl); 419 if (getenv(JLDEBUG_ENV_ENTRY) != 0) 420 printf("Expanded wildcards:\n" 421 " before: \"%s\"\n" 422 " after : \"%s\"\n", 423 classpath, expanded); 424 return expanded; 425 } 426 427 #ifdef DEBUG_WILDCARD 428 static void 429 FileList_print(FileList fl) 430 { 431 int i; 432 putchar('['); 433 for (i = 0; i < fl->size; i++) { 434 if (i > 0) printf(", "); 435 printf("\"%s\"",fl->files[i]); 436 } 437 putchar(']'); 438 } 439 440 static void 441 wildcardExpandArgv(const char ***argv) 442 { 443 int i; 444 for (i = 0; (*argv)[i]; i++) { 445 if (equal((*argv)[i], "-cp") || 446 equal((*argv)[i], "-classpath")) { 447 i++; 448 (*argv)[i] = wildcardExpandClasspath((*argv)[i]); 449 } 450 } 451 } 452 453 static void 454 debugPrintArgv(char *argv[]) 455 { 456 int i; 457 putchar('['); 458 for (i = 0; argv[i]; i++) { 459 if (i > 0) printf(", "); 460 printf("\"%s\"", argv[i]); 461 } 462 printf("]\n"); 463 } 464 465 int 466 main(int argc, char *argv[]) 467 { 468 argv[0] = "java"; 469 wildcardExpandArgv((const char***)&argv); 470 debugPrintArgv(argv); 471 /* execvp("java", argv); */ 472 return 0; 473 } 474 #endif /* DEBUG_WILDCARD */ 475 476 /* Cute little perl prototype implementation.... 477 478 my $sep = ($^O =~ /^(Windows|cygwin)/) ? ";" : ":"; 479 480 sub expand($) { 481 opendir DIR, $_[0] or return $_[0]; 482 join $sep, map {"$_[0]/$_"} grep {/\.(jar|JAR)$/} readdir DIR; 483 } 484 485 sub munge($) { 486 join $sep, 487 map {(! -r $_ and s/[\/\\]+\*$//) ? expand $_ : $_} split $sep, $_[0]; 488 } 489 490 for (my $i = 0; $i < @ARGV - 1; $i++) { 491 $ARGV[$i+1] = munge $ARGV[$i+1] if $ARGV[$i] =~ /^-c(p|lasspath)$/; 492 } 493 494 $ENV{CLASSPATH} = munge $ENV{CLASSPATH} if exists $ENV{CLASSPATH}; 495 @ARGV = ("java", @ARGV); 496 print "@ARGV\n"; 497 exec @ARGV; 498 499 */