brandelf.c revision 18962
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$
29 */
30
31#include <stdlib.h>
32#include <stdio.h>
33#include <fcntl.h>
34#include <sys/imgact_elf.h>
35
36int usage();
37
38main(int argc, char **argv)
39{
40	extern char *optarg;
41	extern int optind;
42
43	char type[10] = "FreeBSD";
44	int ch, change = 0, verbose = 0;
45
46	while ((ch = getopt(argc, argv, "t:v")) != EOF)
47		switch (ch) {
48		case 'v':
49			verbose = 1;
50			break;
51		case 't':
52			change = 1;
53			strcpy(type, optarg);
54			break;
55		default:
56			usage();
57	}
58	argc -= optind;
59	argv += optind;
60	if (!argc) {
61		fprintf(stderr, "No file(s) specified.\n");
62		exit(1);
63	}
64	while (argc) {
65		int fd;
66		char buffer[EI_NINDENT];
67		char string[(EI_NINDENT-EI_SPARE)+1];
68
69		if ((fd = open(argv[0], O_RDWR, 0)) < 0) {
70			fprintf(stderr, "No such file %s.\n", argv[0]);
71			goto fail;
72
73		}
74		if (read(fd, buffer, EI_NINDENT) < EI_NINDENT) {
75			fprintf(stderr, "File '%s' too short.\n", argv[0]);
76			goto fail;
77		}
78		if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
79		    buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
80			fprintf(stderr, "File '%s' is not ELF format.\n",
81				argv[0]);
82			goto fail;
83		}
84		if (!change) {
85			bzero(string, sizeof(string));
86			strncpy(string, &buffer[EI_SPARE], EI_NINDENT-EI_SPARE);
87			if (strlen(string)) {
88				fprintf(stdout, "File '%s' is of brand '%s'.\n",
89					argv[0], string);
90			}
91			else
92				fprintf(stdout, "File '%s' has no branding.\n",
93					argv[0]);
94		}
95		else {
96			strncpy(&buffer[EI_SPARE], type, EI_NINDENT-EI_SPARE);
97			lseek(fd, 0, SEEK_SET);
98			if (write(fd, buffer, EI_NINDENT) != EI_NINDENT) {
99				fprintf(stderr, "Error writing %s\n", argv[0]);
100				goto fail;
101			}
102		}
103fail:
104		argc--;
105		argv++;
106	}
107}
108
109int
110usage()
111{
112	fprintf(stderr, "Usage: brandelf [-t string] file ...\n");
113}
114