home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / utlsrc33.lzh / UTLSRC33 / SIZE.C < prev    next >
C/C++ Source or Header  |  1993-07-30  |  12KB  |  411 lines

  1. /* Size of rel file utility (`size') for GNU.
  2.    Copyright (C) 1986 Free Software Foundation, Inc.
  3.  
  4.                NO WARRANTY
  5.  
  6.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16.  
  17.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  18. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  19. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  20. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  21. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  22. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  23. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  24. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  25. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  27.  
  28.         GENERAL PUBLIC LICENSE TO COPY
  29.  
  30.   1. You may copy and distribute verbatim copies of this source file
  31. as you receive it, in any medium, provided that you conspicuously
  32. and appropriately publish on each copy a valid copyright notice
  33. "Copyright (C) 1986 Free Software Foundation, Inc.", and include
  34. following the copyright notice a verbatim copy of the above disclaimer
  35. of warranty and of this License.
  36.  
  37.   2. You may modify your copy or copies of this source file or
  38. any portion of it, and copy and distribute such modifications under
  39. the terms of Paragraph 1 above, provided that you also do the following:
  40.  
  41.     a) cause the modified files to carry prominent notices stating
  42.     that you changed the files and the date of any change; and
  43.  
  44.     b) cause the whole of any work that you distribute or publish,
  45.     that in whole or in part contains or is a derivative of this
  46.     program or any part thereof, to be licensed at no charge to all
  47.     third parties on terms identical to those contained in this
  48.     License Agreement (except that you may choose to grant more extensive
  49.     warranty protection to some or all third parties, at your option).
  50.  
  51.     c) You may charge a distribution fee for the physical act of
  52.     transferring a copy, and you may at your option offer warranty
  53.     protection in exchange for a fee.
  54.  
  55. Mere aggregation of another unrelated program with this program (or its
  56. derivative) on a volume of a storage or distribution medium does not bring
  57. the other program under the scope of these terms.
  58.  
  59.   3. You may copy and distribute this program (or a portion or derivative
  60. of it, under Paragraph 2) in object code or executable form under the terms
  61. of Paragraphs 1 and 2 above provided that you also do one of the following:
  62.  
  63.     a) accompany it with the complete corresponding machine-readable
  64.     source code, which must be distributed under the terms of
  65.     Paragraphs 1 and 2 above; or,
  66.  
  67.     b) accompany it with a written offer, valid for at least three
  68.     years, to give any third party free (except for a nominal
  69.     shipping charge) a complete machine-readable copy of the
  70.     corresponding source code, to be distributed under the terms of
  71.     Paragraphs 1 and 2 above; or,
  72.  
  73.     c) accompany it with the information you received as to where the
  74.     corresponding source code may be obtained.  (This alternative is
  75.     allowed only for noncommercial distribution and only if you
  76.     received the program in object code or executable form alone.)
  77.  
  78. For an executable file, complete source code means all the source code for
  79. all modules it contains; but, as a special exception, it need not include
  80. source code for modules which are standard libraries that accompany the
  81. operating system on which the executable file runs.
  82.  
  83.   4. You may not copy, sublicense, distribute or transfer this program
  84. except as expressly provided under this License Agreement.  Any attempt
  85. otherwise to copy, sublicense, distribute or transfer this program is void and
  86. your rights to use the program under this License agreement shall be
  87. automatically terminated.  However, parties who have received computer
  88. software programs from you with this License Agreement will not have
  89. their licenses terminated so long as such parties remain in full compliance.
  90.  
  91.   5. If you wish to incorporate parts of this program into other free
  92. programs whose distribution conditions are different, write to the Free
  93. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  94. worked out a simple rule that can be stated here, but we will often permit
  95. this.  We will be guided by the two goals of preserving the free status of
  96. all derivatives of our free software and of promoting the sharing and reuse of
  97. software.
  98.  
  99.  In other words, you are welcome to use, share and improve this program.
  100.  You are forbidden to forbid anyone else to use, share and improve
  101.  what you give them.   Help stamp out software-hoarding!  */
  102.  
  103. #if (CROSSATARI || atarist)
  104. #ifdef CROSSATARI
  105. #include "gnu-out.h"
  106. #include "st-out.h"
  107. #include "/usr/include/stdio.h"
  108. #include "/usr/include/strings.h"
  109. #include "gnu-ar.h"
  110. #include "/usr/include/sys/file.h"
  111. #else
  112. #include <gnu-out.h>
  113. #include <st-out.h>
  114. #include <stdio.h>
  115. #include <strings.h>
  116. #include <gnu-ar.h>
  117. #include <file.h>
  118. #endif
  119. #else
  120. #include <a.out.h>
  121. #include <stdio.h>
  122. #include <strings.h>
  123. #include <ar.h>
  124. #include <sys/file.h>
  125. #endif
  126.  
  127. /* Number of input file names specified.  */
  128.  
  129. int number_of_files;
  130.  
  131. /* Current file's name */
  132.  
  133. char *input_name;
  134.  
  135. /* Current member's name, or 0 if processing a non-library file.  */
  136.  
  137. char *input_member;
  138.  
  139. /* Offset within archive of the current member,
  140.    if we are processing an archive.  */
  141.  
  142. int member_offset;
  143.  
  144. void do_one_file (), do_one_rel_file ();
  145. char *concat ();
  146.  
  147. main (argc, argv)
  148.      char **argv;
  149.      int argc;
  150. {
  151.   int i;
  152.  
  153.   number_of_files = argc - 1;
  154.  
  155.   printf ("text\tdata\tbss\tdec\thex\n");
  156.  
  157.   /* Now scan and describe the files.  */
  158.  
  159.   if (number_of_files == 0)
  160.     do_one_file ("a.out");
  161.   else
  162.     for (i = 1; i < argc; i++)
  163.       do_one_file (argv[i]);
  164. }
  165.  
  166. /* Print the filename of the current file on 'outfile' (a stdio stream).  */
  167.  
  168. print_file_name (outfile)
  169.      FILE *outfile;
  170. {
  171.   fprintf (outfile, "%s", input_name);
  172.   if (input_member)
  173.     fprintf (outfile, "(%s)", input_member);
  174. }
  175.  
  176. /* process one input file */
  177. void scan_library ();
  178.  
  179. void
  180. do_one_file (name)
  181.      char *name;
  182. {
  183.   int len, magicnum, desc;
  184.  
  185.   desc = open (name, O_RDONLY, 0);
  186.  
  187.   if (desc < 0)
  188.     {
  189.       perror_name (name);
  190.       return;
  191.     }
  192.  
  193.   input_name = name;
  194.   input_member = 0;
  195.  
  196.   len = read (desc, &magicnum, sizeof magicnum);
  197.   if (len != sizeof magicnum)
  198.     error_with_file ("failure reading header");
  199.   else if (!N_BADMAG (*((struct exec *)&magicnum)))
  200.     do_one_rel_file (desc, 0);
  201.   else
  202.     {
  203.       char armag[SARMAG];
  204.  
  205.       lseek (desc, 0, 0);
  206.       if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  207.     error_with_file ("malformed input file (not rel or archive)");
  208.       else
  209.         scan_library (desc);
  210.     }
  211.  
  212.   close (desc);
  213. }
  214.  
  215. /* Read in the archive data about one member.
  216.    subfile_offset is the address within the archive of the start of that data.
  217.    The value returned is the length of the member's contents, which does
  218.    not include the archive data about the member.
  219.  
  220.    If there are no more valid members, zero is returned.  */
  221.  
  222. int
  223. decode_library_subfile (desc, subfile_offset)
  224.      int desc;
  225.      int subfile_offset;
  226. {
  227.   int bytes_read;
  228.   int namelen;
  229.   int member_length;
  230.   char *name;
  231.   struct ar_hdr hdr1;
  232.  
  233.   lseek (desc, subfile_offset, 0);
  234.  
  235.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  236.   if (!bytes_read)
  237.     ;        /* end of archive */
  238.  
  239.   else if (sizeof hdr1 != bytes_read)
  240.     error_with_file ("malformed library archive");
  241.  
  242.   else if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  243.     error_with_file ("malformatted header of archive member");
  244.  
  245.   else
  246.     {
  247.       namelen = index (hdr1.ar_name, ' ') - hdr1.ar_name;
  248.       name = (char *) xmalloc (namelen+1);
  249.       strncpy (name, hdr1.ar_name, namelen);
  250.       *(name + namelen) = 0;
  251.  
  252.       input_member = name;
  253.  
  254.       return member_length;
  255.     }
  256.   return 0;   /* tell caller to exit loop */
  257. }
  258.  
  259. /* Scan a library and describe each member.  */
  260.  
  261. void
  262. scan_library (desc)
  263.      int desc;
  264. {
  265.   int this_subfile_offset = SARMAG;
  266.   int member_length;
  267.   
  268.   while (member_length = decode_library_subfile (desc, this_subfile_offset))
  269.     {
  270.       /* describe every member except the ranlib data if any */
  271.       if (strcmp (input_member, "__.SYMDEF"))
  272.     do_one_rel_file (desc, this_subfile_offset + sizeof (struct ar_hdr));
  273.  
  274.       this_subfile_offset += ((member_length + sizeof (struct ar_hdr)) + 1) & -2;
  275.     }
  276. }
  277.  
  278. void read_header ();
  279.  
  280. void
  281. do_one_rel_file (desc, offset)
  282.      int desc;
  283.      int offset;
  284. {
  285.   struct exec header;   /* file header read in here */
  286.   int total;
  287.  
  288. #if 0
  289.   header.a_magic = 0;
  290. #else
  291.   N_SET_MAGIC(header, 0);
  292. #endif
  293.  
  294.   read_header (desc, &header, offset);
  295.  
  296.   if (N_BADMAG (header))
  297.     {
  298.       error_with_file ("bad magic number");
  299.       return;
  300.     }
  301.  
  302.   total = header.a_text + header.a_data + header.a_bss;
  303.   printf ("%d\t%d\t%d\t%d\t%x", header.a_text, header.a_data, header.a_bss, total, total);
  304.  
  305.   if (number_of_files > 1 || input_member)
  306.     {
  307.       printf ("\t");
  308.       print_file_name (stdout);
  309.     }
  310.   printf ("\n");
  311. }
  312.  
  313.  
  314. /* read a file's header */
  315.  
  316. void
  317. read_header (desc, loc, offset)
  318.      int desc;
  319.      struct exec *loc;
  320.      int offset;
  321. {
  322.   int len;
  323.   lseek (desc, offset, 0);
  324.   len = read (desc, loc, sizeof (struct exec));
  325.   if (len != sizeof (struct exec))
  326.     error_with_file ("failure reading header");
  327. }
  328.  
  329. /* Report a fatal error.
  330.    STRING is a printf format string and ARG is one arg for it.  */
  331.  
  332. fatal (string, arg)
  333.      char *string, *arg;
  334. {
  335.   fprintf (stderr, "size: ");
  336.   fprintf (stderr, string, arg);
  337.   fprintf (stderr, "\n");
  338.   exit (1);
  339. }
  340.  
  341. /* Report a nonfatal error.
  342.    STRING is a printf format string and ARG is one arg for it.  */
  343.  
  344. error (string, arg)
  345.      char *string, *arg;
  346. {
  347.   fprintf (stderr, "size: ");
  348.   fprintf (stderr, string, arg);
  349.   fprintf (stderr, "\n");
  350. }
  351.  
  352. /* Report a nonfatal error.
  353.    STRING is printed, followed by the current file name.  */
  354.  
  355. error_with_file (string)
  356.      char *string;
  357. {
  358.   fprintf (stderr, "size: ");
  359.   print_file_name (stderr);
  360.   fprintf (stderr, ": ");
  361.   fprintf (stderr, string);
  362.   fprintf (stderr, "\n");
  363. }
  364.  
  365. /* Report a fatal error using the message for the last failed system call,
  366.    followed by the string NAME.  */
  367.  
  368. perror_name (name)
  369.      char *name;
  370. {
  371.   extern int errno, sys_nerr;
  372.   extern char *sys_errlist[];
  373.   char *s;
  374.  
  375.   if (errno < sys_nerr)
  376.     s = concat (name, ": ", sys_errlist[errno]);
  377.   else
  378.     s = concat (name, ": ", "unknown error");
  379.   error (s, name);
  380. }
  381.  
  382. /* Like malloc but get fatal error if memory is exhausted.  */
  383.  
  384. int
  385. xmalloc (size)
  386.      int size;
  387. {
  388.   int result = malloc (size);
  389.   if (!result)
  390.     fatal ("virtual memory exhausted", 0);
  391.   return result;
  392. }
  393.  
  394. /* Return a newly-allocated string
  395.    whose contents concatenate those of S1, S2, S3.  */
  396.  
  397. char *
  398. concat (s1, s2, s3)
  399.      char *s1, *s2, *s3;
  400. {
  401.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  402.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  403.  
  404.   strcpy (result, s1);
  405.   strcpy (result + len1, s2);
  406.   strcpy (result + len1 + len2, s3);
  407.   *(result + len1 + len2 + len3) = 0;
  408.  
  409.   return result;
  410. }
  411.