xref: /freebsd/usr.sbin/crunch/crunchgen/crunched_main.c (revision a5921bc3653e2e286715e6fe8d473ec0d02da38c)
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 /*
27  * crunched_main.c - main program for crunched binaries, it branches to a
28  * 	particular subprogram based on the value of argv[0].  Also included
29  *	is a little program invoked when the crunched binary is called via
30  *	its EXECNAME.  This one prints out the list of compiled-in binaries,
31  *	or calls one of them based on argv[1].   This allows the testing of
32  *	the crunched binary without creating all the links.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 struct stub {
43     char *name;
44     int (*f)();
45 };
46 
47 extern char *__progname;
48 extern struct stub entry_points[];
49 
50 int
51 main(int argc, char **argv, char **envp)
52 {
53     char *slash, *basename;
54     struct stub *ep;
55 
56     if(argv[0] == NULL || *argv[0] == '\0')
57 	crunched_usage();
58 
59     slash = strrchr(argv[0], '/');
60     basename = slash? slash+1 : argv[0];
61 
62     for(ep=entry_points; ep->name != NULL; ep++)
63 	if(!strcmp(basename, ep->name)) break;
64 
65     if(ep->name)
66 	return ep->f(argc, argv, envp);
67     else {
68 	fprintf(stderr, "%s: %s not compiled in\n", EXECNAME, basename);
69 	crunched_usage();
70     }
71 }
72 
73 
74 int
75 crunched_here(char *path)
76 {
77     char *slash, *basename;
78     struct stub *ep;
79 
80     slash = strrchr(path, '/');
81     basename = slash? slash+1 : path;
82 
83     for(ep=entry_points; ep->name != NULL; ep++)
84 	if(!strcmp(basename, ep->name))
85 	    return 1;
86     return 0;
87 }
88 
89 
90 int
91 crunched_main(int argc, char **argv, char **envp)
92 {
93     char *slash;
94     struct stub *ep;
95     int columns, len;
96 
97     if(argc <= 1)
98 	crunched_usage();
99 
100     slash = strrchr(argv[1], '/');
101     __progname = slash? slash+1 : argv[1];
102 
103     return main(--argc, ++argv, envp);
104 }
105 
106 
107 int
108 crunched_usage()
109 {
110     int columns, len;
111     struct stub *ep;
112 
113     fprintf(stderr, "usage: %s <prog> <args> ..., where <prog> is one of:\n",
114 	    EXECNAME);
115     columns = 0;
116     for(ep=entry_points; ep->name != NULL; ep++) {
117 	len = strlen(ep->name) + 1;
118 	if(columns+len < 80)
119 	    columns += len;
120 	else {
121 	    fprintf(stderr, "\n");
122 	    columns = len;
123 	}
124 	fprintf(stderr, " %s", ep->name);
125     }
126     fprintf(stderr, "\n");
127     exit(1);
128 }
129 
130 /* end of crunched_main.c */
131