ack/util/amisc/astrip.c

153 lines
2.8 KiB
C
Raw Normal View History

1987-03-09 19:15:41 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1994-06-24 11:31:16 +00:00
/* $Id$ */
1985-04-12 14:55:00 +00:00
#include <stdlib.h>
1985-04-12 14:55:00 +00:00
#include <stdio.h>
2019-03-24 15:53:13 +00:00
#include <sys/stat.h>
#include "system.h"
#include "object.h"
#include "out.h"
1985-04-12 14:55:00 +00:00
/*
astrip -- remove symbols and relocation bits
*/
2019-03-24 15:53:13 +00:00
char tname[L_tmpnam];
1985-04-12 14:55:00 +00:00
FILE *tf;
struct outhead buf;
1987-01-15 22:06:16 +00:00
int readerror, writeerror;
1985-04-12 14:55:00 +00:00
2019-03-24 15:53:13 +00:00
static int copy(char *, char *, long, FILE *, FILE *);
static int strip(char *);
int main(int argc, char **argv)
1985-04-12 14:55:00 +00:00
{
int status;
2019-03-24 15:53:13 +00:00
if (sys_tmpnam(tname)==NULL)
{
fprintf(stderr, "astrip: cannot create temporary filename\n");
return(1);
}
fclose(fopen(tname,"wb"));
1985-04-12 14:55:00 +00:00
while(--argc) {
if ((status = strip(argv[argc])) > 1)
break;
}
2019-03-24 15:53:13 +00:00
remove(tname);
return status;
1985-04-12 14:55:00 +00:00
}
2019-03-24 15:53:13 +00:00
int strip(char *name)
1985-04-12 14:55:00 +00:00
{
long size;
2019-03-24 15:53:13 +00:00
FILE *fw;
1985-04-12 14:55:00 +00:00
1987-01-15 22:06:16 +00:00
if (! rd_open(name)) {
1985-04-12 14:55:00 +00:00
fprintf(stderr, "astrip: cannot open %s\n", name);
return(1);
}
1987-01-15 22:06:16 +00:00
readerror = 0;
writeerror = 0;
rd_ohead(&buf);
if(readerror || BADMAGIC(buf)) {
1985-04-12 14:55:00 +00:00
fprintf(stderr, "astrip: %s-- bad format\n", name);
1987-01-15 22:06:16 +00:00
rd_close();
1985-04-12 14:55:00 +00:00
return(1);
}
size = OFF_RELO(buf) - SZ_HEAD;
buf.oh_flags &= ~HF_LINK;
buf.oh_nrelo = 0;
buf.oh_nname = 0;
buf.oh_nchar = 0;
1987-01-15 22:06:16 +00:00
if (! wr_open(tname)) {
1985-04-12 14:55:00 +00:00
fprintf(stderr, "astrip: cannot create temp file %s\n", tname);
1987-01-15 22:06:16 +00:00
rd_close();
1985-04-12 14:55:00 +00:00
return(2);
}
1987-01-15 22:06:16 +00:00
wr_ohead(&buf);
wr_close();
1987-01-15 22:06:16 +00:00
if (writeerror) {
fprintf(stderr, "astrip: write error on temp file %s\n", tname);
rd_close();
return(1);
}
2019-03-24 15:53:13 +00:00
fw = fopen(tname, "ab");
if ((fw == NULL) || (fseek(fw, (long)SZ_HEAD, SEEK_SET)!=0)) {
fprintf(stderr, "astrip: cannot create temp file %s\n", tname);
rd_close();
2019-03-24 15:53:13 +00:00
fclose(fw);
return(2);
}
if(copy(name, tname, size, rd_fd(), fw)) {
1987-01-15 22:06:16 +00:00
rd_close();
2019-03-24 15:53:13 +00:00
fclose(fw);
1985-04-12 14:55:00 +00:00
return(1);
}
1987-01-15 22:06:16 +00:00
rd_close();
2019-03-24 15:53:13 +00:00
fclose(fw);
1985-04-12 14:55:00 +00:00
size += SZ_HEAD;
1987-01-15 22:06:16 +00:00
if (! rd_open(tname)) {
1985-04-12 14:55:00 +00:00
fprintf(stderr, "astrip: cannot read temp file %s\n", tname);
return(2);
}
2019-03-24 15:53:13 +00:00
fw = fopen(name, "wb");
if (fw == NULL) {
1992-07-09 14:02:43 +00:00
fprintf(stderr, "astrip: cannot write %s\n", name);
rd_close();
return(1);
}
if(copy(tname, name, size, rd_fd(), fw)) {
2019-03-24 15:53:13 +00:00
fclose(fw);
1987-01-15 22:06:16 +00:00
rd_close();
1985-04-12 14:55:00 +00:00
return(2);
}
2019-03-24 15:53:13 +00:00
fclose(fw);
1987-01-15 22:06:16 +00:00
rd_close();
2019-03-24 15:53:13 +00:00
/* Change the mode to everything. */
chmod(name,S_IRWXU | S_IRWXG | S_IRWXO);
1985-04-12 14:55:00 +00:00
return(0);
}
2019-03-24 15:53:13 +00:00
static int copy(char *fnam, char *tnam, long size, FILE *fr, FILE *fw)
1985-04-12 14:55:00 +00:00
{
2019-03-24 15:53:13 +00:00
register int s;
1985-04-12 14:55:00 +00:00
char lbuf[512];
while(size != (long)0) {
s = 512;
if(size < 512)
s = (int) size;
1987-01-15 22:06:16 +00:00
rd_bytes(fr, lbuf, (long) s);
if (readerror) {
1985-04-12 14:55:00 +00:00
fprintf(stderr, "astrip: unexpected eof on %s\n", fnam);
return(1);
}
1987-01-15 22:06:16 +00:00
wr_bytes(fw, lbuf, (long) s);
if (writeerror) {
1985-04-12 14:55:00 +00:00
fprintf(stderr, "astrip: write error on %s\n", tnam);
return(1);
}
size -= (long)s;
}
return(0);
}
2019-03-24 15:53:13 +00:00
void rd_fatal(void)
1985-04-12 14:55:00 +00:00
{
1987-01-15 22:06:16 +00:00
readerror = 1;
1985-04-12 14:55:00 +00:00
}
2019-03-24 15:53:13 +00:00
void wr_fatal(void)
1985-04-12 14:55:00 +00:00
{
1987-01-15 22:06:16 +00:00
writeerror = 1;
1985-04-12 14:55:00 +00:00
}