|
|
1.1 root 1: /*
2: * static char ID_cp[] = "@(#) copy.c: 1.4 10/7/83";
3:
4: */
5: #include <stdio.h>
6: #include <sys/types.h>
7: #include <sys/stat.h>
8: #define ERROR 0
9: #define BSIZE 256
10: #include "filehdr.h"
11: #include "scnhdr.h"
12: #include "reloc.h"
13: #include "syms.h"
14: #include "linenum.h"
15: #include "ldfcn.h"
16:
17: LDFILE *oldfile;
18: FILE *compress;
19:
20: int
21: cpyfile(argv,argv2)
22: char *argv;
23: char *argv2;
24: {
25:
26: extern int copy();
27: extern int vflag;
28: FILHDR fhead;
29: LINENO line;
30: RELOC rel;
31: SCNHDR sect;
32: SYMENT symbol;
33: AUXENT auxil;
34: long indx;
35: long datasze;
36: int sumrel;
37: int sumln;
38: long frstrel;
39: long frstln;
40: int i;
41: long k;
42: #ifdef FLEXNAMES
43: char buf[BUFSIZ];
44: #endif
45: struct stat statrec;
46: unsigned short fmode;
47:
48: indx = 0;
49: sumrel = 0;
50: sumln = 0;
51: frstrel = 0;
52: frstln = 0;
53:
54: if ((oldfile = ldopen(argv, NULL)) == NULL) {
55: fprintf(stderr,"unable to open %s \n", *argv);
56: return(FAILURE);
57: }
58: if (stat(argv, &statrec) != 0) {
59: fprintf(stderr,"unable to obtain mode of %s \n", *argv);
60: return(FAILURE);
61: }
62: fmode=statrec.st_mode;
63: if ((compress = fopen(argv2, "w")) == NULL) {
64: fprintf(stderr,"unable to create %s\n", argv2);
65: return(FAILURE);
66: }
67: chmod(argv2,fmode);
68:
69: if ( ldfhread(oldfile, &fhead) == FAILURE) {
70: if (vflag > 0)
71: fprintf(stderr,"cannot read header\n");
72: return(ERROR);
73: }
74:
75: indx += FILHSZ;
76: if (fwrite(&fhead, FILHSZ, 1, compress) != 1)
77: return(ERROR);
78:
79: if ( ldohseek(oldfile) == SUCCESS)
80: if ( copy((long)HEADER(oldfile).f_opthdr) != SUCCESS) {
81: if ( vflag > 0)
82: fprintf(stderr,"error in copying opt header\n");
83: return(ERROR);
84: }
85: indx += HEADER(oldfile).f_opthdr;
86:
87: for (i=1; i <= HEADER(oldfile).f_nscns; ++i) {
88: if ( ldshread(oldfile, i, §) != SUCCESS) {
89: if ( vflag > 0)
90: fprintf(stderr,"cannot read sect head at %d\n", i);
91: return(ERROR);
92: }
93: if ( i==0 || frstrel == 0)
94: frstrel = sect.s_relptr;
95: if ( i == 0 || frstln == 0)
96: frstln = sect.s_lnnoptr;
97: sumrel += sect.s_nreloc;
98: sumln += sect.s_nlnno;
99:
100: if( fwrite(§, SCNHSZ, 1, compress) != 1)
101: return(FAILURE);
102: indx += SCNHSZ;
103: }
104:
105: if (sumrel != 0)
106: datasze = frstrel - indx;
107: else
108: if (sumln != 0)
109: datasze = frstln - indx;
110: else
111: datasze = HEADER(oldfile).f_symptr - indx;
112:
113: if ( datasze != 0)
114: if ( copy((long)datasze) != SUCCESS) {
115: if ( vflag > 0)
116: fprintf(stderr,"error in copying raw data\n");
117: return(FAILURE);
118: }
119: indx = indx + datasze;
120:
121: if ( HEADER(oldfile).f_flags != F_RELFLG && sumrel != 0)
122: for ( i=0; i < sumrel; i++) {
123: if (fread(&rel, RELSZ, 1, IOPTR(oldfile)) < 1)
124: return(FAILURE);
125: if ( fwrite(&rel, RELSZ, 1, compress) != 1)
126: return(FAILURE);
127: indx += RELSZ;
128: }
129:
130: if ( HEADER(oldfile).f_flags != F_LNNO && sumln != 0) {
131: for ( i=0; i < sumln; i++) {
132: if (fread(&line, LINESZ, 1, IOPTR(oldfile)) < 1)
133: return(FAILURE);
134: if (fwrite(&line, LINESZ, 1, compress) != 1)
135: return(FAILURE);
136: indx += LINESZ;
137: }
138: }
139:
140: if ( indx != HEADER(oldfile).f_symptr) {
141: fprintf(stderr,"error in indexing\n");
142: return(FAILURE);
143: }
144:
145: for(k=0L; k < HEADER(oldfile).f_nsyms; k++) {
146: ldtbread(oldfile, k, &symbol);
147: fwrite(&symbol, SYMESZ, 1, compress);
148: for ( i=0; i < symbol.n_numaux; i++) {
149: if (fread(&auxil, AUXESZ, 1, IOPTR(oldfile)) != 1)
150: return(FAILURE);
151: k++;
152: if(fwrite(&auxil, AUXESZ, 1, compress) != 1)
153: return(FAILURE);
154: }
155: }
156: #ifdef FLEXNAMES
157: /*
158: * Copy the string table (rest of the object file)
159: */
160: while ((i = fread(buf, sizeof(char), BUFSIZ, IOPTR(oldfile))) > 0)
161: {
162: i -= fwrite(buf, sizeof(char), i, compress);
163: if (i != 0)
164: return (FAILURE);
165: }
166: if (i != 0)
167: return (FAILURE);
168: #endif
169: fclose(compress);
170: ldclose(oldfile);
171: return(SUCCESS);
172: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.