astrip.c 2.5 KB

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