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