FreeCalypso > hg > tcs211-patches
annotate tool/archive.c @ 3:003df9892e57
import original TCS211 libs from Sotovik
author | Space Falcon <falcon@ivan.Harhan.ORG> |
---|---|
date | Fri, 05 Jun 2015 21:19:08 +0000 |
parents | 058d377fc299 |
children | e33380b5bd46 |
rev | line source |
---|---|
2
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * This module implements archive processing for ti-libpatch. |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 */ |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 #include <sys/types.h> |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 #include <stdio.h> |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 #include <stdlib.h> |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 #include <string.h> |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 #include <strings.h> |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 #include "ar.h" |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 #include "globals.h" |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 process_archive() |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 { |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 FILE *inf, *outf; |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 char ar_signature_buf[SARMAG]; |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 struct ar_hdr ar_hdr; |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 char member_name[17], *cp; |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 unsigned size; |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 char *buf; |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
22 inf = fopen(lib_in_filename, "r"); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 if (!inf) { |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 perror(lib_in_filename); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 exit(1); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 } |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 if (fread(ar_signature_buf, 1, SARMAG, inf) != SARMAG || |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 strncmp(ar_signature_buf, ARMAG, SARMAG)) { |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 fprintf(stderr, "error: %s is not an archive\n", |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 lib_in_filename); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
31 exit(1); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 } |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 outf = fopen(lib_out_filename, "w"); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 if (!outf) { |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
35 perror(lib_out_filename); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
36 exit(1); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 } |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 fwrite(ar_signature_buf, 1, SARMAG, outf); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 while (fread(&ar_hdr, sizeof(struct ar_hdr), 1, inf)) { |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 if (strncmp(ar_hdr.ar_fmag, ARFMAG, sizeof(ar_hdr.ar_fmag))) { |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 fprintf(stderr, "error parsing %s: bad ar_fmag\n", |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 lib_in_filename); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
43 exit(1); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
44 } |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
45 bcopy(ar_hdr.ar_name, member_name, 16); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
46 member_name[16] = '\0'; |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 cp = index(member_name, '/'); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 if (!cp) { |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 fprintf(stderr, |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 "error parsing %s: no \'/\' in ar_name[]\n", |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 lib_in_filename); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 exit(1); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 } |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
54 *cp = '\0'; |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 size = strtoul(ar_hdr.ar_size, 0, 10); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 buf = malloc(size); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 if (!buf) { |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 fprintf(stderr, |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 "error: unable to malloc buffer for archive member \"%s\"\n", |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 member_name); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 exit(1); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 } |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 if (fread(buf, 1, size, inf) != size) { |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 fprintf(stderr, |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 "error reading the body of member \"%s\" from %s\n", |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 member_name, lib_in_filename); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 exit(1); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 } |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 if (size & 1 && getc(inf) != '\n') { |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 fprintf(stderr, |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 "error parsing %s: no \\n after odd-sized member \"%s\"\n", |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 lib_in_filename, member_name); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 exit(1); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 } |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 /* the patch hook will go here */ |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 /* write it out */ |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 fwrite(&ar_hdr, sizeof(struct ar_hdr), 1, outf); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 fwrite(buf, 1, size, outf); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 free(buf); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 if (size & 1) |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
81 putc('\n', outf); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
82 } |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 fclose(inf); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 fclose(outf); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 return(0); |
058d377fc299
ti-libpatch: archive processing implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
86 } |