brandelf.c revision 26837
118962Ssos/*- 218962Ssos * Copyright (c) 1996 S�ren Schmidt 318962Ssos * All rights reserved. 418962Ssos * 518962Ssos * Redistribution and use in source and binary forms, with or without 618962Ssos * modification, are permitted provided that the following conditions 718962Ssos * are met: 818962Ssos * 1. Redistributions of source code must retain the above copyright 918962Ssos * notice, this list of conditions and the following disclaimer 1018962Ssos * in this position and unchanged. 1118962Ssos * 2. Redistributions in binary form must reproduce the above copyright 1218962Ssos * notice, this list of conditions and the following disclaimer in the 1318962Ssos * documentation and/or other materials provided with the distribution. 1418962Ssos * 3. The name of the author may not be used to endorse or promote products 1518962Ssos * derived from this software withough specific prior written permission 1618962Ssos * 1718962Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1818962Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1918962Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2018962Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2118962Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2218962Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2318962Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2418962Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2518962Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2618962Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2718962Ssos * 2826837Scharnier * $Id: brandelf.c,v 1.6 1997/05/21 23:07:17 jdp Exp $ 2918962Ssos */ 3018962Ssos 3125984Sjdp#include <elf.h> 3225984Sjdp#include <fcntl.h> 3325984Sjdp#include <stdio.h> 3418962Ssos#include <stdlib.h> 3522499Sjoerg#include <string.h> 3622499Sjoerg#include <unistd.h> 3726837Scharnier#include <err.h> 3818962Ssos 3926837Scharnierstatic void usage __P((void)); 4018962Ssos 4122499Sjoergint 4218962Ssosmain(int argc, char **argv) 4318962Ssos{ 4418962Ssos 4522499Sjoerg const char *type = "FreeBSD"; 4622499Sjoerg int retval = 0; 4718962Ssos int ch, change = 0, verbose = 0; 4818962Ssos 4924360Simp while ((ch = getopt(argc, argv, "t:v")) != -1) 5018962Ssos switch (ch) { 5118962Ssos case 'v': 5218962Ssos verbose = 1; 5318962Ssos break; 5418962Ssos case 't': 5518962Ssos change = 1; 5622499Sjoerg type = optarg; 5718962Ssos break; 5818962Ssos default: 5918962Ssos usage(); 6018962Ssos } 6118962Ssos argc -= optind; 6218962Ssos argv += optind; 6326837Scharnier if (!argc) 6426837Scharnier errx(1, "no file(s) specified"); 6518962Ssos while (argc) { 6618962Ssos int fd; 6725984Sjdp char buffer[EI_NIDENT]; 6825984Sjdp char string[(EI_NIDENT-EI_BRAND)+1]; 6918962Ssos 7018962Ssos if ((fd = open(argv[0], O_RDWR, 0)) < 0) { 7126837Scharnier warnx("no such file %s", argv[0]); 7222499Sjoerg retval = 1; 7318962Ssos goto fail; 7418962Ssos 7518962Ssos } 7625984Sjdp if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) { 7726837Scharnier warnx("file '%s' too short", argv[0]); 7822499Sjoerg retval = 1; 7918962Ssos goto fail; 8018962Ssos } 8118962Ssos if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 || 8218962Ssos buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) { 8326837Scharnier warnx("file '%s' is not ELF format", argv[0]); 8422499Sjoerg retval = 1; 8518962Ssos goto fail; 8618962Ssos } 8718962Ssos if (!change) { 8818962Ssos bzero(string, sizeof(string)); 8925984Sjdp strncpy(string, &buffer[EI_BRAND], EI_NIDENT-EI_BRAND); 9018962Ssos if (strlen(string)) { 9118962Ssos fprintf(stdout, "File '%s' is of brand '%s'.\n", 9218962Ssos argv[0], string); 9318962Ssos } 9418962Ssos else 9518962Ssos fprintf(stdout, "File '%s' has no branding.\n", 9618962Ssos argv[0]); 9718962Ssos } 9818962Ssos else { 9925984Sjdp strncpy(&buffer[EI_BRAND], type, EI_NIDENT-EI_BRAND); 10018962Ssos lseek(fd, 0, SEEK_SET); 10125984Sjdp if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) { 10226837Scharnier warnx("error writing %s", argv[0]); 10326837Scharnier retval = 1; 10418962Ssos goto fail; 10518962Ssos } 10618962Ssos } 10718962Ssosfail: 10818962Ssos argc--; 10918962Ssos argv++; 11018962Ssos } 11122499Sjoerg 11222499Sjoerg return retval; 11318962Ssos} 11418962Ssos 11526837Scharnierstatic void 11626837Scharnierusage() 11718962Ssos{ 11826837Scharnier fprintf(stderr, "usage: brandelf [-t string] file ...\n"); 11922499Sjoerg exit(1); 12018962Ssos} 121