brandelf.c revision 25984
1173412Skevlo/*- 278344Sobrien * Copyright (c) 1996 S�ren Schmidt 378344Sobrien * All rights reserved. 478344Sobrien * 578344Sobrien * Redistribution and use in source and binary forms, with or without 678344Sobrien * modification, are permitted provided that the following conditions 778344Sobrien * are met: 878344Sobrien * 1. Redistributions of source code must retain the above copyright 978344Sobrien * notice, this list of conditions and the following disclaimer 1078344Sobrien * in this position and unchanged. 1178344Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1278344Sobrien * notice, this list of conditions and the following disclaimer in the 1378344Sobrien * documentation and/or other materials provided with the distribution. 1478344Sobrien * 3. The name of the author may not be used to endorse or promote products 1578344Sobrien * derived from this software withough specific prior written permission 1678344Sobrien * 1778344Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1878344Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1978344Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2078344Sobrien * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2178344Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2278344Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2378344Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2478344Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2578344Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2678344Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2778344Sobrien * 2878344Sobrien * $Id: brandelf.c,v 1.5 1997/03/29 04:28:17 imp Exp $ 2978344Sobrien */ 3078344Sobrien 3178344Sobrien#include <elf.h> 3278344Sobrien#include <fcntl.h> 3378344Sobrien#include <stdio.h> 3478344Sobrien#include <stdlib.h> 3578344Sobrien#include <string.h> 3678344Sobrien#include <unistd.h> 3778344Sobrien 3878344Sobrienint usage(void); 3978344Sobrien 4078344Sobrienint 4178344Sobrienmain(int argc, char **argv) 4278344Sobrien{ 4378344Sobrien 4478344Sobrien const char *type = "FreeBSD"; 4578344Sobrien int retval = 0; 4678344Sobrien int ch, change = 0, verbose = 0; 4778344Sobrien 4878344Sobrien while ((ch = getopt(argc, argv, "t:v")) != -1) 4978344Sobrien switch (ch) { 5078344Sobrien case 'v': 5178344Sobrien verbose = 1; 5278344Sobrien break; 5378344Sobrien case 't': 54173412Skevlo change = 1; 5578344Sobrien type = optarg; 5678344Sobrien break; 5778344Sobrien default: 5878344Sobrien usage(); 5978344Sobrien } 6078344Sobrien argc -= optind; 61201227Sed argv += optind; 6278344Sobrien if (!argc) { 6378344Sobrien fprintf(stderr, "No file(s) specified.\n"); 6478344Sobrien exit(1); 6578344Sobrien } 6678344Sobrien while (argc) { 6778344Sobrien int fd; 6878344Sobrien char buffer[EI_NIDENT]; 6978344Sobrien char string[(EI_NIDENT-EI_BRAND)+1]; 7078344Sobrien 71201227Sed if ((fd = open(argv[0], O_RDWR, 0)) < 0) { 7278344Sobrien fprintf(stderr, "No such file %s.\n", argv[0]); 7378344Sobrien retval = 1; 7478344Sobrien goto fail; 7578344Sobrien 7678344Sobrien } 7778344Sobrien if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) { 7878344Sobrien fprintf(stderr, "File '%s' too short.\n", argv[0]); 7978344Sobrien retval = 1; 8078344Sobrien goto fail; 8178344Sobrien } 8278344Sobrien if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 || 8378344Sobrien buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) { 8478344Sobrien fprintf(stderr, "File '%s' is not ELF format.\n", 85201227Sed argv[0]); 8678344Sobrien retval = 1; 8778344Sobrien goto fail; 8878344Sobrien } 8978344Sobrien if (!change) { 9078344Sobrien bzero(string, sizeof(string)); 9178344Sobrien strncpy(string, &buffer[EI_BRAND], EI_NIDENT-EI_BRAND); 9278344Sobrien if (strlen(string)) { 9378344Sobrien fprintf(stdout, "File '%s' is of brand '%s'.\n", 9478344Sobrien argv[0], string); 9578344Sobrien } 9678344Sobrien else 9778344Sobrien fprintf(stdout, "File '%s' has no branding.\n", 9878344Sobrien argv[0]); 99201227Sed } 10078344Sobrien else { 10178344Sobrien strncpy(&buffer[EI_BRAND], type, EI_NIDENT-EI_BRAND); 10278344Sobrien lseek(fd, 0, SEEK_SET); 10378344Sobrien if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) { 10478344Sobrien fprintf(stderr, "Error writing %s\n", argv[0]); 10578344Sobrien retval = 1; 10678344Sobrien goto fail; 10778344Sobrien } 10878344Sobrien } 10978344Sobrienfail: 11078344Sobrien argc--; 111201227Sed argv++; 11278344Sobrien } 11378344Sobrien 11478344Sobrien return retval; 11578344Sobrien} 11678344Sobrien 11778344Sobrienint 11878344Sobrienusage(void) 119{ 120 fprintf(stderr, "Usage: brandelf [-t string] file ...\n"); 121 exit(1); 122} 123