-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
java: add the skeleton of a java vm program
This program loads the given class and determines whether a main method is present. Signed-off-by: Vegard Nossum <[email protected]>
- Loading branch information
Vegard Nossum
committed
Jun 5, 2008
1 parent
7c2b828
commit f9ca163
Showing
2 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* cafebabe - the class loader library in C | ||
* Copyright (C) 2008 Vegard Nossum <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
#define _GNU_SOURCE | ||
|
||
#include <errno.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "cafebabe/access.h" | ||
#include "cafebabe/attribute_info.h" | ||
#include "cafebabe/class.h" | ||
#include "cafebabe/constant_pool.h" | ||
#include "cafebabe/error.h" | ||
#include "cafebabe/field_info.h" | ||
#include "cafebabe/method_info.h" | ||
#include "cafebabe/stream.h" | ||
|
||
int | ||
main(int argc, char *argv[]) | ||
{ | ||
if (argc != 2) { | ||
fprintf(stderr, "usage: %s CLASS\n", argv[0]); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
const char *classname = argv[1]; | ||
char *filename; | ||
if (asprintf(&filename, "%s.class", classname) == -1) { | ||
fprintf(stderr, "error: %s\n", strerror(errno)); | ||
goto out; | ||
} | ||
|
||
struct cafebabe_stream stream; | ||
if (cafebabe_stream_open(&stream, filename)) { | ||
fprintf(stderr, "error: %s: %s\n", filename, | ||
cafebabe_stream_error(&stream)); | ||
goto out_filename; | ||
} | ||
|
||
struct cafebabe_class class; | ||
if (cafebabe_class_init(&class, &stream)) { | ||
fprintf(stderr, "error: %s:%d/%d: %s\n", filename, | ||
stream.virtual_i, stream.virtual_n, | ||
cafebabe_stream_error(&stream)); | ||
goto out_stream; | ||
} | ||
|
||
struct cafebabe_method_info *main_method; | ||
if (cafebabe_class_get_method(&class, | ||
"main", "([Ljava/lang/String;)V", &main_method)) | ||
{ | ||
fprintf(stderr, "error: %s: no main method\n", classname); | ||
goto out_class; | ||
} | ||
|
||
cafebabe_class_deinit(&class); | ||
cafebabe_stream_close(&stream); | ||
|
||
return EXIT_SUCCESS; | ||
|
||
out_class: | ||
cafebabe_class_deinit(&class); | ||
out_stream: | ||
cafebabe_stream_close(&stream); | ||
out_filename: | ||
free(filename); | ||
out: | ||
return EXIT_FAILURE; | ||
} |