1 /* bucomm.c -- Bin Utils COMmon code.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 /* We might put this in a library someday so it could be dynamically
23 loaded, but for now it's not necessary. */
26 #include "libiberty.h"
28 #include "filenames.h"
31 #include <time.h> /* ctime, maybe time_t */
33 #ifndef HAVE_TIME_T_IN_TIME_H
34 #ifndef HAVE_TIME_T_IN_TYPES_H
47 CONST char *errmsg = bfd_errmsg (bfd_get_error ());
50 fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
52 fprintf (stderr, "%s: %s\n", program_name, errmsg);
59 bfd_nonfatal (string);
68 fprintf (stderr, "%s: ", program_name);
69 vfprintf (stderr, format, args);
73 #ifdef ANSI_PROTOTYPES
75 fatal (const char *format, ...)
79 va_start (args, format);
80 report (format, args);
86 non_fatal (const char *format, ...)
90 va_start (args, format);
91 report (format, args);
103 Format = va_arg (args, char *);
104 report (Format, args);
117 Format = va_arg (args, char *);
118 report (Format, args);
123 /* Set the default BFD target based on the configured target. Doing
124 this permits the binutils to be configured for a particular target,
125 and linked against a shared BFD library which was configured for a
129 set_default_bfd_target ()
131 /* The macro TARGET is defined by Makefile. */
132 const char *target = TARGET;
134 if (! bfd_set_default_target (target))
135 fatal (_("can't set BFD default target to `%s': %s"),
136 target, bfd_errmsg (bfd_get_error ()));
139 /* After a false return from bfd_check_format_matches with
140 bfd_get_error () == bfd_error_file_ambiguously_recognized, print
141 the possible matching targets. */
144 list_matching_formats (p)
147 fprintf (stderr, _("%s: Matching formats:"), program_name);
149 fprintf (stderr, " %s", *p++);
150 fputc ('\n', stderr);
153 /* List the supported targets. */
156 list_supported_targets (name, f)
160 extern const bfd_target *const *bfd_target_vector;
164 fprintf (f, _("Supported targets:"));
166 fprintf (f, _("%s: supported targets:"), name);
167 for (t = 0; bfd_target_vector[t] != NULL; t++)
168 fprintf (f, " %s", bfd_target_vector[t]->name);
172 /* List the supported architectures. */
175 list_supported_architectures (name, f)
182 fprintf (f, _("Supported architectures:"));
184 fprintf (f, _("%s: supported architectures:"), name);
186 for (arch = bfd_arch_list (); *arch; arch++)
187 fprintf (f, " %s", *arch);
191 /* Display the archive header for an element as if it were an ls -l listing:
193 Mode User\tGroup\tSize\tDate Name */
196 print_arelt_descr (file, abfd, verbose)
205 if (bfd_stat_arch_elt (abfd, &buf) == 0)
209 time_t when = buf.st_mtime;
210 CONST char *ctime_result = (CONST char *) ctime (&when);
212 /* POSIX format: skip weekday and seconds from ctime output. */
213 sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
215 mode_string (buf.st_mode, modebuf);
217 /* POSIX 1003.2/D11 says to skip first character (entry type). */
218 fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1,
219 (long) buf.st_uid, (long) buf.st_gid,
220 (long) buf.st_size, timebuf);
224 fprintf (file, "%s\n", bfd_get_filename (abfd));
227 /* Return the name of a temporary file in the same directory as FILENAME. */
230 make_tempname (filename)
233 static char template[] = "stXXXXXX";
235 char *slash = strrchr (filename, '/');
237 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
239 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
240 char *bslash = strrchr (filename, '\\');
241 if (slash == NULL || (bslash != NULL && bslash > slash))
243 if (slash == NULL && filename[0] != '\0' && filename[1] == ':')
244 slash = filename + 1;
248 if (slash != (char *) NULL)
254 tmpname = xmalloc (strlen (filename) + sizeof (template) + 2);
255 strcpy (tmpname, filename);
256 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
257 /* If tmpname is "X:", appending a slash will make it a root
258 directory on drive X, which is NOT the same as the current
259 directory on drive X. */
260 if (tmpname[1] == ':' && tmpname[2] == '\0')
261 strcat (tmpname, ".");
263 strcat (tmpname, "/");
264 strcat (tmpname, template);
270 tmpname = xmalloc (sizeof (template));
271 strcpy (tmpname, template);
277 /* Parse a string into a VMA, with a fatal error if it can't be
288 ret = bfd_scan_vma (s, &end, 0);
291 fatal (_("%s: bad number: %s"), arg, s);