1 /* 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char copyright[] = 10 "@(#) Copyright (c) 1988, 1993\n\ 11 The Regents of the University of California. All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)size.c 8.1 (Berkeley) 06/06/93"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/file.h> 20 #include <errno.h> 21 #include <a.out.h> 22 #include <unistd.h> 23 #include <stdlib.h> 24 #include <stdio.h> 25 #include <string.h> 26 27 void err __P((const char *, ...)); 28 int show __P((int, char *)); 29 void usage __P((void)); 30 31 int 32 main(argc, argv) 33 int argc; 34 char *argv[]; 35 { 36 int ch, eval; 37 38 while ((ch = getopt(argc, argv, "")) != EOF) 39 switch(ch) { 40 case '?': 41 default: 42 usage(); 43 } 44 argc -= optind; 45 argv += optind; 46 47 eval = 0; 48 if (*argv) 49 do { 50 eval |= show(argc, *argv); 51 } while (*++argv); 52 else 53 eval |= show(1, "a.out"); 54 exit(eval); 55 } 56 57 int 58 show(count, name) 59 int count; 60 char *name; 61 { 62 static int first = 1; 63 struct exec head; 64 u_long total; 65 int fd; 66 67 if ((fd = open(name, O_RDONLY, 0)) < 0) { 68 err("%s: %s", name, strerror(errno)); 69 return (1); 70 } 71 if (read(fd, &head, sizeof(head)) != sizeof(head) || N_BADMAG(head)) { 72 err("%s: not in a.out format", name); 73 return (1); 74 } 75 (void)close(fd); 76 77 if (first) { 78 first = 0; 79 (void)printf("text\tdata\tbss\tdec\thex\n"); 80 } 81 total = head.a_text + head.a_data + head.a_bss; 82 (void)printf("%lu\t%lu\t%lu\t%lu\t%lx", head.a_text, head.a_data, 83 head.a_bss, total, total); 84 if (count > 1) 85 (void)printf("\t%s", name); 86 (void)printf("\n"); 87 return (0); 88 } 89 90 void 91 usage() 92 { 93 (void)fprintf(stderr, "usage: size [file ...]\n"); 94 exit(1); 95 } 96 97 #if __STDC__ 98 #include <stdarg.h> 99 #else 100 #include <varargs.h> 101 #endif 102 103 void 104 #if __STDC__ 105 err(const char *fmt, ...) 106 #else 107 err(fmt, va_alist) 108 char *fmt; 109 va_dcl 110 #endif 111 { 112 va_list ap; 113 #if __STDC__ 114 va_start(ap, fmt); 115 #else 116 va_start(ap); 117 #endif 118 (void)fprintf(stderr, "size: "); 119 (void)vfprintf(stderr, fmt, ap); 120 va_end(ap); 121 (void)fprintf(stderr, "\n"); 122 } 123