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