src/java.base/share/native/libjli/wildcard.c
author mbaesken
Thu, 28 Nov 2019 13:02:39 +0100
changeset 59323 ae2eb76c486d
parent 51597 4c78f4fd8370
permissions -rw-r--r--
8234821: remove unused functions from libjli Reviewed-by: clanger, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51597
4c78f4fd8370 8207744: Clean up inconsistent use of opendir/closedir versus opendir64/closedir64
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Class-Path Wildcards
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * The syntax for wildcards is a single asterisk. The class path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * foo/"*", e.g., loads all jar files in the directory named foo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * (This requires careful quotation when used in shell scripts.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Only files whose names end in .jar or .JAR are matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Files whose names end in .zip, or which have a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * magic number, regardless of filename extension, are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Files are considered regardless of whether or not they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * "hidden" in the UNIX sense, i.e., have names beginning with '.'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A wildcard only matches jar files, not class files in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * directory.  If you want to load both class files and jar files from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * a single directory foo then you can say foo:foo/"*", or foo/"*":foo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * if you want the jar files to take precedence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Subdirectories are not searched recursively, i.e., foo/"*" only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * looks for jar files in foo, not in foo/bar, foo/baz, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Expansion of wildcards is done early, prior to the invocation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * program's main method, rather than late, during the class-loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * process itself.  Each element of the input class path containing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * wildcard is replaced by the (possibly empty) sequence of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * generated by enumerating the jar files in the named directory.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * the directory foo contains a.jar, b.jar, and c.jar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * e.g., then the class path foo/"*" is expanded into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * foo/a.jar:foo/b.jar:foo/c.jar, and that string would be the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * of the system property java.class.path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * The order in which the jar files in a directory are enumerated in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * the expanded class path is not specified and may vary from platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * to platform and even from moment to moment on the same machine.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * well-constructed application should not depend upon any particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * order.  If a specific order is required then the jar files can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * enumerated explicitly in the class path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * The CLASSPATH environment variable is not treated any differently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * from the -classpath (equiv. -cp) command-line option,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * i.e. wildcards are honored in all these cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Class-path wildcards are not honored in the Class-Path jar-manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Class-path wildcards are honored not only by the Java launcher but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * also by most other command-line tools that accept class paths, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * in particular by javac and javadoc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Class-path wildcards are not honored in any other kind of path, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * especially not in the bootstrap class path, which is a mere
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * artifact of our implementation and not something that developers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * should use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * Classpath wildcards are only expanded in the Java launcher code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * supporting the use of wildcards on the command line and in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * CLASSPATH environment variable.  We do not support the use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * wildcards by applications that embed the JVM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
#include <stddef.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
#include "java.h"       /* Strictly for PATH_SEPARATOR/FILE_SEPARATOR */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
#include "jli_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
#ifdef _WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
#else /* Unix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
#include <dirent.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
#endif /* Unix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
51597
4c78f4fd8370 8207744: Clean up inconsistent use of opendir/closedir versus opendir64/closedir64
bpb
parents: 47216
diff changeset
   103
#if defined(_AIX)
4c78f4fd8370 8207744: Clean up inconsistent use of opendir/closedir versus opendir64/closedir64
bpb
parents: 47216
diff changeset
   104
  #define DIR DIR64
4c78f4fd8370 8207744: Clean up inconsistent use of opendir/closedir versus opendir64/closedir64
bpb
parents: 47216
diff changeset
   105
  #define dirent dirent64
4c78f4fd8370 8207744: Clean up inconsistent use of opendir/closedir versus opendir64/closedir64
bpb
parents: 47216
diff changeset
   106
  #define opendir opendir64
4c78f4fd8370 8207744: Clean up inconsistent use of opendir/closedir versus opendir64/closedir64
bpb
parents: 47216
diff changeset
   107
  #define readdir readdir64
4c78f4fd8370 8207744: Clean up inconsistent use of opendir/closedir versus opendir64/closedir64
bpb
parents: 47216
diff changeset
   108
  #define closedir closedir64
4c78f4fd8370 8207744: Clean up inconsistent use of opendir/closedir versus opendir64/closedir64
bpb
parents: 47216
diff changeset
   109
#endif
4c78f4fd8370 8207744: Clean up inconsistent use of opendir/closedir versus opendir64/closedir64
bpb
parents: 47216
diff changeset
   110
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
exists(const char* filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
#ifdef _WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    return _access(filename, 0) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    return access(filename, F_OK) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
#define NEW_(TYPE) ((TYPE) JLI_MemAlloc(sizeof(struct TYPE##_)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * Wildcard directory iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * WildcardIterator_for(wildcard) returns an iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * Each call to that iterator's next() method returns the basename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * of an entry in the wildcard's directory.  The basename's memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * belongs to the iterator.  The caller is responsible for prepending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * the directory name and file separator, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * When done with the iterator, call the close method to clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
typedef struct WildcardIterator_* WildcardIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
#ifdef _WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
struct WildcardIterator_
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    HANDLE handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    char *firstFile; /* Stupid FindFirstFile...FindNextFile */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
};
13411
224a28370893 7146424: Wildcard expansion for single entry classpath
ksrini
parents: 7668
diff changeset
   140
// since this is used repeatedly we keep it here.
224a28370893 7146424: Wildcard expansion for single entry classpath
ksrini
parents: 7668
diff changeset
   141
static WIN32_FIND_DATA find_data;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
static WildcardIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
WildcardIterator_for(const char *wildcard)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    WildcardIterator it = NEW_(WildcardIterator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    HANDLE handle = FindFirstFile(wildcard, &find_data);
17445
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 14517
diff changeset
   147
    if (handle == INVALID_HANDLE_VALUE) {
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 14517
diff changeset
   148
        JLI_MemFree(it);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return NULL;
17445
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 14517
diff changeset
   150
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    it->handle = handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    it->firstFile = find_data.cFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    return it;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
static char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
WildcardIterator_next(WildcardIterator it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    if (it->firstFile != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        char *firstFile = it->firstFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        it->firstFile = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return firstFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    return FindNextFile(it->handle, &find_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        ? find_data.cFileName : NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
WildcardIterator_close(WildcardIterator it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    if (it) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        FindClose(it->handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        JLI_MemFree(it->firstFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        JLI_MemFree(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
#else /* Unix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
struct WildcardIterator_
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    DIR *dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
static WildcardIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
WildcardIterator_for(const char *wildcard)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    DIR *dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    int wildlen = JLI_StrLen(wildcard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    if (wildlen < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        dir = opendir(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        char *dirname = JLI_StringDup(wildcard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        dirname[wildlen - 1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        dir = opendir(dirname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        JLI_MemFree(dirname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    if (dir == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        WildcardIterator it = NEW_(WildcardIterator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        it->dir = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return it;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
static char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
WildcardIterator_next(WildcardIterator it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    struct dirent* dirp = readdir(it->dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    return dirp ? dirp->d_name : NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
WildcardIterator_close(WildcardIterator it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    if (it) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        closedir(it->dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        JLI_MemFree(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
#endif /* Unix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
equal(const char *s1, const char *s2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    return JLI_StrCmp(s1, s2) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
isJarFileName(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
{
7028
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
   232
    int len = (int)JLI_StrLen(filename);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    return (len >= 4) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        (filename[len - 4] == '.') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        (equal(filename + len - 3, "jar") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         equal(filename + len - 3, "JAR")) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        /* Paranoia: Maybe filename is "DIR:foo.jar" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        (JLI_StrChr(filename, PATH_SEPARATOR) == NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
static char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
wildcardConcat(const char *wildcard, const char *basename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
{
7028
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
   244
    int wildlen = (int)JLI_StrLen(wildcard);
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
   245
    int baselen = (int)JLI_StrLen(basename);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    char *filename = (char *) JLI_MemAlloc(wildlen + baselen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /* Replace the trailing '*' with basename */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    memcpy(filename, wildcard, wildlen-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    memcpy(filename+wildlen-1, basename, baselen+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    return filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   253
static JLI_List
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
wildcardFileList(const char *wildcard)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    const char *basename;
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   257
    JLI_List fl = JLI_List_new(16);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    WildcardIterator it = WildcardIterator_for(wildcard);
14517
e50acb436a8d 8000476: Memory Leaks and uninitialized memory access in PKCS11 and other native code
jzavgren
parents: 13411
diff changeset
   259
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    if (it == NULL)
14517
e50acb436a8d 8000476: Memory Leaks and uninitialized memory access in PKCS11 and other native code
jzavgren
parents: 13411
diff changeset
   261
    {
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   262
        JLI_List_free(fl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return NULL;
14517
e50acb436a8d 8000476: Memory Leaks and uninitialized memory access in PKCS11 and other native code
jzavgren
parents: 13411
diff changeset
   264
    }
e50acb436a8d 8000476: Memory Leaks and uninitialized memory access in PKCS11 and other native code
jzavgren
parents: 13411
diff changeset
   265
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    while ((basename = WildcardIterator_next(it)) != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (isJarFileName(basename))
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   268
            JLI_List_add(fl, wildcardConcat(wildcard, basename));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    WildcardIterator_close(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    return fl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
isWildcard(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
{
7028
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
   276
    int len = (int)JLI_StrLen(filename);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    return (len > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        (filename[len - 1] == '*') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        (len == 1 || IS_FILE_SEPARATOR(filename[len - 2])) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        (! exists(filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
43330
6aa5f2938b75 8172309: classpath wildcards code does not support --class-path
henryjen
parents: 32267
diff changeset
   283
static int
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   284
FileList_expandWildcards(JLI_List fl)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
{
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   286
    size_t i, j;
43330
6aa5f2938b75 8172309: classpath wildcards code does not support --class-path
henryjen
parents: 32267
diff changeset
   287
    int expandedCnt = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    for (i = 0; i < fl->size; i++) {
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   289
        if (isWildcard(fl->elements[i])) {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   290
            JLI_List expanded = wildcardFileList(fl->elements[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (expanded != NULL && expanded->size > 0) {
43330
6aa5f2938b75 8172309: classpath wildcards code does not support --class-path
henryjen
parents: 32267
diff changeset
   292
                expandedCnt++;
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   293
                JLI_MemFree(fl->elements[i]);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   294
                JLI_List_ensureCapacity(fl, fl->size + expanded->size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                for (j = fl->size - 1; j >= i+1; j--)
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   296
                    fl->elements[j+expanded->size-1] = fl->elements[j];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                for (j = 0; j < expanded->size; j++)
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   298
                    fl->elements[i+j] = expanded->elements[j];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                i += expanded->size - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                fl->size += expanded->size - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                /* fl expropriates expanded's elements. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                expanded->size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   304
            JLI_List_free(expanded);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
43330
6aa5f2938b75 8172309: classpath wildcards code does not support --class-path
henryjen
parents: 32267
diff changeset
   307
    return expandedCnt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
const char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
JLI_WildcardExpandClasspath(const char *classpath)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
{
43330
6aa5f2938b75 8172309: classpath wildcards code does not support --class-path
henryjen
parents: 32267
diff changeset
   313
    const char *expanded;
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   314
    JLI_List fl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    if (JLI_StrChr(classpath, '*') == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return classpath;
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   318
    fl = JLI_List_split(classpath, PATH_SEPARATOR);
43330
6aa5f2938b75 8172309: classpath wildcards code does not support --class-path
henryjen
parents: 32267
diff changeset
   319
    expanded = FileList_expandWildcards(fl) ?
6aa5f2938b75 8172309: classpath wildcards code does not support --class-path
henryjen
parents: 32267
diff changeset
   320
        JLI_List_join(fl, PATH_SEPARATOR) : classpath;
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   321
    JLI_List_free(fl);
13411
224a28370893 7146424: Wildcard expansion for single entry classpath
ksrini
parents: 7668
diff changeset
   322
    if (getenv(JLDEBUG_ENV_ENTRY) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        printf("Expanded wildcards:\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
               "    before: \"%s\"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
               "    after : \"%s\"\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
               classpath, expanded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    return expanded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
#ifdef DEBUG_WILDCARD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
static void
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   332
FileList_print(JLI_List fl)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
{
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   334
    size_t i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    putchar('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    for (i = 0; i < fl->size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (i > 0) printf(", ");
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents: 29742
diff changeset
   338
        printf("\"%s\"",fl->elements[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    putchar(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
wildcardExpandArgv(const char ***argv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    for (i = 0; (*argv)[i]; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (equal((*argv)[i], "-cp") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            equal((*argv)[i], "-classpath")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            (*argv)[i] = wildcardExpandClasspath((*argv)[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
debugPrintArgv(char *argv[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    putchar('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    for (i = 0; argv[i]; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (i > 0) printf(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        printf("\"%s\"", argv[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    printf("]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
main(int argc, char *argv[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    argv[0] = "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    wildcardExpandArgv((const char***)&argv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    debugPrintArgv(argv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /* execvp("java", argv); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
#endif /* DEBUG_WILDCARD */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
/* Cute little perl prototype implementation....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
my $sep = ($^O =~ /^(Windows|cygwin)/) ? ";" : ":";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
sub expand($) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
  opendir DIR, $_[0] or return $_[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
  join $sep, map {"$_[0]/$_"} grep {/\.(jar|JAR)$/} readdir DIR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
sub munge($) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
  join $sep,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    map {(! -r $_ and s/[\/\\]+\*$//) ? expand $_ : $_} split $sep, $_[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
for (my $i = 0; $i < @ARGV - 1; $i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
  $ARGV[$i+1] = munge $ARGV[$i+1] if $ARGV[$i] =~ /^-c(p|lasspath)$/;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
$ENV{CLASSPATH} = munge $ENV{CLASSPATH} if exists $ENV{CLASSPATH};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
@ARGV = ("java", @ARGV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
print "@ARGV\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
exec @ARGV;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
*/