home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / sozobon / scsrc20 / tools / size.c < prev   
C/C++ Source or Header  |  1991-02-22  |  6KB  |  357 lines

  1. /* Copyright (c) 1988,89,91 by Sozobon, Limited.  Author: Tony Andrews
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  */
  11.  
  12. static    char    Version[] =
  13. "size: version 2.00  Copyright (c) 1988,89,91 by Sozobon, Limited.";
  14.  
  15. #include <stdio.h>
  16. #ifndef UNIXHOST
  17. #include <ar.h>
  18. #else
  19. #include "crossar.h"
  20. #endif
  21.  
  22. #ifdef    MINIX
  23. #define    O_RDONLY    0
  24. #else
  25. #include <fcntl.h>
  26. #endif
  27.  
  28. #ifndef UNIXHOST
  29. #define SWAPW(a,b)
  30. #define SWAPL(a,b)
  31. #else
  32. #define SWAPW(a,b)    swapw(a,b)
  33. #define SWAPL(a,b)    swapl(a,b)
  34. long crossl();
  35. #endif
  36.  
  37. /*
  38.  * Object file header
  39.  */
  40. struct hdr_l {
  41.     long    tsize;
  42.     long    dsize;
  43.     long    bsize;
  44.     long    ssize;
  45.     long    stksize;
  46.     long    entrypt;
  47. };
  48.  
  49. #define    OMAGIC    0x601a        /* object module magic number */
  50.  
  51. #ifdef MINIX
  52.  
  53. #define MMAGIC    0x4100301L
  54. #define MMAGIC2    0x20
  55.  
  56. struct mhdr {
  57.     long magic;
  58.     long magic2;
  59.     long tsize, dsize, bsize;
  60.     long fill;
  61.     long totsize;
  62.     long syms;
  63. };
  64. #endif
  65.  
  66. extern    long    lseek();
  67.  
  68. int    oflag = 0;
  69. int    xflag = 0;
  70.  
  71. usage()
  72. {
  73.     fprintf(stderr, "usage: size [-o|-x] [-V] file ...\n");
  74.     exit(1);
  75. }
  76.  
  77. main(argc, argv)
  78. int    argc;
  79. char    *argv[];
  80. {
  81.     for (argv++; *argv != NULL && argv[0][0] == '-' ;argv++) {
  82.         switch (argv[0][1]) {
  83.  
  84.         case 'o':
  85.         case 'O':
  86.             oflag++; break;
  87.         case 'x':
  88.         case 'X':
  89.             xflag++; break;
  90.         case 'V':
  91.         case 'v':
  92.             printf("%s\n", Version);
  93.             break;
  94.         default:
  95.             usage();
  96.         }
  97.     }
  98.  
  99.     for (; *argv != NULL; argv++)
  100.         dofile(*argv);
  101.  
  102.     exit(0);
  103. }
  104.  
  105. dofile(f)
  106. char    *f;
  107. {
  108.     int    fd;
  109.     struct    hdr_l    h;
  110.     int    magic;
  111.  
  112.     if ((fd = open(f, O_RDONLY)) < 0) {
  113.         fprintf(stderr, "size: can't open file '%s'\n", f);
  114.         return;
  115.     }
  116.  
  117.     if (read(fd, &magic, 2) != 2) {
  118.         fprintf(stderr, "size: '%s' not an object module\n", f);
  119.         close(fd);
  120.         return;
  121.     }
  122.  
  123.     SWAPW(&magic, 1);
  124.     switch (magic) {
  125.  
  126.     case OMAGIC:    /* object module */
  127.         if (read(fd, &h, sizeof(h)) != sizeof(h)) {
  128.             fprintf(stderr, "size: '%s' not an object module\n", f);
  129.             close(fd);
  130.             return;
  131.         }
  132.         showit((char *) 0, f, &h);
  133.         break;
  134.  
  135.     case ARMAG1:    /* libraries */
  136.     case ARMAG2:
  137.         dolib(f, fd);
  138.         break;
  139.  
  140.     default:
  141.  
  142. #ifdef    MINIX
  143.         {
  144.         long    lmagic;
  145.         struct    mhdr    mh;
  146.  
  147.         lseek(fd, 0L, 0);
  148.  
  149.         if (read(fd, &lmagic, 4) == 4 && 
  150. #ifdef UNIXHOST
  151.             (SWAPL(&lmagic, 1) == 0) &&
  152. #endif
  153.             lmagic == MMAGIC) {
  154.             lseek(fd, 0L, 0);
  155.             if (read(fd, &mh, sizeof(mh)) != sizeof(mh) ||
  156. #ifdef UNIXHOST
  157.                 SWAPL(&mh, sizeof(mh)/4) ||
  158. #endif
  159.                 mh.magic2 != MMAGIC2) {
  160.                 fprintf(stderr,
  161.                     "size: '%s' not an object module\n", f);
  162.                 close(fd);
  163.                 return;
  164.             }
  165.             mshowit((char *) 0, f, &mh);
  166.             close(fd);
  167.             return;
  168.         }
  169.         }
  170. #endif
  171.         fprintf(stderr, "size: unknown file type '%s'\n", f);
  172.         break;
  173.  
  174.     }
  175.  
  176.     close(fd);
  177. }
  178.  
  179. /*
  180.  * gethdr(fd, a) - read an archive header at the current file position
  181.  *
  182.  * Returns TRUE on success, FALSE at eof.
  183.  */
  184. int
  185. gethdr(fd, a)
  186. int    fd;
  187. struct    ar_hdr    *a;
  188. {
  189.     if (read(fd, a, ARHSZ) == ARHSZ)
  190.         return (a->ar_name[0] != '\0');
  191.     else
  192.         return 0;
  193. }
  194.  
  195. /*
  196.  * dolib(f, fd) - show the size of all object modules in a library
  197.  */
  198. dolib(f, fd)
  199. char    *f;
  200. int    fd;
  201. {
  202.     struct    ar_hdr    a;
  203.     struct    hdr_l    h;
  204.     short magic;
  205.     long    next;
  206.     long    ts, ds, bs;
  207.  
  208.     ts = ds = bs = 0;
  209.  
  210.     while (gethdr(fd, &a)) {
  211. #ifndef UNIXHOST
  212.         next = lseek(fd, 0L, 1) + a.ar_size;
  213. #else
  214.         next = lseek(fd, 0L, 1) + crossl(a.ar_size);
  215. #endif
  216.         if (read(fd, &magic, sizeof(magic)) != sizeof(magic) || 
  217. #ifdef UNIXHOST
  218.             SWAPW(&magic, 1) ||
  219. #endif
  220.             magic != OMAGIC) {
  221.             fprintf(stderr, "size: '%s' not an object module\n", f);
  222.             break;
  223.         }
  224.         read(fd, &h, sizeof(h));
  225.         showit(f, a.ar_name, &h);
  226.  
  227.         ts += h.tsize;
  228.         ds += h.dsize;
  229.         bs += h.bsize;
  230.  
  231.         if (lseek(fd, next, 0) < 0)
  232.             break;
  233.     }
  234.  
  235.     printf("%s(TOTAL): ", f);
  236.  
  237.     if (oflag)
  238.         printf("%lo + %lo + %lo = %lo\n", ts, ds, bs, ts+ds+bs);
  239.     else if (xflag)
  240.         printf("0x%lx + 0x%lx + 0x%lx = 0x%lx\n", ts, ds, bs, ts+ds+bs);
  241.     else /* decimal */
  242.         printf("%ld + %ld + %ld = %ld\n", ts, ds, bs, ts+ds+bs);
  243. }
  244.  
  245. showit(l, f, h)
  246. char    *l;
  247. char    *f;
  248. struct    hdr_l    *h;
  249. {
  250.     if (l != (char *) 0)
  251.         printf("%s(%.14s): ", l, f);
  252.     else
  253.         printf("%s: ", f);
  254.  
  255.     SWAPL(h, 6);
  256.     if (oflag)
  257.         printf("%lo + %lo + %lo = %lo\n",
  258.             h->tsize, h->dsize, h->bsize,
  259.             h->tsize + h->dsize + h->bsize);
  260.     else if (xflag)
  261.         printf("0x%lx + 0x%lx + 0x%lx = 0x%lx\n",
  262.             h->tsize, h->dsize, h->bsize,
  263.             h->tsize + h->dsize + h->bsize);
  264.     else /* decimal */
  265.         printf("%ld + %ld + %ld = %ld\n",
  266.             h->tsize, h->dsize, h->bsize,
  267.             h->tsize + h->dsize + h->bsize);
  268. }
  269.  
  270. #ifdef    MINIX
  271. mshowit(l, f, mh)
  272. char    *l;
  273. char    *f;
  274. struct    mhdr    *mh;
  275. {
  276.     if (l != (char *) 0)
  277.         printf("%s(%.14s): ", l, f);
  278.     else
  279.         printf("%s: ", f);
  280.  
  281.     if (oflag)
  282.         printf("%lo + %lo + %lo = %lo\n",
  283.             mh->tsize, mh->dsize, mh->bsize,
  284.             mh->tsize + mh->dsize + mh->bsize);
  285.     else if (xflag)
  286.         printf("0x%lx + 0x%lx + 0x%lx = 0x%lx\n",
  287.             mh->tsize, mh->dsize, mh->bsize,
  288.             mh->tsize + mh->dsize + mh->bsize);
  289.     else /* decimal */
  290.         printf("%ld + %ld + %ld = %ld\n",
  291.             mh->tsize, mh->dsize, mh->bsize,
  292.             mh->tsize + mh->dsize + mh->bsize);
  293. }
  294. #endif
  295.  
  296. #ifdef UNIXHOST
  297. long
  298. crossl(cp)
  299. char *cp;
  300. {
  301.     union {
  302.         long l;
  303.         char c[4];
  304.     } u;
  305.  
  306. #ifdef LITTLE_ENDIAN
  307.     u.c[0] = cp[3];
  308.     u.c[1] = cp[2];
  309.     u.c[2] = cp[1];
  310.     u.c[3] = cp[0];
  311. #else
  312.     u.c[0] = cp[0];
  313.     u.c[1] = cp[1];
  314.     u.c[2] = cp[2];
  315.     u.c[3] = cp[3];
  316. #endif
  317.     return u.l;
  318. }
  319.  
  320. swapw(cp, n)
  321. char *cp;
  322. {
  323. #ifdef LITTLE_ENDIAN
  324.     char t;
  325.  
  326.     while (n--) {
  327.         t = cp[1];
  328.         cp[1] = cp[0];
  329.         cp[0] = t;
  330.         cp += 2;
  331.     }
  332. #endif
  333.     return 0;
  334. }
  335.  
  336. swapl(cp, n)
  337. char *cp;
  338. {
  339. #ifdef LITTLE_ENDIAN
  340.     char t;
  341.  
  342.     while (n--) {
  343.         t = cp[3];
  344.         cp[3] = cp[0];
  345.         cp[0] = t;
  346.  
  347.         t = cp[2];
  348.         cp[2] = cp[1];
  349.         cp[1] = t;
  350.         cp += 4;
  351.     }
  352. #endif
  353.     return 0;
  354. }
  355.  
  356. #endif
  357.