# HG changeset patch # User Space Falcon # Date 1433536293 0 # Node ID 058d377fc299af0828d88b984f5bdd5ad958ab9d # Parent b9fe017905f83c1b53687210d30bd171e75a14a3 ti-libpatch: archive processing implemented diff -r b9fe017905f8 -r 058d377fc299 tool/Makefile --- a/tool/Makefile Fri Jun 05 01:17:39 2015 +0000 +++ b/tool/Makefile Fri Jun 05 20:31:33 2015 +0000 @@ -1,7 +1,7 @@ CC= gcc CFLAGS= -O2 PROG= ti-libpatch -OBJS= globals.o main.o readspec.o +OBJS= archive.o globals.o main.o readspec.o HDRS= ar.h globals.h patchinfo.h all: ${PROG} diff -r b9fe017905f8 -r 058d377fc299 tool/archive.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool/archive.c Fri Jun 05 20:31:33 2015 +0000 @@ -0,0 +1,86 @@ +/* + * This module implements archive processing for ti-libpatch. + */ + +#include +#include +#include +#include +#include +#include "ar.h" +#include "globals.h" + +process_archive() +{ + FILE *inf, *outf; + char ar_signature_buf[SARMAG]; + struct ar_hdr ar_hdr; + char member_name[17], *cp; + unsigned size; + char *buf; + + inf = fopen(lib_in_filename, "r"); + if (!inf) { + perror(lib_in_filename); + exit(1); + } + if (fread(ar_signature_buf, 1, SARMAG, inf) != SARMAG || + strncmp(ar_signature_buf, ARMAG, SARMAG)) { + fprintf(stderr, "error: %s is not an archive\n", + lib_in_filename); + exit(1); + } + outf = fopen(lib_out_filename, "w"); + if (!outf) { + perror(lib_out_filename); + exit(1); + } + fwrite(ar_signature_buf, 1, SARMAG, outf); + while (fread(&ar_hdr, sizeof(struct ar_hdr), 1, inf)) { + if (strncmp(ar_hdr.ar_fmag, ARFMAG, sizeof(ar_hdr.ar_fmag))) { + fprintf(stderr, "error parsing %s: bad ar_fmag\n", + lib_in_filename); + exit(1); + } + bcopy(ar_hdr.ar_name, member_name, 16); + member_name[16] = '\0'; + cp = index(member_name, '/'); + if (!cp) { + fprintf(stderr, + "error parsing %s: no \'/\' in ar_name[]\n", + lib_in_filename); + exit(1); + } + *cp = '\0'; + size = strtoul(ar_hdr.ar_size, 0, 10); + buf = malloc(size); + if (!buf) { + fprintf(stderr, + "error: unable to malloc buffer for archive member \"%s\"\n", + member_name); + exit(1); + } + if (fread(buf, 1, size, inf) != size) { + fprintf(stderr, + "error reading the body of member \"%s\" from %s\n", + member_name, lib_in_filename); + exit(1); + } + if (size & 1 && getc(inf) != '\n') { + fprintf(stderr, + "error parsing %s: no \\n after odd-sized member \"%s\"\n", + lib_in_filename, member_name); + exit(1); + } + /* the patch hook will go here */ + /* write it out */ + fwrite(&ar_hdr, sizeof(struct ar_hdr), 1, outf); + fwrite(buf, 1, size, outf); + free(buf); + if (size & 1) + putc('\n', outf); + } + fclose(inf); + fclose(outf); + return(0); +} diff -r b9fe017905f8 -r 058d377fc299 tool/main.c --- a/tool/main.c Fri Jun 05 01:17:39 2015 +0000 +++ b/tool/main.c Fri Jun 05 20:31:33 2015 +0000 @@ -1,5 +1,5 @@ /* - * libpatch main module + * ti-libpatch main module */ #include @@ -18,6 +18,6 @@ patch_desc_filename = argv[2]; lib_out_filename = argv[3]; read_spec_file(); - /* code to be filled here */ + process_archive(); exit(0); }