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