1 /*- 2 * Copyright (c) 1996 S�ren Schmidt 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software withough specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id: brandelf.c,v 1.7 1997/06/23 06:47:12 charnier Exp $ 29 */ 30 31 #include <elf.h> 32 #include <fcntl.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 #include <err.h> 38 39 static void usage __P((void)); 40 41 int 42 main(int argc, char **argv) 43 { 44 45 const char *type = "FreeBSD"; 46 int retval = 0; 47 int ch, change = 0, verbose = 0; 48 49 while ((ch = getopt(argc, argv, "t:v")) != -1) 50 switch (ch) { 51 case 'v': 52 verbose = 1; 53 break; 54 case 't': 55 change = 1; 56 type = optarg; 57 break; 58 default: 59 usage(); 60 } 61 argc -= optind; 62 argv += optind; 63 if (!argc) 64 errx(1, "no file(s) specified"); 65 while (argc) { 66 int fd; 67 char buffer[EI_NIDENT]; 68 char string[(EI_NIDENT-EI_BRAND)+1]; 69 70 if ((fd = open(argv[0], change? O_RDWR: O_RDONLY, 0)) < 0) { 71 warn("error opening file %s", argv[0]); 72 retval = 1; 73 goto fail; 74 75 } 76 if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) { 77 warnx("file '%s' too short", argv[0]); 78 retval = 1; 79 goto fail; 80 } 81 if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 || 82 buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) { 83 warnx("file '%s' is not ELF format", argv[0]); 84 retval = 1; 85 goto fail; 86 } 87 if (!change) { 88 bzero(string, sizeof(string)); 89 strncpy(string, &buffer[EI_BRAND], EI_NIDENT-EI_BRAND); 90 if (strlen(string)) { 91 fprintf(stdout, "File '%s' is of brand '%s'.\n", 92 argv[0], string); 93 } 94 else 95 fprintf(stdout, "File '%s' has no branding.\n", 96 argv[0]); 97 } 98 else { 99 strncpy(&buffer[EI_BRAND], type, EI_NIDENT-EI_BRAND); 100 lseek(fd, 0, SEEK_SET); 101 if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) { 102 warnx("error writing %s", argv[0]); 103 retval = 1; 104 goto fail; 105 } 106 } 107 fail: 108 argc--; 109 argv++; 110 } 111 112 return retval; 113 } 114 115 static void 116 usage() 117 { 118 fprintf(stderr, "usage: brandelf [-t string] file ...\n"); 119 exit(1); 120 } 121