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