size.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* Size of rel file utility (`size') for GNU.
  2. Copyright (C) 1986 Richard M. Stallman
  3. NO WARRANTY
  4. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  5. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  6. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  7. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  8. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  9. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  10. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  11. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  12. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  13. CORRECTION.
  14. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  15. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  16. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  17. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  18. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  19. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  20. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  21. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  22. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  23. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  24. GENERAL PUBLIC LICENSE TO COPY
  25. 1. You may copy and distribute verbatim copies of this source file
  26. as you receive it, in any medium, provided that you conspicuously
  27. and appropriately publish on each copy a valid copyright notice
  28. "Copyright (C) 1986 Richard M. Stallman"; and include following the
  29. copyright notice a verbatim copy of the above disclaimer of warranty
  30. and of this License.
  31. 2. You may modify your copy or copies of this source file or
  32. any portion of it, and copy and distribute such modifications under
  33. the terms of Paragraph 1 above, provided that you also do the following:
  34. a) cause the modified files to carry prominent notices stating
  35. that you changed the files and the date of any change; and
  36. b) cause the whole of any work that you distribute or publish,
  37. that in whole or in part contains or is a derivative of this
  38. program or any part thereof, to be freely distributed
  39. and licensed to all third parties on terms identical to those
  40. contained in this License Agreement (except that you may choose
  41. to grant more extensive warranty protection to third parties,
  42. at your option).
  43. 3. You may copy and distribute this program or any portion of it in
  44. compiled, executable or object code form under the terms of Paragraphs
  45. 1 and 2 above provided that you do the following:
  46. a) cause each such copy to be accompanied by the
  47. corresponding machine-readable source code, which must
  48. be distributed under the terms of Paragraphs 1 and 2 above; or,
  49. b) cause each such copy to be accompanied by a
  50. written offer, with no time limit, to give any third party
  51. free (except for a nominal shipping charge) a machine readable
  52. copy of the corresponding source code, to be distributed
  53. under the terms of Paragraphs 1 and 2 above; or,
  54. c) in the case of a recipient of this program in compiled, executable
  55. or object code form (without the corresponding source code) you
  56. shall cause copies you distribute to be accompanied by a copy
  57. of the written offer of source code which you received along
  58. with the copy you received.
  59. 4. You may not copy, sublicense, distribute or transfer this program
  60. except as expressly provided under this License Agreement. Any attempt
  61. otherwise to copy, sublicense, distribute or transfer this program is void and
  62. your rights to use the program under this License agreement shall be
  63. automatically terminated. However, parties who have received computer
  64. software programs from you with this License Agreement will not have
  65. their licenses terminated so long as such parties remain in full compliance.
  66. In other words, you are welcome to use, share and improve this program.
  67. You are forbidden to forbid anyone else to use, share and improve
  68. what you give them. Help stamp out software-hoarding! */
  69. /* define this for running on the TI NU machine. */
  70. /* #define numachine 1 */
  71. #ifdef numachine
  72. #define O_RDONLY 0
  73. #endif
  74. #include <a.out.h>
  75. #include <stdio.h>
  76. #include <strings.h>
  77. #include <ar.h>
  78. #ifndef numachine
  79. #include <sys/file.h>
  80. #endif
  81. int number_of_files;
  82. /* current file's name */
  83. char *input_name;
  84. /* current member's name, or 0 if processing a non-library file. */
  85. char *input_member;
  86. /* offset within archive of the current member, if we are processing an archive. */
  87. int member_offset;
  88. void do_one_file (), do_one_rel_file ();
  89. char *concat ();
  90. main(argc, argv)
  91. char **argv;
  92. int argc;
  93. {
  94. int i;
  95. number_of_files = argc - 1;
  96. if (!number_of_files)
  97. fatal ("no files specified", 0);
  98. printf ("text\tdata\tbss\tdec\thex\n");
  99. /* Now scan and describe the files. */
  100. for (i = 1; i < argc; i++)
  101. do_one_file (argv[i]);
  102. }
  103. /* open the file specified by `name', and return a descriptor. */
  104. /* Print the filename of the current file on 'outfile' (a stdio stream). */
  105. print_file_name (outfile)
  106. FILE *outfile;
  107. {
  108. fprintf (outfile, "%s", input_name);
  109. if (input_member)
  110. fprintf (outfile, "(%s)", input_member);
  111. }
  112. /* process one input file */
  113. void scan_library ();
  114. void
  115. do_one_file (name)
  116. char *name;
  117. {
  118. int len, magicnum, desc;
  119. desc = open (name, O_RDONLY, 0);
  120. if (desc < 0)
  121. {
  122. perror_name (name);
  123. return;
  124. }
  125. input_name = name;
  126. input_member = 0;
  127. len = read (desc, &magicnum, sizeof magicnum);
  128. if (len != sizeof magicnum)
  129. error_with_file ("failure reading header of ");
  130. else if (!N_BADMAG(*((struct exec *)&magicnum)))
  131. do_one_rel_file (desc, 0);
  132. else
  133. {
  134. char armag[SARMAG];
  135. lseek (desc, 0, 0);
  136. if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  137. error_with_file ("malformed input file (not rel or archive) ");
  138. else
  139. scan_library (desc);
  140. }
  141. close (desc);
  142. }
  143. /* read in the archive data about one member.
  144. subfile_offset is the address within the archive of the start of that data.
  145. The value returned is the length of the member's contents, which does
  146. not include the archive data about the member.
  147. If there are no more valid members, zero is returned. */
  148. int
  149. decode_library_subfile (desc, subfile_offset)
  150. int desc;
  151. int subfile_offset;
  152. {
  153. int bytes_read;
  154. int namelen;
  155. int member_length;
  156. char *name;
  157. struct ar_hdr hdr1;
  158. lseek (desc, subfile_offset, 0);
  159. bytes_read = read (desc, &hdr1, sizeof hdr1);
  160. if (!bytes_read)
  161. ; /* end of archive */
  162. else if (sizeof hdr1 != bytes_read)
  163. error_with_file ("malformed library archive ");
  164. else if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  165. error_with_file ("malformatted header of archive member in ");
  166. else
  167. {
  168. namelen = index (hdr1.ar_name, ' ') - hdr1.ar_name;
  169. name = (char *) xmalloc (namelen+1);
  170. strncpy (name, hdr1.ar_name, namelen);
  171. *(name + namelen) = 0;
  172. input_member = name;
  173. return member_length;
  174. }
  175. return 0; /* tell caller to exit loop */
  176. }
  177. /* Scan a library and describe each member. */
  178. void
  179. scan_library (desc)
  180. int desc;
  181. {
  182. int this_subfile_offset = SARMAG;
  183. int member_length;
  184. while (member_length = decode_library_subfile (desc, this_subfile_offset))
  185. {
  186. /* describe every member except the ranlib data if any */
  187. if (strcmp (input_member, "__.SYMDEF"))
  188. do_one_rel_file (desc, this_subfile_offset + sizeof (struct ar_hdr));
  189. this_subfile_offset += ((member_length + sizeof (struct ar_hdr)) + 1) & -2;
  190. }
  191. }
  192. void read_header ();
  193. void
  194. do_one_rel_file (desc, offset)
  195. int desc;
  196. int offset;
  197. {
  198. struct exec header; /* file header read in here */
  199. int total;
  200. header.a_magic = 0;
  201. read_header (desc, &header, offset);
  202. if (N_BADMAG(header))
  203. {
  204. error_with_file ("bad magic number in ");
  205. return;
  206. }
  207. total = header.a_text + header.a_data + header.a_bss;
  208. printf ("%d\t%d\t%d\t%d\t%x", header.a_text, header.a_data, header.a_bss, total, total);
  209. if (number_of_files > 1 || input_member)
  210. {
  211. printf ("\t");
  212. print_file_name (stdout);
  213. }
  214. printf ("\n");
  215. }
  216. /* read a file's header */
  217. void
  218. read_header (desc, loc, offset)
  219. int desc;
  220. struct exec *loc;
  221. int offset;
  222. {
  223. int len;
  224. lseek (desc, offset, 0);
  225. len = read (desc, loc, sizeof (struct exec));
  226. if (len != sizeof (struct exec))
  227. error_with_file ("failure reading header of ");
  228. }
  229. /* Report a fatal error. 'string' is a printf format string and 'arg' is one arg for it. */
  230. fatal (string, arg)
  231. char *string, *arg;
  232. {
  233. fprintf (stderr, "size: ");
  234. fprintf (stderr, string, arg);
  235. fprintf (stderr, "\n");
  236. exit (1);
  237. }
  238. /* Report a nonfatal error. 'string' is a printf format string and 'arg' is one arg for it. */
  239. error (string, arg)
  240. char *string, *arg;
  241. {
  242. fprintf (stderr, "size: ");
  243. fprintf (stderr, string, arg);
  244. fprintf (stderr, "\n");
  245. }
  246. /* Report a nonfatal error. 'string' is printed, followed by the current file name. */
  247. error_with_file (string)
  248. char *string;
  249. {
  250. fprintf (stderr, "size: ");
  251. fprintf (stderr, string);
  252. print_file_name (stderr);
  253. fprintf (stderr, "\n");
  254. }
  255. /* Report a fatal error using the message for the last failed system call,
  256. followed by the string 'name'. */
  257. perror_name (name)
  258. char *name;
  259. {
  260. extern int errno, sys_nerr;
  261. extern char *sys_errlist[];
  262. char *s;
  263. if (errno < sys_nerr)
  264. s = concat ("", sys_errlist[errno], " for %s");
  265. else
  266. s = "cannot open %s";
  267. error (s, name);
  268. }
  269. /* Like malloc but get fatal error if memory is exhausted. */
  270. int
  271. xmalloc (size)
  272. int size;
  273. {
  274. int result = malloc (size);
  275. if (!result)
  276. fatal ("virtual memory exhausted", 0);
  277. return result;
  278. }
  279. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
  280. char *
  281. concat (s1, s2, s3)
  282. char *s1, *s2, *s3;
  283. {
  284. int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  285. char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  286. strcpy (result, s1);
  287. strcpy (result + len1, s2);
  288. strcpy (result + len1 + len2, s3);
  289. *(result + len1 + len2 + len3) = 0;
  290. return result;
  291. }