brandelf.c revision 25984
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 *
2825984Sjdp *  $Id: brandelf.c,v 1.5 1997/03/29 04:28:17 imp 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>
3718962Ssos
3822499Sjoergint usage(void);
3918962Ssos
4022499Sjoergint
4118962Ssosmain(int argc, char **argv)
4218962Ssos{
4318962Ssos
4422499Sjoerg	const char *type = "FreeBSD";
4522499Sjoerg	int retval = 0;
4618962Ssos	int ch, change = 0, verbose = 0;
4718962Ssos
4824360Simp	while ((ch = getopt(argc, argv, "t:v")) != -1)
4918962Ssos		switch (ch) {
5018962Ssos		case 'v':
5118962Ssos			verbose = 1;
5218962Ssos			break;
5318962Ssos		case 't':
5418962Ssos			change = 1;
5522499Sjoerg			type = optarg;
5618962Ssos			break;
5718962Ssos		default:
5818962Ssos			usage();
5918962Ssos	}
6018962Ssos	argc -= optind;
6118962Ssos	argv += optind;
6218962Ssos	if (!argc) {
6318962Ssos		fprintf(stderr, "No file(s) specified.\n");
6418962Ssos		exit(1);
6518962Ssos	}
6618962Ssos	while (argc) {
6718962Ssos		int fd;
6825984Sjdp		char buffer[EI_NIDENT];
6925984Sjdp		char string[(EI_NIDENT-EI_BRAND)+1];
7018962Ssos
7118962Ssos		if ((fd = open(argv[0], O_RDWR, 0)) < 0) {
7218962Ssos			fprintf(stderr, "No such file %s.\n", argv[0]);
7322499Sjoerg			retval = 1;
7418962Ssos			goto fail;
7518962Ssos
7618962Ssos		}
7725984Sjdp		if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) {
7818962Ssos			fprintf(stderr, "File '%s' too short.\n", argv[0]);
7922499Sjoerg			retval = 1;
8018962Ssos			goto fail;
8118962Ssos		}
8218962Ssos		if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
8318962Ssos		    buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
8418962Ssos			fprintf(stderr, "File '%s' is not ELF format.\n",
8518962Ssos				argv[0]);
8622499Sjoerg			retval = 1;
8718962Ssos			goto fail;
8818962Ssos		}
8918962Ssos		if (!change) {
9018962Ssos			bzero(string, sizeof(string));
9125984Sjdp			strncpy(string, &buffer[EI_BRAND], EI_NIDENT-EI_BRAND);
9218962Ssos			if (strlen(string)) {
9318962Ssos				fprintf(stdout, "File '%s' is of brand '%s'.\n",
9418962Ssos					argv[0], string);
9518962Ssos			}
9618962Ssos			else
9718962Ssos				fprintf(stdout, "File '%s' has no branding.\n",
9818962Ssos					argv[0]);
9918962Ssos		}
10018962Ssos		else {
10125984Sjdp			strncpy(&buffer[EI_BRAND], type, EI_NIDENT-EI_BRAND);
10218962Ssos			lseek(fd, 0, SEEK_SET);
10325984Sjdp			if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
10418962Ssos				fprintf(stderr, "Error writing %s\n", argv[0]);
10522499Sjoerg			retval = 1;
10618962Ssos				goto fail;
10718962Ssos			}
10818962Ssos		}
10918962Ssosfail:
11018962Ssos		argc--;
11118962Ssos		argv++;
11218962Ssos	}
11322499Sjoerg
11422499Sjoerg	return retval;
11518962Ssos}
11618962Ssos
11718962Ssosint
11822499Sjoergusage(void)
11918962Ssos{
12018962Ssos	fprintf(stderr, "Usage: brandelf [-t string] file ...\n");
12122499Sjoerg	exit(1);
12218962Ssos}
123