# HG changeset patch # User Space Falcon # Date 1433317200 0 # Node ID 12e230d431f0a54667c9bbc21ab2eec32fad1309 started writing libpatch tool diff -r 000000000000 -r 12e230d431f0 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Wed Jun 03 07:40:00 2015 +0000 @@ -0,0 +1,4 @@ +syntax: regexp + +^tool/.*\.o$ +^tool/libpatch$ diff -r 000000000000 -r 12e230d431f0 tool/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool/Makefile Wed Jun 03 07:40:00 2015 +0000 @@ -0,0 +1,18 @@ +CC= gcc +CFLAGS= -O2 +PROG= libpatch +OBJS= globals.o main.o readspec.o +HDRS= ar.h globals.h patchinfo.h + +all: ${PROG} + +${PROG}: ${OBJS} + ${CC} -o $@ ${OBJS} + +${OBJS}: ${HDRS} + +install: + install -c -o bin -g bin -m 755 ${PROG} /usr/local/bin + +clean: + rm -f *.o ${PROG} *errs diff -r 000000000000 -r 12e230d431f0 tool/ar.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool/ar.h Wed Jun 03 07:40:00 2015 +0000 @@ -0,0 +1,22 @@ +/* + * Copyright (c) 1980 Regents of the University of California. + * All rights reserved. The Berkeley software License Agreement + * specifies the terms and conditions for redistribution. + * + * @(#)ar.h 5.1 (Berkeley) 5/30/85 + */ + +#define ARMAG "!\n" +#define SARMAG 8 + +#define ARFMAG "`\n" + +struct ar_hdr { + char ar_name[16]; + char ar_date[12]; + char ar_uid[6]; + char ar_gid[6]; + char ar_mode[8]; + char ar_size[10]; + char ar_fmag[2]; +}; diff -r 000000000000 -r 12e230d431f0 tool/globals.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool/globals.c Wed Jun 03 07:40:00 2015 +0000 @@ -0,0 +1,7 @@ +/* + * This module contains global variable definitions for our libpatch tool. + */ + +char *lib_in_filename, *lib_out_filename; +char *patch_desc_filename; +struct patch_module_desc *patch_module_list; diff -r 000000000000 -r 12e230d431f0 tool/globals.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool/globals.h Wed Jun 03 07:40:00 2015 +0000 @@ -0,0 +1,5 @@ +/* external declarations for the global variables defined in globals.c */ + +extern char *lib_in_filename, *lib_out_filename; +extern char *patch_desc_filename; +extern struct patch_module_desc *patch_module_list; diff -r 000000000000 -r 12e230d431f0 tool/main.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool/main.c Wed Jun 03 07:40:00 2015 +0000 @@ -0,0 +1,23 @@ +/* + * libpatch main module + */ + +#include +#include +#include "globals.h" + +main(argc, argv) + char **argv; +{ + if (argc != 4) { + fprintf(stderr, "usage: %s in.out patch-desc out.lib\n", + argv[0]); + exit(1); + } + lib_in_filename = argv[1]; + patch_desc_filename = argv[2]; + lib_out_filename = argv[3]; + read_spec_file(); + /* code to be filled here */ + exit(0); +} diff -r 000000000000 -r 12e230d431f0 tool/patchinfo.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool/patchinfo.h Wed Jun 03 07:40:00 2015 +0000 @@ -0,0 +1,18 @@ +/* + * Our libpatch tool's first step will be to read and parse an ASCII text file + * specifying the patches to be applied. This information will be captured + * in data structures defined in this header file. + */ + +struct patch_module_desc { + char *member_name; + struct patch_desc *patches; + struct patch_module_desc *next; +}; + +struct patch_desc { + char *section; + unsigned offset; + u_char new_byte; + struct patch_desc *next; +}; diff -r 000000000000 -r 12e230d431f0 tool/readspec.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool/readspec.c Wed Jun 03 07:40:00 2015 +0000 @@ -0,0 +1,126 @@ +/* + * This module contains the code that reads and parses the patch + * description or specification ASCII text file. + */ + +#include +#include +#include +#include +#include +#include +#include "patchinfo.h" +#include "globals.h" + +static int lineno; +static struct patch_module_desc **module_add_ptr = &patch_module_list; +static struct patch_desc **patch_add_ptr; + +static void +set_module(name) + char *name; +{ + int len; + char *buf; + struct patch_module_desc *newmod; + + len = sizeof(struct patch_module_desc) + strlen(name) + 1; + buf = malloc(len); + if (!buf) { + perror("malloc"); + exit(1); + } + newmod = (struct patch_module_desc *) buf; + newmod->member_name = buf + sizeof(struct patch_module_desc); + strcpy(newmod->member_name, name); + newmod->patches = 0; + newmod->next = 0; + *module_add_ptr = newmod; + module_add_ptr = &newmod->next; + patch_add_ptr = &newmod->patches; +} + +static void +add_patch(argline) + char *argline; +{ + char *cp, *args[3]; + int i, len; + char *buf; + struct patch_desc *rec; + + for (cp = argline, i = 0; i < 3; i++) { + while (isspace(*cp)) + cp++; + if (!*cp) { + fprintf(stderr, "%s line %d: too few fields\n", + patch_desc_filename, lineno); + exit(1); + } + args[i] = cp; + while (*cp && !isspace(*cp)) + cp++; + if (*cp) + *cp++ = '\0'; + } + while (isspace(*cp)) + cp++; + if (*cp) { + fprintf(stderr, "%s line %d: too many fields\n", + patch_desc_filename, lineno); + exit(1); + } + if (!patch_add_ptr) { + fprintf(stderr, + "error: patch given before module (%s line %d)\n", + patch_desc_filename, lineno); + exit(1); + } + len = sizeof(struct patch_desc) + strlen(args[0]) + 1; + buf = malloc(len); + if (!buf) { + perror("malloc"); + exit(1); + } + rec = (struct patch_desc *) buf; + rec->section = buf + sizeof(struct patch_desc); + strcpy(rec->section, args[0]); + rec->offset = strtoul(args[1], 0, 16); + rec->new_byte = strtoul(args[2], 0, 16); + rec->next = 0; + *patch_add_ptr = rec; + patch_add_ptr = &rec->next; +} + +read_spec_file() +{ + FILE *f; + char linebuf[128], *cp, *np; + + f = fopen(patch_desc_filename, "r"); + if (!f) { + perror(patch_desc_filename); + exit(1); + } + for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) { + for (cp = linebuf; isspace(*cp); cp++) + ; + if (!*cp || *cp == '#') + continue; + if (*cp == '[') { + np = ++cp; + cp = index(cp, ']'); + if (!cp) { + fprintf(stderr, + "%s line %d: unmatched bracket\n", + patch_desc_filename, lineno); + exit(1); + } + *cp = '\0'; + set_module(np); + } else + add_patch(cp); + } + fclose(f); + return(0); +}