assembler: improve error report for string syntax error
[rofl0r-agsutils.git] / agstract.c
blobd6ffd793fc09bc05ddc9fe2da601808264c64786
1 #define _GNU_SOURCE
2 #include "Clib32.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <errno.h>
8 #include "version.h"
9 #define ADS ":::AGStract " VERSION " by rofl0r:::"
11 __attribute__((noreturn))
12 void usage(char *argv0) {
13 dprintf(2, ADS "\nusage:\n%s agsgame.exe targetdir\n\n", argv0);
14 exit(1);
17 static void dump_exe(struct AgsFile *ags, const char *dir) {
18 if(ags->pack_off) {
19 char fnbuf[512];
20 snprintf(fnbuf, sizeof(fnbuf), "%s/agspack.exestub", dir);
21 AgsFile_extract(ags, 0, 0, ags->pack_off, fnbuf);
25 static int open_packfile(const char* fn) {
26 return open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0660);
29 int main(int argc, char** argv) {
30 if(argc < 3) usage(argv[0]);
31 dprintf(1, ADS "\n");
32 struct AgsFile ags_b, *ags = &ags_b;
33 char *fn = argv[1];
34 char *dir = argv[2];
35 char fnbuf[512];
36 snprintf(fnbuf, sizeof(fnbuf), "%s/agspack.info", dir);
37 int outfd = open_packfile(fnbuf);
38 if(outfd == -1 && errno == ENOENT) {
39 mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
40 outfd = open_packfile(fnbuf);
42 if(outfd == -1) {
43 perror("open");
44 dprintf(2, "did you forget to create %s?\n", dir);
45 perror(fnbuf);
46 return 1;
48 dprintf(outfd, "info=infofile created by %s\n"
49 "info=this file is needed to reconstruct the packfile with AGSpack\n", ADS);
50 AgsFile_init(ags, fn);
51 if(!AgsFile_open(ags)) {
52 dprintf(2, "error opening %s\n", fn);
53 return 1;
55 dump_exe(ags, dir);
56 size_t i, l = AgsFile_getFileCount(ags), ld =AgsFile_getDataFileCount(ags);
57 dprintf(1, "%s: version %d, containing %zu files.\n", fn, AgsFile_getVersion(ags), l);
58 dprintf(outfd, "agspackfile=%s\nagsversion=%d\nfilecount=%zu\n", fn, AgsFile_getVersion(ags), l);
59 dprintf(outfd, "datafilecount=%zu\n", ld);
60 for(i = 0; i < ld; i++) {
61 dprintf(outfd, "df%zu=%s\n", i, AgsFile_getDataFileName(ags, i));
63 for(i = 0; i < l; i++) {
64 char buf[16];
65 snprintf(buf, sizeof(buf), "%d", AgsFile_getFileNumber(ags, i));
66 dprintf(outfd, "fileno%zu=%s\n", i, buf);
68 for(i = 0; i < l; i++) {
69 char *currfn = AgsFile_getFileName(ags, i);
70 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dir, currfn);
71 dprintf(1, "%s -> %s\n", currfn, fnbuf);
72 dprintf(outfd, "%zu=%s\n", i, currfn);
73 AgsFile_dump(ags, i, fnbuf);
76 AgsFile_close(ags);
77 close(outfd);
79 return 0;