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