astrip.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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$ */
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <signal.h>
  13. #include "out.h"
  14. #include "object.h"
  15. #include <missing_proto.h>
  16. /*
  17. astrip -- remove symbols and relocation bits
  18. */
  19. char temp_name[] = "/tmp/sXXXXXX";
  20. char *tname;
  21. FILE *tf;
  22. struct outhead buf;
  23. int readerror, writeerror;
  24. int strip(char *name);
  25. int copy(char *fnam, char *tnam, long size, int fr, int fw);
  26. int main(int argc, char *argv[])
  27. {
  28. int status;
  29. signal(SIGHUP, SIG_IGN);
  30. signal(SIGINT, SIG_IGN);
  31. signal(SIGQUIT, SIG_IGN);
  32. tname = mktemp(temp_name);
  33. while(--argc) {
  34. if ((status = strip(argv[argc])) > 1)
  35. break;
  36. }
  37. unlink(tname);
  38. exit(status);
  39. }
  40. int strip(char *name)
  41. {
  42. long size;
  43. int fw;
  44. if (! rd_open(name)) {
  45. fprintf(stderr, "astrip: cannot open %s\n", name);
  46. return(1);
  47. }
  48. readerror = 0;
  49. writeerror = 0;
  50. rd_ohead(&buf);
  51. if(readerror || BADMAGIC(buf)) {
  52. fprintf(stderr, "astrip: %s-- bad format\n", name);
  53. rd_close();
  54. return(1);
  55. }
  56. size = OFF_RELO(buf) - SZ_HEAD;
  57. buf.oh_flags &= ~HF_LINK;
  58. buf.oh_nrelo = 0;
  59. buf.oh_nname = 0;
  60. buf.oh_nchar = 0;
  61. if (! wr_open(tname)) {
  62. fprintf(stderr, "astrip: cannot create temp file %s\n", tname);
  63. rd_close();
  64. return(2);
  65. }
  66. wr_ohead(&buf);
  67. wr_close();
  68. if (writeerror) {
  69. fprintf(stderr, "astrip: write error on temp file %s\n", tname);
  70. rd_close();
  71. return(1);
  72. }
  73. fw = open(tname, 2);
  74. if (fw < 0 || lseek(fw, (long)SZ_HEAD, 0) < 0) {
  75. fprintf(stderr, "astrip: cannot create temp file %s\n", tname);
  76. rd_close();
  77. close(fw);
  78. return(2);
  79. }
  80. if(copy(name, tname, size, rd_fd(), fw)) {
  81. rd_close();
  82. close(fw);
  83. return(1);
  84. }
  85. rd_close();
  86. close(fw);
  87. size += SZ_HEAD;
  88. if (! rd_open(tname)) {
  89. fprintf(stderr, "astrip: cannot read temp file %s\n", tname);
  90. return(2);
  91. }
  92. fw = creat(name, 0777);
  93. if (fw < 0) {
  94. fprintf(stderr, "astrip: cannot write %s\n", name);
  95. rd_close();
  96. return(1);
  97. }
  98. if(copy(tname, name, size, rd_fd(), fw)) {
  99. close(fw);
  100. rd_close();
  101. return(2);
  102. }
  103. close(fw);
  104. rd_close();
  105. return(0);
  106. }
  107. int copy(char *fnam, char *tnam, long size, int fr, int fw)
  108. {
  109. int s/*, n*/;
  110. char lbuf[512];
  111. while(size != (long)0) {
  112. s = 512;
  113. if(size < 512)
  114. s = (int) size;
  115. rd_bytes(fr, lbuf, (long) s);
  116. if (readerror) {
  117. fprintf(stderr, "astrip: unexpected eof on %s\n", fnam);
  118. return(1);
  119. }
  120. wr_bytes(fw, lbuf, (long) s);
  121. if (writeerror) {
  122. fprintf(stderr, "astrip: write error on %s\n", tnam);
  123. return(1);
  124. }
  125. size -= (long)s;
  126. }
  127. return(0);
  128. }
  129. void rd_fatal()
  130. {
  131. readerror = 1;
  132. }
  133. void wr_fatal()
  134. {
  135. writeerror = 1;
  136. }