Pristine Ack-5.5
[Ack-5.5.git] / util / amisc / astrip.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  */
5 /* $Id: astrip.c,v 1.10 1994/06/24 10:13:51 ceriel Exp $ */
6
7 #include "out.h"
8 #include <signal.h>
9 #include <stdio.h>
10
11 /*
12
13         astrip -- remove symbols and relocation bits
14
15 */
16
17 char    temp_name[] = "/tmp/sXXXXXX";
18 char    *tname;
19 char    *mktemp();
20 FILE    *fopen();
21 FILE    *tf;
22 struct outhead buf;
23 int     readerror, writeerror;
24
25 main(argc, argv)
26 char **argv;
27 {
28         int     status;
29
30         signal(SIGHUP, SIG_IGN);
31         signal(SIGINT, SIG_IGN);
32         signal(SIGQUIT, SIG_IGN);
33         tname = mktemp(temp_name);
34         while(--argc) {
35                 if ((status = strip(argv[argc])) > 1)
36                         break;
37         }
38         unlink(tname);
39         exit(status);
40 }
41
42 extern long lseek();
43
44 strip(name)
45 char *name;
46 {
47         long size;
48         int fw;
49
50         if (! rd_open(name)) {
51                 fprintf(stderr, "astrip: cannot open %s\n", name);
52                 return(1);
53         }
54         readerror = 0;
55         writeerror = 0;
56         rd_ohead(&buf);
57         if(readerror || BADMAGIC(buf)) {
58                 fprintf(stderr, "astrip: %s-- bad format\n", name);
59                 rd_close();
60                 return(1);
61         }
62         size = OFF_RELO(buf) - SZ_HEAD;
63         buf.oh_flags &= ~HF_LINK;
64         buf.oh_nrelo = 0;
65         buf.oh_nname = 0;
66         buf.oh_nchar = 0;
67
68
69         if (! wr_open(tname)) {
70                 fprintf(stderr, "astrip: cannot create temp file %s\n", tname);
71                 rd_close();
72                 return(2);
73         }
74         wr_ohead(&buf);
75         wr_close();
76         if (writeerror) {
77                 fprintf(stderr, "astrip: write error on temp file %s\n", tname);
78                 rd_close();
79                 return(1);
80         }
81         fw = open(tname, 2);
82         if (fw < 0 || lseek(fw, (long)SZ_HEAD, 0) < 0) {
83                 fprintf(stderr, "astrip: cannot create temp file %s\n", tname);
84                 rd_close();
85                 close(fw);
86                 return(2);
87         }
88         if(copy(name, tname, size, rd_fd(), fw)) {
89                 rd_close();
90                 close(fw);
91                 return(1);
92         }
93         rd_close();
94         close(fw);
95         size += SZ_HEAD;
96         if (! rd_open(tname)) {
97                 fprintf(stderr, "astrip: cannot read temp file %s\n", tname);
98                 return(2);
99         }
100         fw = creat(name, 0777);
101         if (fw < 0) {
102                 fprintf(stderr, "astrip: cannot write %s\n", name);
103                 rd_close();
104                 return(1);
105         }
106         if(copy(tname, name, size, rd_fd(), fw)) {
107                 close(fw);
108                 rd_close();
109                 return(2);
110         }
111
112         close(fw);
113         rd_close();
114         return(0);
115 }
116
117 copy(fnam, tnam, size, fr, fw)
118 char *fnam;
119 char *tnam;
120 long size;
121 {
122         register s, n;
123         char lbuf[512];
124
125         while(size != (long)0) {
126                 s = 512;
127                 if(size < 512)
128                         s = (int) size;
129                 rd_bytes(fr, lbuf, (long) s);
130                 if (readerror) {
131                         fprintf(stderr, "astrip: unexpected eof on %s\n", fnam);
132                         return(1);
133                 }
134                 wr_bytes(fw, lbuf, (long) s);
135                 if (writeerror) {
136                         fprintf(stderr, "astrip: write error on %s\n", tnam);
137                         return(1);
138                 }
139                 size -= (long)s;
140         }
141         return(0);
142 }
143
144 rd_fatal()
145 {
146         readerror = 1;
147 }
148
149 wr_fatal()
150 {
151         writeerror = 1;
152 }