11722Sjkh/*
21722Sjkh * Copyright (c) 1994 University of Maryland
31722Sjkh * All Rights Reserved.
41722Sjkh *
51722Sjkh * Permission to use, copy, modify, distribute, and sell this software and its
61722Sjkh * documentation for any purpose is hereby granted without fee, provided that
71722Sjkh * the above copyright notice appear in all copies and that both that
81722Sjkh * copyright notice and this permission notice appear in supporting
91722Sjkh * documentation, and that the name of U.M. not be used in advertising or
101722Sjkh * publicity pertaining to distribution of the software without specific,
111722Sjkh * written prior permission.  U.M. makes no representations about the
121722Sjkh * suitability of this software for any purpose.  It is provided "as is"
131722Sjkh * without express or implied warranty.
141722Sjkh *
151722Sjkh * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
161722Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
171722Sjkh * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
181722Sjkh * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
191722Sjkh * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
201722Sjkh * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
211722Sjkh *
221722Sjkh * Author: James da Silva, Systems Design and Analysis Group
231722Sjkh *			   Computer Science Department
241722Sjkh *			   University of Maryland at College Park
251722Sjkh */
261722Sjkh/*
278857Srgrimes * crunched_main.c - main program for crunched binaries, it branches to a
281722Sjkh * 	particular subprogram based on the value of argv[0].  Also included
291722Sjkh *	is a little program invoked when the crunched binary is called via
301722Sjkh *	its EXECNAME.  This one prints out the list of compiled-in binaries,
311722Sjkh *	or calls one of them based on argv[1].   This allows the testing of
321722Sjkh *	the crunched binary without creating all the links.
331722Sjkh */
34237625Sobrien
35237625Sobrien#include <sys/cdefs.h>
36237625Sobrien__FBSDID("$FreeBSD$");
37237625Sobrien
381722Sjkh#include <stdio.h>
39173067Syar#include <stdlib.h>
401722Sjkh#include <string.h>
411722Sjkh
421722Sjkhstruct stub {
431722Sjkh    char *name;
441722Sjkh    int (*f)();
451722Sjkh};
461722Sjkh
47173065Syarextern char *__progname;
481722Sjkhextern struct stub entry_points[];
491722Sjkh
50237625Sobrienint
51237625Sobrienmain(int argc, char **argv, char **envp)
521722Sjkh{
531722Sjkh    char *slash, *basename;
541722Sjkh    struct stub *ep;
551722Sjkh
561722Sjkh    if(argv[0] == NULL || *argv[0] == '\0')
571722Sjkh	crunched_usage();
581722Sjkh
591722Sjkh    slash = strrchr(argv[0], '/');
601722Sjkh    basename = slash? slash+1 : argv[0];
611722Sjkh
621722Sjkh    for(ep=entry_points; ep->name != NULL; ep++)
631722Sjkh	if(!strcmp(basename, ep->name)) break;
641722Sjkh
651722Sjkh    if(ep->name)
66101341Sgshapiro	return ep->f(argc, argv, envp);
671722Sjkh    else {
681722Sjkh	fprintf(stderr, "%s: %s not compiled in\n", EXECNAME, basename);
691722Sjkh	crunched_usage();
701722Sjkh    }
711722Sjkh}
721722Sjkh
731722Sjkh
74237625Sobrienint
75237625Sobriencrunched_here(char *path)
768664Sphk{
778664Sphk    char *slash, *basename;
788664Sphk    struct stub *ep;
798664Sphk
808664Sphk    slash = strrchr(path, '/');
818664Sphk    basename = slash? slash+1 : path;
828664Sphk
838664Sphk    for(ep=entry_points; ep->name != NULL; ep++)
848664Sphk	if(!strcmp(basename, ep->name))
858664Sphk	    return 1;
868664Sphk    return 0;
878664Sphk}
888664Sphk
898664Sphk
90237625Sobrienint
91237625Sobriencrunched_main(int argc, char **argv, char **envp)
921722Sjkh{
93173065Syar    char *slash;
941722Sjkh    struct stub *ep;
951722Sjkh    int columns, len;
961722Sjkh
978857Srgrimes    if(argc <= 1)
981722Sjkh	crunched_usage();
991722Sjkh
100173065Syar    slash = strrchr(argv[1], '/');
101173065Syar    __progname = slash? slash+1 : argv[1];
102173065Syar
103101550Sgshapiro    return main(--argc, ++argv, envp);
1041722Sjkh}
1051722Sjkh
1061722Sjkh
107237625Sobrienint
108237625Sobriencrunched_usage()
1091722Sjkh{
1101722Sjkh    int columns, len;
1111722Sjkh    struct stub *ep;
1121722Sjkh
11329453Scharnier    fprintf(stderr, "usage: %s <prog> <args> ..., where <prog> is one of:\n",
1141722Sjkh	    EXECNAME);
1151722Sjkh    columns = 0;
1161722Sjkh    for(ep=entry_points; ep->name != NULL; ep++) {
1171722Sjkh	len = strlen(ep->name) + 1;
1181722Sjkh	if(columns+len < 80)
1191722Sjkh	    columns += len;
1201722Sjkh	else {
1211722Sjkh	    fprintf(stderr, "\n");
1221722Sjkh	    columns = len;
1231722Sjkh	}
1241722Sjkh	fprintf(stderr, " %s", ep->name);
1251722Sjkh    }
1261722Sjkh    fprintf(stderr, "\n");
1271722Sjkh    exit(1);
1281722Sjkh}
1291722Sjkh
1301722Sjkh/* end of crunched_main.c */
131