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