ack/util/amisc/astrip.c

153 lines
2.5 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 "out.h"
#include <signal.h>
#include <stdio.h>
/*
astrip -- remove symbols and relocation bits
*/
char temp_name[] = "/tmp/sXXXXXX";
1985-04-12 14:55:00 +00:00
char *tname;
char *mktemp();
FILE *fopen();
FILE *tf;
struct outhead buf;
1987-01-15 22:06:16 +00:00
int readerror, writeerror;
1985-04-12 14:55:00 +00:00
main(argc, argv)
char **argv;
{
int status;
signal(SIGHUP, SIG_IGN);
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
tname = mktemp(temp_name);
1985-04-12 14:55:00 +00:00
while(--argc) {
if ((status = strip(argv[argc])) > 1)
break;
}
unlink(tname);
exit(status);
}
extern long lseek();
1985-04-12 14:55:00 +00:00
strip(name)
char *name;
{
long size;
int 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);
}
fw = open(tname, 2);
if (fw < 0 || lseek(fw, (long)SZ_HEAD, 0) < 0) {
fprintf(stderr, "astrip: cannot create temp file %s\n", tname);
rd_close();
close(fw);
return(2);
}
if(copy(name, tname, size, rd_fd(), fw)) {
1987-01-15 22:06:16 +00:00
rd_close();
close(fw);
1985-04-12 14:55:00 +00:00
return(1);
}
1987-01-15 22:06:16 +00:00
rd_close();
close(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);
}
1992-07-09 14:02:43 +00:00
fw = creat(name, 0777);
if (fw < 0) {
fprintf(stderr, "astrip: cannot write %s\n", name);
rd_close();
return(1);
}
if(copy(tname, name, size, rd_fd(), fw)) {
close(fw);
1987-01-15 22:06:16 +00:00
rd_close();
1985-04-12 14:55:00 +00:00
return(2);
}
close(fw);
1987-01-15 22:06:16 +00:00
rd_close();
1985-04-12 14:55:00 +00:00
return(0);
}
copy(fnam, tnam, size, fr, fw)
1985-04-12 14:55:00 +00:00
char *fnam;
char *tnam;
long size;
{
register s, n;
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);
}
1987-01-15 22:06:16 +00:00
rd_fatal()
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
}
1987-01-15 22:06:16 +00:00
wr_fatal()
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
}