xref: /openbsd-src/usr.sbin/crunchgen/crunched_main.c (revision 3b318c57c77b9e6fc6629145db8fd65fa564b684)
1*3b318c57Sderaadt /* $OpenBSD: crunched_main.c,v 1.7 2017/07/27 15:12:30 deraadt Exp $	 */
2cbf5262eSderaadt 
3cbf5262eSderaadt /*
4cbf5262eSderaadt  * Copyright (c) 1994 University of Maryland
5cbf5262eSderaadt  * All Rights Reserved.
6cbf5262eSderaadt  *
7cbf5262eSderaadt  * Permission to use, copy, modify, distribute, and sell this software and its
8cbf5262eSderaadt  * documentation for any purpose is hereby granted without fee, provided that
9cbf5262eSderaadt  * the above copyright notice appear in all copies and that both that
10cbf5262eSderaadt  * copyright notice and this permission notice appear in supporting
11cbf5262eSderaadt  * documentation, and that the name of U.M. not be used in advertising or
12cbf5262eSderaadt  * publicity pertaining to distribution of the software without specific,
13cbf5262eSderaadt  * written prior permission.  U.M. makes no representations about the
14cbf5262eSderaadt  * suitability of this software for any purpose.  It is provided "as is"
15cbf5262eSderaadt  * without express or implied warranty.
16cbf5262eSderaadt  *
17cbf5262eSderaadt  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18cbf5262eSderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19cbf5262eSderaadt  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20cbf5262eSderaadt  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21cbf5262eSderaadt  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22cbf5262eSderaadt  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23cbf5262eSderaadt  *
24cbf5262eSderaadt  * Author: James da Silva, Systems Design and Analysis Group
25cbf5262eSderaadt  *			   Computer Science Department
26cbf5262eSderaadt  *			   University of Maryland at College Park
27cbf5262eSderaadt  */
28cbf5262eSderaadt /*
29cbf5262eSderaadt  * crunched_main.c - main program for crunched binaries, it branches to a
30cbf5262eSderaadt  *	particular subprogram based on the value of argv[0].  Also included
31cbf5262eSderaadt  *	is a little program invoked when the crunched binary is called via
32cbf5262eSderaadt  *	its EXECNAME.  This one prints out the list of compiled-in binaries,
33cbf5262eSderaadt  *	or calls one of them based on argv[1].   This allows the testing of
34cbf5262eSderaadt  *	the crunched binary without creating all the links.
35cbf5262eSderaadt  */
36cbf5262eSderaadt #include <stdio.h>
37cbf5262eSderaadt #include <string.h>
38c88282a7Sderaadt #include <stdlib.h>
39cbf5262eSderaadt 
40cbf5262eSderaadt struct stub {
41cbf5262eSderaadt 	char	*name;
42cbf5262eSderaadt 	int	(*f)();
43cbf5262eSderaadt };
44cbf5262eSderaadt 
45cbf5262eSderaadt extern struct stub entry_points[];
46cbf5262eSderaadt 
47*3b318c57Sderaadt void	crunched_usage(void);
48*3b318c57Sderaadt 
49e6368c0eSguenther static int
crunched_main2(char * progname,int argc,char ** argv,char ** envp)50e6368c0eSguenther crunched_main2(char *progname, int argc, char **argv, char **envp)
51cbf5262eSderaadt {
52cbf5262eSderaadt 	struct stub	*ep;
53cbf5262eSderaadt 
54e6368c0eSguenther 	if (progname == NULL || *progname == '\0')
55cbf5262eSderaadt 		crunched_usage();
56cbf5262eSderaadt 
57cbf5262eSderaadt 	for (ep = entry_points; ep->name != NULL; ep++)
58e6368c0eSguenther 		if (!strcmp(progname, ep->name))
59cbf5262eSderaadt 			break;
60cbf5262eSderaadt 
61cbf5262eSderaadt 	if (ep->name)
62cbf5262eSderaadt 		return ep->f(argc, argv, envp);
63e6368c0eSguenther 	fprintf(stderr, "%s: %s not compiled in\n", EXECNAME, progname);
64cbf5262eSderaadt 	crunched_usage();
65*3b318c57Sderaadt 	return (1);
66cbf5262eSderaadt }
67cbf5262eSderaadt 
68cbf5262eSderaadt int
main(int argc,char * argv[],char ** envp)69e6368c0eSguenther main(int argc, char *argv[], char **envp)
70e6368c0eSguenther {
71e6368c0eSguenther 	extern char	*__progname;
72e6368c0eSguenther 
73e6368c0eSguenther 	return crunched_main2(__progname, argc, argv, envp);
74e6368c0eSguenther }
75e6368c0eSguenther 
76e6368c0eSguenther int
crunched_main(int argc,char ** argv,char ** envp)77cbf5262eSderaadt crunched_main(int argc, char **argv, char **envp)
78cbf5262eSderaadt {
79cbf5262eSderaadt 	if (argc <= 1)
80cbf5262eSderaadt 		crunched_usage();
81e6368c0eSguenther 	return crunched_main2(argv[1], argc-1, argv+1, envp);
82cbf5262eSderaadt }
83cbf5262eSderaadt 
84*3b318c57Sderaadt void
crunched_usage(void)85bb2262a1Sderaadt crunched_usage(void)
86cbf5262eSderaadt {
8731c4039aSderaadt 	int		columns = 0, len;
88cbf5262eSderaadt 	struct stub	*ep;
89cbf5262eSderaadt 
90cbf5262eSderaadt 	fprintf(stderr,
91cbf5262eSderaadt 	    "Usage: %s <prog> <args> ..., where <prog> is one of:\n",
92cbf5262eSderaadt 	    EXECNAME);
93cbf5262eSderaadt 	for (ep = entry_points; ep->name != NULL; ep++) {
94cbf5262eSderaadt 		len = strlen(ep->name) + 1;
95cbf5262eSderaadt 		if (columns + len < 80)
96cbf5262eSderaadt 			columns += len;
97cbf5262eSderaadt 		else {
98cbf5262eSderaadt 			fprintf(stderr, "\n");
99cbf5262eSderaadt 			columns = len;
100cbf5262eSderaadt 		}
101cbf5262eSderaadt 		fprintf(stderr, " %s", ep->name);
102cbf5262eSderaadt 	}
103cbf5262eSderaadt 	fprintf(stderr, "\n");
104cbf5262eSderaadt 	exit(1);
105cbf5262eSderaadt }
106