diff options
| author | John Polstra <jdp@FreeBSD.org> | 1998-10-21 15:13:16 +0000 |
|---|---|---|
| committer | John Polstra <jdp@FreeBSD.org> | 1998-10-21 15:13:16 +0000 |
| commit | 778a593eb23237f2be30eab0a3280d908fa2723a (patch) | |
| tree | e8121780f8a7c9656adda7b365f16db3dd47faf4 /usr.bin/objformat/objformat.c | |
| parent | 300ee8246ec26d7877f4b3af1663602c4d5cd418 (diff) | |
Notes
Diffstat (limited to 'usr.bin/objformat/objformat.c')
| -rw-r--r-- | usr.bin/objformat/objformat.c | 120 |
1 files changed, 12 insertions, 108 deletions
diff --git a/usr.bin/objformat/objformat.c b/usr.bin/objformat/objformat.c index 827af6eda1e9..32ed3859081f 100644 --- a/usr.bin/objformat/objformat.c +++ b/usr.bin/objformat/objformat.c @@ -26,112 +26,32 @@ * $FreeBSD$ */ -#include <sys/types.h> - +#include <err.h> +#include <objformat.h> #include <stdio.h> #include <stdlib.h> -#include <unistd.h> #include <string.h> +#include <unistd.h> -#ifdef FREEBSD_ELF -int objformat_aout = 0; -#else -int objformat_aout = 1; -#endif - -void -getobjfmt(void) -{ - char *env; - int i; - - /* first hint is /etc/objformat */ - FILE *fp = fopen("/etc/objformat", "r"); - if (fp) { - char buf[1024]; - buf[1023] = '\0'; - while (fgets(buf, sizeof(buf) - 1, fp) != NULL) { - i = strlen(buf); - if (buf[i - 1] == '\n') - buf[i - 1] = '\0'; - if (strcmp(buf, "OBJFORMAT=aout") == 0) - objformat_aout = 1; - else if (strcmp(buf, "OBJFORMAT=elf") == 0) - objformat_aout = 0; - else - fprintf(stderr, "Unrecognized line in /etc/objformat: %s\n", buf); - } - fclose(fp); - } - /* but the user $OBJFORMAT overrides system default */ - env = getenv("OBJFORMAT"); - if (env) { - if (strcmp(env, "aout") == 0) - objformat_aout = 1; - else if (strcmp(env, "elf") == 0) - objformat_aout = 0; - else - fprintf(stderr, "Unrecognized value of $OBJFORMAT: %s\n", env); - } -} - -void -scanargv(int *argc, char **argv, int strip) -{ - int i, j; - - for (i = 1; i < *argc; i++) { - if (strcmp (argv[i], "-aout") == 0) { - objformat_aout = 1; - continue; - } else if (strcmp (argv[i], "-elf") == 0) { - objformat_aout = 0; - continue; - } - } - - /* if just looking, return now */ - if (!strip) - return; - - /* otherwise, remove all traces of switches from argv */ - for (i = 1; i < *argc; i++) { - if (strcmp (argv[i], "-aout") == 0 || - strcmp (argv[i], "-elf") == 0) { - /* copy NULL at end of argv as well */ - for (j = i + 1; j <= *argc; j++) { - argv[j - 1] = argv[j]; - } - (*argc)--; - } - } -} - - -#ifdef MAIN int main(int argc, char **argv) { + char objformat[32]; char *path, *chunk; - char *postfix; char *cmd, *newcmd = NULL; char *objformat_path; - int i; + + if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1) + errx(1, "Invalid object format"); cmd = strrchr(argv[0], '/'); - if (cmd) + if (cmd != NULL) cmd++; else cmd = argv[0]; - getobjfmt(); - scanargv(&argc, argv, 1); - if (strcmp(cmd, "objformat") == 0) { - if (objformat_aout) - printf("aout\n"); - else - printf("elf\n"); + printf("%s\n", objformat); exit(0); } @@ -141,35 +61,19 @@ main(int argc, char **argv) objformat_path = "/usr/libexec"; path = strdup(objformat_path); - if (objformat_aout) { - putenv("OBJFORMAT=aout"); - postfix = "aout"; - } else { - putenv("OBJFORMAT=elf"); - postfix = "elf"; - } + setenv("OBJFORMAT", objformat, 1); while ((chunk = strsep(&path, ":")) != NULL) { if (newcmd != NULL) { free(newcmd); newcmd = NULL; } - asprintf(&newcmd, "%s/%s/%s", chunk, postfix, cmd); + asprintf(&newcmd, "%s/%s/%s", chunk, objformat, cmd); if (newcmd == NULL) err(1, "cannot allocate memory for new command"); - if (getenv("OBJFORMAT_DEBUG") != NULL) { - fprintf(stderr, "objformat: %s -> %s\n", cmd, newcmd); -#if 0 - for (i = 1; i < argc; i++) - fprintf(stderr, "argv[%d]: %s\n", i, argv[i]); -#endif - } - argv[0] = newcmd; execv(newcmd, argv); } - err(1, "could not exec %s/%s in %s", postfix, cmd, objformat_path); + err(1, "could not exec %s/%s in %s", objformat, cmd, objformat_path); } - -#endif |
