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