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