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