comparison tool/archive.c @ 2:058d377fc299

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