1 /* $OpenBSD: crunched_main.c,v 1.1 2008/08/22 15:18:55 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 39 struct stub { 40 char *name; 41 int (*f)(); 42 }; 43 44 extern struct stub entry_points[]; 45 46 int 47 main(int argc, char *argv[], char **envp) 48 { 49 char *slash, *basename; 50 struct stub *ep; 51 52 if (argv[0] == NULL || *argv[0] == '\0') 53 crunched_usage(); 54 55 slash = strrchr(argv[0], '/'); 56 basename = slash ? slash + 1 : argv[0]; 57 58 for (ep = entry_points; ep->name != NULL; ep++) 59 if (!strcmp(basename, ep->name)) 60 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 int 71 crunched_main(int argc, char **argv, char **envp) 72 { 73 struct stub *ep; 74 int columns, len; 75 76 if (argc <= 1) 77 crunched_usage(); 78 79 return main(--argc, ++argv, envp); 80 } 81 82 int 83 crunched_usage() 84 { 85 int columns, len; 86 struct stub *ep; 87 88 fprintf(stderr, 89 "Usage: %s <prog> <args> ..., where <prog> is one of:\n", 90 EXECNAME); 91 columns = 0; 92 for (ep = entry_points; ep->name != NULL; ep++) { 93 len = strlen(ep->name) + 1; 94 if (columns + len < 80) 95 columns += len; 96 else { 97 fprintf(stderr, "\n"); 98 columns = len; 99 } 100 fprintf(stderr, " %s", ep->name); 101 } 102 fprintf(stderr, "\n"); 103 exit(1); 104 } 105