| 1 | /* omflibcl.c (emx+gcc) -- Copyright (c) 1993-1996 by Eberhard Mattes */
|
|---|
| 2 |
|
|---|
| 3 | /* Copy an OMFLIB. */
|
|---|
| 4 |
|
|---|
| 5 | #include <stdio.h>
|
|---|
| 6 | #include <stdlib.h>
|
|---|
| 7 | #include <string.h>
|
|---|
| 8 | #include <errno.h>
|
|---|
| 9 | #include "omflib0.h"
|
|---|
| 10 | #include <sys/omflib.h>
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | int omflib_copy_lib (struct omflib *dst, struct omflib *src, char *error)
|
|---|
| 14 | {
|
|---|
| 15 | int i, rc, *reverse;
|
|---|
| 16 | long long_page;
|
|---|
| 17 | struct omf_rec rec;
|
|---|
| 18 |
|
|---|
| 19 | if (src->mod_count == -1 && omflib_make_mod_tab (src, error) != 0)
|
|---|
| 20 | return -1;
|
|---|
| 21 |
|
|---|
| 22 | /* Build a vector which maps page numbers to module table
|
|---|
| 23 | entries. */
|
|---|
| 24 |
|
|---|
| 25 | reverse = malloc (65536 * sizeof (*reverse));
|
|---|
| 26 | if (reverse == NULL)
|
|---|
| 27 | {
|
|---|
| 28 | errno = ENOMEM;
|
|---|
| 29 | return omflib_set_error (error);
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | for (i = 0; i < 65536; ++i)
|
|---|
| 33 | reverse[i] = -1;
|
|---|
| 34 | for (i = 0; i < src->mod_count; ++i)
|
|---|
| 35 | reverse[src->mod_tab[i].page] = i;
|
|---|
| 36 |
|
|---|
| 37 | /* Now read the source library sequentially, starting on page 1. */
|
|---|
| 38 |
|
|---|
| 39 | fseek (src->f, src->page_size, SEEK_SET);
|
|---|
| 40 | for (;;)
|
|---|
| 41 | {
|
|---|
| 42 | long_page = ftell (src->f) / src->page_size;
|
|---|
| 43 | if (long_page == -1)
|
|---|
| 44 | {
|
|---|
| 45 | omflib_set_error (error);
|
|---|
| 46 | free (reverse);
|
|---|
| 47 | return -1;
|
|---|
| 48 | }
|
|---|
| 49 | if (long_page > 65535)
|
|---|
| 50 | {
|
|---|
| 51 | strcpy (error, "Source library too big");
|
|---|
| 52 | free (reverse);
|
|---|
| 53 | return -1;
|
|---|
| 54 | }
|
|---|
| 55 | if (fread (&rec, sizeof (rec), 1, src->f) != 1)
|
|---|
| 56 | {
|
|---|
| 57 | if (ferror (src->f))
|
|---|
| 58 | omflib_set_error (error);
|
|---|
| 59 | else
|
|---|
| 60 | strcpy (error, "Unexpected end of file");
|
|---|
| 61 | free (reverse);
|
|---|
| 62 | return -1;
|
|---|
| 63 | }
|
|---|
| 64 | if (rec.rec_type == LIBEND)
|
|---|
| 65 | break;
|
|---|
| 66 | if (rec.rec_type != THEADR)
|
|---|
| 67 | {
|
|---|
| 68 | strcpy (error, "THEADR or LIBEND expected");
|
|---|
| 69 | free (reverse);
|
|---|
| 70 | return -1;
|
|---|
| 71 | }
|
|---|
| 72 | fseek (src->f, -sizeof (rec), SEEK_CUR);
|
|---|
| 73 | i = reverse[long_page];
|
|---|
| 74 | if (i == -1)
|
|---|
| 75 | {
|
|---|
| 76 | /* Unknown module, perhaps an import definition. Copy the
|
|---|
| 77 | module. */
|
|---|
| 78 | rc = omflib_copy_module (dst, dst->f, src, src->f, NULL, error);
|
|---|
| 79 | }
|
|---|
| 80 | else if (src->mod_tab[i].flags & FLAG_DELETED)
|
|---|
| 81 | {
|
|---|
| 82 | /* Module marked for deletion. Skip it. */
|
|---|
| 83 | rc = omflib_copy_module (NULL, NULL, src, src->f, NULL, error);
|
|---|
| 84 | }
|
|---|
| 85 | else
|
|---|
| 86 | {
|
|---|
| 87 | /* Copy the module. */
|
|---|
| 88 | rc = omflib_copy_module (dst, dst->f, src, src->f,
|
|---|
| 89 | src->mod_tab[i].name, error);
|
|---|
| 90 | }
|
|---|
| 91 | if (rc != 0)
|
|---|
| 92 | {
|
|---|
| 93 | free (reverse);
|
|---|
| 94 | return rc;
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | free (reverse);
|
|---|
| 98 | return 0;
|
|---|
| 99 | }
|
|---|