1 | /* bucomm.c -- Bin Utils COMmon code.
|
---|
2 | Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001
|
---|
3 | Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | This file is part of GNU Binutils.
|
---|
6 |
|
---|
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.
|
---|
11 |
|
---|
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.
|
---|
16 |
|
---|
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
|
---|
20 | 02111-1307, USA. */
|
---|
21 | |
---|
22 |
|
---|
23 | /* We might put this in a library someday so it could be dynamically
|
---|
24 | loaded, but for now it's not necessary. */
|
---|
25 |
|
---|
26 | #include "bfd.h"
|
---|
27 | #include "libiberty.h"
|
---|
28 | #include "bucomm.h"
|
---|
29 | #include "filenames.h"
|
---|
30 |
|
---|
31 | #include <sys/stat.h>
|
---|
32 | #include <time.h> /* ctime, maybe time_t */
|
---|
33 |
|
---|
34 | #ifndef HAVE_TIME_T_IN_TIME_H
|
---|
35 | #ifndef HAVE_TIME_T_IN_TYPES_H
|
---|
36 | typedef long time_t;
|
---|
37 | #endif
|
---|
38 | #endif
|
---|
39 | |
---|
40 |
|
---|
41 | /* Error reporting */
|
---|
42 |
|
---|
43 | char *program_name;
|
---|
44 |
|
---|
45 | void
|
---|
46 | bfd_nonfatal (string)
|
---|
47 | CONST char *string;
|
---|
48 | {
|
---|
49 | CONST char *errmsg = bfd_errmsg (bfd_get_error ());
|
---|
50 |
|
---|
51 | if (string)
|
---|
52 | fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
|
---|
53 | else
|
---|
54 | fprintf (stderr, "%s: %s\n", program_name, errmsg);
|
---|
55 | }
|
---|
56 |
|
---|
57 | void
|
---|
58 | bfd_fatal (string)
|
---|
59 | CONST char *string;
|
---|
60 | {
|
---|
61 | bfd_nonfatal (string);
|
---|
62 | xexit (1);
|
---|
63 | }
|
---|
64 |
|
---|
65 | void
|
---|
66 | report (format, args)
|
---|
67 | const char * format;
|
---|
68 | va_list args;
|
---|
69 | {
|
---|
70 | fprintf (stderr, "%s: ", program_name);
|
---|
71 | vfprintf (stderr, format, args);
|
---|
72 | putc ('\n', stderr);
|
---|
73 | }
|
---|
74 |
|
---|
75 | #ifdef ANSI_PROTOTYPES
|
---|
76 | void
|
---|
77 | fatal (const char *format, ...)
|
---|
78 | {
|
---|
79 | va_list args;
|
---|
80 |
|
---|
81 | va_start (args, format);
|
---|
82 | report (format, args);
|
---|
83 | va_end (args);
|
---|
84 | xexit (1);
|
---|
85 | }
|
---|
86 |
|
---|
87 | void
|
---|
88 | non_fatal (const char *format, ...)
|
---|
89 | {
|
---|
90 | va_list args;
|
---|
91 |
|
---|
92 | va_start (args, format);
|
---|
93 | report (format, args);
|
---|
94 | va_end (args);
|
---|
95 | }
|
---|
96 | #else
|
---|
97 | void
|
---|
98 | fatal (va_alist)
|
---|
99 | va_dcl
|
---|
100 | {
|
---|
101 | char *Format;
|
---|
102 | va_list args;
|
---|
103 |
|
---|
104 | va_start (args);
|
---|
105 | Format = va_arg (args, char *);
|
---|
106 | report (Format, args);
|
---|
107 | va_end (args);
|
---|
108 | xexit (1);
|
---|
109 | }
|
---|
110 |
|
---|
111 | void
|
---|
112 | non_fatal (va_alist)
|
---|
113 | va_dcl
|
---|
114 | {
|
---|
115 | char *Format;
|
---|
116 | va_list args;
|
---|
117 |
|
---|
118 | va_start (args);
|
---|
119 | Format = va_arg (args, char *);
|
---|
120 | report (Format, args);
|
---|
121 | va_end (args);
|
---|
122 | }
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | /* Set the default BFD target based on the configured target. Doing
|
---|
126 | this permits the binutils to be configured for a particular target,
|
---|
127 | and linked against a shared BFD library which was configured for a
|
---|
128 | different target. */
|
---|
129 |
|
---|
130 | void
|
---|
131 | set_default_bfd_target ()
|
---|
132 | {
|
---|
133 | /* The macro TARGET is defined by Makefile. */
|
---|
134 | const char *target = TARGET;
|
---|
135 |
|
---|
136 | if (! bfd_set_default_target (target))
|
---|
137 | fatal (_("can't set BFD default target to `%s': %s"),
|
---|
138 | target, bfd_errmsg (bfd_get_error ()));
|
---|
139 | }
|
---|
140 |
|
---|
141 | /* After a false return from bfd_check_format_matches with
|
---|
142 | bfd_get_error () == bfd_error_file_ambiguously_recognized, print
|
---|
143 | the possible matching targets. */
|
---|
144 |
|
---|
145 | void
|
---|
146 | list_matching_formats (p)
|
---|
147 | char **p;
|
---|
148 | {
|
---|
149 | fprintf (stderr, _("%s: Matching formats:"), program_name);
|
---|
150 | while (*p)
|
---|
151 | fprintf (stderr, " %s", *p++);
|
---|
152 | fputc ('\n', stderr);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /* List the supported targets. */
|
---|
156 |
|
---|
157 | void
|
---|
158 | list_supported_targets (name, f)
|
---|
159 | const char *name;
|
---|
160 | FILE *f;
|
---|
161 | {
|
---|
162 | extern const bfd_target *const *bfd_target_vector;
|
---|
163 | int t;
|
---|
164 |
|
---|
165 | if (name == NULL)
|
---|
166 | fprintf (f, _("Supported targets:"));
|
---|
167 | else
|
---|
168 | fprintf (f, _("%s: supported targets:"), name);
|
---|
169 | for (t = 0; bfd_target_vector[t] != NULL; t++)
|
---|
170 | fprintf (f, " %s", bfd_target_vector[t]->name);
|
---|
171 | fprintf (f, "\n");
|
---|
172 | }
|
---|
173 | |
---|
174 |
|
---|
175 | /* Display the archive header for an element as if it were an ls -l listing:
|
---|
176 |
|
---|
177 | Mode User\tGroup\tSize\tDate Name */
|
---|
178 |
|
---|
179 | void
|
---|
180 | print_arelt_descr (file, abfd, verbose)
|
---|
181 | FILE *file;
|
---|
182 | bfd *abfd;
|
---|
183 | boolean verbose;
|
---|
184 | {
|
---|
185 | struct stat buf;
|
---|
186 |
|
---|
187 | if (verbose)
|
---|
188 | {
|
---|
189 | if (bfd_stat_arch_elt (abfd, &buf) == 0)
|
---|
190 | {
|
---|
191 | char modebuf[11];
|
---|
192 | char timebuf[40];
|
---|
193 | time_t when = buf.st_mtime;
|
---|
194 | CONST char *ctime_result = (CONST char *) ctime (&when);
|
---|
195 |
|
---|
196 | /* POSIX format: skip weekday and seconds from ctime output. */
|
---|
197 | sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
|
---|
198 |
|
---|
199 | mode_string (buf.st_mode, modebuf);
|
---|
200 | modebuf[10] = '\0';
|
---|
201 | /* POSIX 1003.2/D11 says to skip first character (entry type). */
|
---|
202 | fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1,
|
---|
203 | (long) buf.st_uid, (long) buf.st_gid,
|
---|
204 | (long) buf.st_size, timebuf);
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | fprintf (file, "%s\n", bfd_get_filename (abfd));
|
---|
209 | }
|
---|
210 |
|
---|
211 | /* Return the name of a temporary file in the same directory as FILENAME. */
|
---|
212 |
|
---|
213 | char *
|
---|
214 | make_tempname (filename)
|
---|
215 | char *filename;
|
---|
216 | {
|
---|
217 | static char template[] = "stXXXXXX";
|
---|
218 | char *tmpname;
|
---|
219 | char *slash = strrchr (filename, '/');
|
---|
220 |
|
---|
221 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM
|
---|
222 | {
|
---|
223 | /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
|
---|
224 | char *bslash = strrchr (filename, '\\');
|
---|
225 | if (slash == NULL || (bslash != NULL && bslash > slash))
|
---|
226 | slash = bslash;
|
---|
227 | if (slash == NULL && filename[0] != '\0' && filename[1] == ':')
|
---|
228 | slash = filename + 1;
|
---|
229 | }
|
---|
230 | #endif
|
---|
231 |
|
---|
232 | if (slash != (char *) NULL)
|
---|
233 | {
|
---|
234 | char c;
|
---|
235 |
|
---|
236 | c = *slash;
|
---|
237 | *slash = 0;
|
---|
238 | tmpname = xmalloc (strlen (filename) + sizeof (template) + 2);
|
---|
239 | strcpy (tmpname, filename);
|
---|
240 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM
|
---|
241 | /* If tmpname is "X:", appending a slash will make it a root
|
---|
242 | directory on drive X, which is NOT the same as the current
|
---|
243 | directory on drive X. */
|
---|
244 | if (tmpname[1] == ':' && tmpname[2] == '\0')
|
---|
245 | strcat (tmpname, ".");
|
---|
246 | #endif
|
---|
247 | strcat (tmpname, "/");
|
---|
248 | strcat (tmpname, template);
|
---|
249 | mktemp (tmpname);
|
---|
250 | *slash = c;
|
---|
251 | }
|
---|
252 | else
|
---|
253 | {
|
---|
254 | tmpname = xmalloc (sizeof (template));
|
---|
255 | strcpy (tmpname, template);
|
---|
256 | mktemp (tmpname);
|
---|
257 | }
|
---|
258 | return tmpname;
|
---|
259 | }
|
---|
260 |
|
---|
261 | /* Parse a string into a VMA, with a fatal error if it can't be
|
---|
262 | parsed. */
|
---|
263 |
|
---|
264 | bfd_vma
|
---|
265 | parse_vma (s, arg)
|
---|
266 | const char *s;
|
---|
267 | const char *arg;
|
---|
268 | {
|
---|
269 | bfd_vma ret;
|
---|
270 | const char *end;
|
---|
271 |
|
---|
272 | ret = bfd_scan_vma (s, &end, 0);
|
---|
273 |
|
---|
274 | if (*end != '\0')
|
---|
275 | fatal (_("%s: bad number: %s"), arg, s);
|
---|
276 |
|
---|
277 | return ret;
|
---|
278 | }
|
---|