astrip.c 2.6 KB

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