xref: /original-bsd/usr.bin/size/size.c (revision 95ecee29)
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.2 (Berkeley) 12/09/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 		(void)close(fd);
73 		err("%s: not in a.out format", name);
74 		return (1);
75 	}
76 	(void)close(fd);
77 
78 	if (first) {
79 		first = 0;
80 		(void)printf("text\tdata\tbss\tdec\thex\n");
81 	}
82 	total = head.a_text + head.a_data + head.a_bss;
83 	(void)printf("%lu\t%lu\t%lu\t%lu\t%lx", head.a_text, head.a_data,
84 	    head.a_bss, total, total);
85 	if (count > 1)
86 		(void)printf("\t%s", name);
87 	(void)printf("\n");
88 	return (0);
89 }
90 
91 void
92 usage()
93 {
94 	(void)fprintf(stderr, "usage: size [file ...]\n");
95 	exit(1);
96 }
97 
98 #if __STDC__
99 #include <stdarg.h>
100 #else
101 #include <varargs.h>
102 #endif
103 
104 void
105 #if __STDC__
106 err(const char *fmt, ...)
107 #else
108 err(fmt, va_alist)
109 	char *fmt;
110         va_dcl
111 #endif
112 {
113 	va_list ap;
114 #if __STDC__
115 	va_start(ap, fmt);
116 #else
117 	va_start(ap);
118 #endif
119 	(void)fprintf(stderr, "size: ");
120 	(void)vfprintf(stderr, fmt, ap);
121 	va_end(ap);
122 	(void)fprintf(stderr, "\n");
123 }
124