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
39 /* Ought to be defined in libiberty.h... */
40 extern int mkstemps PARAMS ((char *, int));
50 CONST char *errmsg = bfd_errmsg (bfd_get_error ());
53 fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
55 fprintf (stderr, "%s: %s\n", program_name, errmsg);
62 bfd_nonfatal (string);
71 fprintf (stderr, "%s: ", program_name);
72 vfprintf (stderr, format, args);
77 fatal VPARAMS ((const char *format, ...))
79 VA_OPEN (args, format);
80 VA_FIXEDARG (args, const char *, format);
82 report (format, args);
88 non_fatal VPARAMS ((const char *format, ...))
90 VA_OPEN (args, format);
91 VA_FIXEDARG (args, const char *, format);
93 report (format, args);
97 /* Set the default BFD target based on the configured target. Doing
98 this permits the binutils to be configured for a particular target,
99 and linked against a shared BFD library which was configured for a
103 set_default_bfd_target ()
105 /* The macro TARGET is defined by Makefile. */
106 const char *target = TARGET;
108 if (! bfd_set_default_target (target))
109 fatal (_("can't set BFD default target to `%s': %s"),
110 target, bfd_errmsg (bfd_get_error ()));
113 /* After a false return from bfd_check_format_matches with
114 bfd_get_error () == bfd_error_file_ambiguously_recognized, print
115 the possible matching targets. */
118 list_matching_formats (p)
121 fprintf (stderr, _("%s: Matching formats:"), program_name);
123 fprintf (stderr, " %s", *p++);
124 fputc ('\n', stderr);
127 /* List the supported targets. */
130 list_supported_targets (name, f)
134 extern const bfd_target *const *bfd_target_vector;
138 fprintf (f, _("Supported targets:"));
140 fprintf (f, _("%s: supported targets:"), name);
141 for (t = 0; bfd_target_vector[t] != NULL; t++)
142 fprintf (f, " %s", bfd_target_vector[t]->name);
146 /* List the supported architectures. */
149 list_supported_architectures (name, f)
156 fprintf (f, _("Supported architectures:"));
158 fprintf (f, _("%s: supported architectures:"), name);
160 for (arch = bfd_arch_list (); *arch; arch++)
161 fprintf (f, " %s", *arch);
165 /* Display the archive header for an element as if it were an ls -l listing:
167 Mode User\tGroup\tSize\tDate Name */
170 print_arelt_descr (file, abfd, verbose)
179 if (bfd_stat_arch_elt (abfd, &buf) == 0)
183 time_t when = buf.st_mtime;
184 CONST char *ctime_result = (CONST char *) ctime (&when);
186 /* POSIX format: skip weekday and seconds from ctime output. */
187 sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
189 mode_string (buf.st_mode, modebuf);
191 /* POSIX 1003.2/D11 says to skip first character (entry type). */
192 fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1,
193 (long) buf.st_uid, (long) buf.st_gid,
194 (long) buf.st_size, timebuf);
198 fprintf (file, "%s\n", bfd_get_filename (abfd));
201 /* Return the name of a temporary file in the same directory as FILENAME. */
204 make_tempname (filename)
207 static char template[] = "stXXXXXX";
209 char *slash = strrchr (filename, '/');
211 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
213 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
214 char *bslash = strrchr (filename, '\\');
215 if (slash == NULL || (bslash != NULL && bslash > slash))
217 if (slash == NULL && filename[0] != '\0' && filename[1] == ':')
218 slash = filename + 1;
222 if (slash != (char *) NULL)
228 tmpname = xmalloc (strlen (filename) + sizeof (template) + 2);
229 strcpy (tmpname, filename);
230 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
231 /* If tmpname is "X:", appending a slash will make it a root
232 directory on drive X, which is NOT the same as the current
233 directory on drive X. */
234 if (tmpname[1] == ':' && tmpname[2] == '\0')
235 strcat (tmpname, ".");
237 strcat (tmpname, "/");
238 strcat (tmpname, template);
239 close (mkstemps (tmpname, 0));
244 tmpname = xmalloc (sizeof (template));
245 strcpy (tmpname, template);
246 close (mkstemps (tmpname, 0));
251 /* Parse a string into a VMA, with a fatal error if it can't be
262 ret = bfd_scan_vma (s, &end, 0);
265 fatal (_("%s: bad number: %s"), arg, s);