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