author | jjg |
Fri, 21 May 2010 17:32:19 -0700 | |
changeset 5652 | 89482b760ef7 |
parent 715 | f16baef3a20e |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2004-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
285
1db7e0f6f7b0
6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents:
273
diff
changeset
|
26 |
#ifdef _WIN32 |
1db7e0f6f7b0
6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents:
273
diff
changeset
|
27 |
/* |
1db7e0f6f7b0
6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents:
273
diff
changeset
|
28 |
* Win* needs this include. However, Linux and Solaris do not. |
1db7e0f6f7b0
6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents:
273
diff
changeset
|
29 |
* Having this include on Solaris SPARC breaks having non US-ASCII |
1db7e0f6f7b0
6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents:
273
diff
changeset
|
30 |
* characters in the value of the Premain-Class attribute. |
1db7e0f6f7b0
6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents:
273
diff
changeset
|
31 |
*/ |
1db7e0f6f7b0
6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents:
273
diff
changeset
|
32 |
#include <ctype.h> |
1db7e0f6f7b0
6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents:
273
diff
changeset
|
33 |
#endif /* _WIN32 */ |
2 | 34 |
#include <string.h> |
35 |
#include <stdlib.h> |
|
36 |
||
37 |
#include "jni.h" |
|
38 |
#include "manifest_info.h" |
|
39 |
#include "JarFacade.h" |
|
40 |
||
41 |
typedef struct { |
|
42 |
jarAttribute* head; |
|
43 |
jarAttribute* tail; |
|
44 |
} iterationContext; |
|
45 |
||
46 |
static void |
|
47 |
doAttribute(const char* name, const char* value, void* user_data) { |
|
48 |
iterationContext* context = (iterationContext*) user_data; |
|
49 |
||
50 |
jarAttribute* attribute = (jarAttribute*)malloc(sizeof(jarAttribute)); |
|
51 |
if (attribute != NULL) { |
|
52 |
attribute->name = strdup(name); |
|
53 |
if (attribute->name == NULL) { |
|
54 |
free(attribute); |
|
55 |
} else { |
|
285
1db7e0f6f7b0
6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents:
273
diff
changeset
|
56 |
char *begin = (char *)value; |
273
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
57 |
char *end; |
285
1db7e0f6f7b0
6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents:
273
diff
changeset
|
58 |
size_t value_len; |
273
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
59 |
|
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
60 |
/* skip any leading white space */ |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
61 |
while (isspace(*begin)) { |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
62 |
begin++; |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
63 |
} |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
64 |
|
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
65 |
/* skip any trailing white space */ |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
66 |
end = &begin[strlen(begin)]; |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
67 |
while (end > begin && isspace(end[-1])) { |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
68 |
end--; |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
69 |
} |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
70 |
|
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
71 |
if (begin == end) { |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
72 |
/* no value so skip this attribute */ |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
73 |
free(attribute->name); |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
74 |
free(attribute); |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
75 |
return; |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
76 |
} |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
77 |
|
285
1db7e0f6f7b0
6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents:
273
diff
changeset
|
78 |
value_len = (size_t)(end - begin); |
273
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
79 |
attribute->value = malloc(value_len + 1); |
2 | 80 |
if (attribute->value == NULL) { |
81 |
free(attribute->name); |
|
82 |
free(attribute); |
|
83 |
} else { |
|
273
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
84 |
/* save the value without leading or trailing whitespace */ |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
85 |
strncpy(attribute->value, begin, value_len); |
285
1db7e0f6f7b0
6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents:
273
diff
changeset
|
86 |
attribute->value[value_len] = '\0'; |
2 | 87 |
attribute->next = NULL; |
88 |
if (context->head == NULL) { |
|
89 |
context->head = attribute; |
|
90 |
} else { |
|
91 |
context->tail->next = attribute; |
|
92 |
} |
|
93 |
context->tail = attribute; |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
} |
|
98 |
} |
|
99 |
||
100 |
/* |
|
101 |
* Return a list of attributes from the main section of the given JAR |
|
102 |
* file. Returns NULL if there is an error or there aren't any attributes. |
|
103 |
*/ |
|
104 |
jarAttribute* |
|
105 |
readAttributes(const char* jarfile) |
|
106 |
{ |
|
107 |
int rc; |
|
108 |
iterationContext context = { NULL, NULL }; |
|
109 |
||
110 |
rc = JLI_ManifestIterate(jarfile, doAttribute, (void*)&context); |
|
111 |
||
112 |
if (rc == 0) { |
|
113 |
return context.head; |
|
114 |
} else { |
|
115 |
freeAttributes(context.head); |
|
116 |
return NULL; |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
||
121 |
/* |
|
122 |
* Free a list of attributes |
|
123 |
*/ |
|
124 |
void |
|
125 |
freeAttributes(jarAttribute* head) { |
|
126 |
while (head != NULL) { |
|
127 |
jarAttribute* next = (jarAttribute*)head->next; |
|
128 |
free(head->name); |
|
129 |
free(head->value); |
|
130 |
free(head); |
|
131 |
head = next; |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
/* |
|
136 |
* Get the value of an attribute in an attribute list. Returns NULL |
|
137 |
* if attribute not found. |
|
138 |
*/ |
|
139 |
char* |
|
140 |
getAttribute(const jarAttribute* attributes, const char* name) { |
|
141 |
while (attributes != NULL) { |
|
142 |
if (strcasecmp(attributes->name, name) == 0) { |
|
143 |
return attributes->value; |
|
144 |
} |
|
145 |
attributes = (jarAttribute*)attributes->next; |
|
146 |
} |
|
147 |
return NULL; |
|
148 |
} |