2
|
1 |
/*
|
|
2 |
* Copyright 2004-2005 Sun Microsystems, Inc. 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. 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 |
|
|
26 |
#include <string.h>
|
|
27 |
#include <stdlib.h>
|
|
28 |
|
|
29 |
#include "jni.h"
|
|
30 |
#include "manifest_info.h"
|
|
31 |
#include "JarFacade.h"
|
|
32 |
|
|
33 |
typedef struct {
|
|
34 |
jarAttribute* head;
|
|
35 |
jarAttribute* tail;
|
|
36 |
} iterationContext;
|
|
37 |
|
|
38 |
static void
|
|
39 |
doAttribute(const char* name, const char* value, void* user_data) {
|
|
40 |
iterationContext* context = (iterationContext*) user_data;
|
|
41 |
|
|
42 |
jarAttribute* attribute = (jarAttribute*)malloc(sizeof(jarAttribute));
|
|
43 |
if (attribute != NULL) {
|
|
44 |
attribute->name = strdup(name);
|
|
45 |
if (attribute->name == NULL) {
|
|
46 |
free(attribute);
|
|
47 |
} else {
|
|
48 |
attribute->value = strdup(value);
|
|
49 |
if (attribute->value == NULL) {
|
|
50 |
free(attribute->name);
|
|
51 |
free(attribute);
|
|
52 |
} else {
|
|
53 |
attribute->next = NULL;
|
|
54 |
if (context->head == NULL) {
|
|
55 |
context->head = attribute;
|
|
56 |
} else {
|
|
57 |
context->tail->next = attribute;
|
|
58 |
}
|
|
59 |
context->tail = attribute;
|
|
60 |
}
|
|
61 |
}
|
|
62 |
|
|
63 |
}
|
|
64 |
}
|
|
65 |
|
|
66 |
/*
|
|
67 |
* Return a list of attributes from the main section of the given JAR
|
|
68 |
* file. Returns NULL if there is an error or there aren't any attributes.
|
|
69 |
*/
|
|
70 |
jarAttribute*
|
|
71 |
readAttributes(const char* jarfile)
|
|
72 |
{
|
|
73 |
int rc;
|
|
74 |
iterationContext context = { NULL, NULL };
|
|
75 |
|
|
76 |
rc = JLI_ManifestIterate(jarfile, doAttribute, (void*)&context);
|
|
77 |
|
|
78 |
if (rc == 0) {
|
|
79 |
return context.head;
|
|
80 |
} else {
|
|
81 |
freeAttributes(context.head);
|
|
82 |
return NULL;
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
|
|
87 |
/*
|
|
88 |
* Free a list of attributes
|
|
89 |
*/
|
|
90 |
void
|
|
91 |
freeAttributes(jarAttribute* head) {
|
|
92 |
while (head != NULL) {
|
|
93 |
jarAttribute* next = (jarAttribute*)head->next;
|
|
94 |
free(head->name);
|
|
95 |
free(head->value);
|
|
96 |
free(head);
|
|
97 |
head = next;
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
|
101 |
/*
|
|
102 |
* Get the value of an attribute in an attribute list. Returns NULL
|
|
103 |
* if attribute not found.
|
|
104 |
*/
|
|
105 |
char*
|
|
106 |
getAttribute(const jarAttribute* attributes, const char* name) {
|
|
107 |
while (attributes != NULL) {
|
|
108 |
if (strcasecmp(attributes->name, name) == 0) {
|
|
109 |
return attributes->value;
|
|
110 |
}
|
|
111 |
attributes = (jarAttribute*)attributes->next;
|
|
112 |
}
|
|
113 |
return NULL;
|
|
114 |
}
|