2 * Copyright (c) 1998, Peter Wemm <peter@netplex.com.au>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/usr.bin/objformat/objformat.c,v 1.6 1998/10/24 02:01:30 jdp Exp $
27 * $DragonFly: src/usr.bin/objformat/objformat.c,v 1.7 2004/01/29 01:39:41 dillon Exp $
31 #include <objformat.h>
42 #define arysize(ary) (sizeof(ary)/sizeof((ary)[0]))
49 static struct command commands[] = {
52 {"c++filt", COMPILER},
59 {"addr2line", BINUTILS2},
66 {"objcopy", BINUTILS2},
67 {"objdump", BINUTILS2},
68 {"ranlib", BINUTILS2},
70 {"strings", BINUTILS2},
72 {"objformat", OBJFORMAT}
76 main(int argc, char **argv)
81 char *cmd, *newcmd = NULL;
84 const char *env_name = NULL;
85 const char *env_value;
86 const char *env_default = NULL;
87 const char *base_path = NULL;
88 int use_objformat = 0;
90 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
91 errx(1, "Invalid object format");
94 * Get the last path elemenet of the program name being executed
96 cmd = strrchr(argv[0], '/');
102 for (cmds = commands; cmds < &commands[arysize(commands)]; ++cmds) {
103 if (strcmp(cmd, cmds->cmd) == 0)
107 switch (cmds->type) {
110 env_default = "gcc2";
111 base_path = "/usr/bin";
118 env_default = "binutils212";
119 env_name = "BINUTILSVER";
120 base_path = "/usr/libexec";
121 if ((ccver = getenv("CCVER")) != NULL) {
122 if (strcmp(ccver, "gcc3") == 0)
123 ;/* change binutils env_default here XXX */
130 err(1, "unknown command type");
136 * The objformat command itself doesn't need another exec
138 if (cmds->type == OBJFORMAT) {
140 fprintf(stderr, "Usage: objformat\n");
144 printf("%s\n", objformat);
149 * make buildworld glue and CCVER overrides.
151 objformat_path = getenv("OBJFORMAT_PATH");
152 if (objformat_path == NULL)
155 if ((env_value = getenv(env_name)) == NULL)
156 env_value = env_default;
158 path = strdup(objformat_path);
160 setenv("OBJFORMAT", objformat, 1);
163 * objformat_path could be sequence of colon-separated paths.
165 while ((chunk = strsep(&path, ":")) != NULL) {
166 if (newcmd != NULL) {
171 asprintf(&newcmd, "%s%s/%s/%s/%s",
172 chunk, base_path, env_value, objformat, cmd);
174 asprintf(&newcmd, "%s%s/%s/%s",
175 chunk, base_path, env_value, cmd);
178 err(1, "cannot allocate memory");
184 err(1, "in path [%s]%s/%s/%s/%s",
185 objformat_path, base_path, env_value, objformat, cmd);
187 err(1, "in path [%s]%s/%s/%s",
188 objformat_path, base_path, env_value, cmd);