Add changelog entries for copyright updates.
[deliverable/binutils-gdb.git] / binutils / bucomm.c
CommitLineData
252b5132 1/* bucomm.c -- Bin Utils COMmon code.
f9c026a8 2 Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001, 2002, 2003, 2006
37cc8ec1 3 Free Software Foundation, Inc.
252b5132
RH
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
b43b5d5f
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
252b5132
RH
21\f
22/* We might put this in a library someday so it could be dynamically
23 loaded, but for now it's not necessary. */
24
25#include "bfd.h"
06d86cf7 26#include "bfdver.h"
252b5132
RH
27#include "libiberty.h"
28#include "bucomm.h"
5af11cab 29#include "filenames.h"
06d86cf7 30#include "libbfd.h"
252b5132
RH
31
32#include <sys/stat.h>
33#include <time.h> /* ctime, maybe time_t */
77f762d6 34#include <assert.h>
252b5132
RH
35
36#ifndef HAVE_TIME_T_IN_TIME_H
37#ifndef HAVE_TIME_T_IN_TYPES_H
38typedef long time_t;
39#endif
40#endif
06d86cf7 41
2da42df6
AJ
42static const char * endian_string (enum bfd_endian);
43static int display_target_list (void);
44static int display_info_table (int, int);
45static int display_target_tables (void);
252b5132 46\f
06d86cf7 47/* Error reporting. */
252b5132
RH
48
49char *program_name;
50
51void
2da42df6 52bfd_nonfatal (const char *string)
252b5132 53{
b1f88ebe 54 const char *errmsg = bfd_errmsg (bfd_get_error ());
252b5132
RH
55
56 if (string)
57 fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
58 else
59 fprintf (stderr, "%s: %s\n", program_name, errmsg);
60}
61
62void
2da42df6 63bfd_fatal (const char *string)
252b5132
RH
64{
65 bfd_nonfatal (string);
66 xexit (1);
67}
68
cba12006 69void
2da42df6 70report (const char * format, va_list args)
252b5132
RH
71{
72 fprintf (stderr, "%s: ", program_name);
73 vfprintf (stderr, format, args);
74 putc ('\n', stderr);
75}
76
252b5132 77void
451dad9c 78fatal VPARAMS ((const char *format, ...))
252b5132 79{
451dad9c
AM
80 VA_OPEN (args, format);
81 VA_FIXEDARG (args, const char *, format);
252b5132 82
252b5132 83 report (format, args);
451dad9c 84 VA_CLOSE (args);
252b5132
RH
85 xexit (1);
86}
87
88void
451dad9c 89non_fatal VPARAMS ((const char *format, ...))
252b5132 90{
451dad9c
AM
91 VA_OPEN (args, format);
92 VA_FIXEDARG (args, const char *, format);
252b5132 93
252b5132 94 report (format, args);
451dad9c 95 VA_CLOSE (args);
252b5132 96}
252b5132
RH
97
98/* Set the default BFD target based on the configured target. Doing
99 this permits the binutils to be configured for a particular target,
100 and linked against a shared BFD library which was configured for a
101 different target. */
102
103void
2da42df6 104set_default_bfd_target (void)
252b5132
RH
105{
106 /* The macro TARGET is defined by Makefile. */
107 const char *target = TARGET;
108
109 if (! bfd_set_default_target (target))
110 fatal (_("can't set BFD default target to `%s': %s"),
111 target, bfd_errmsg (bfd_get_error ()));
112}
113
b34976b6 114/* After a FALSE return from bfd_check_format_matches with
252b5132
RH
115 bfd_get_error () == bfd_error_file_ambiguously_recognized, print
116 the possible matching targets. */
117
118void
2da42df6 119list_matching_formats (char **p)
252b5132
RH
120{
121 fprintf (stderr, _("%s: Matching formats:"), program_name);
122 while (*p)
123 fprintf (stderr, " %s", *p++);
124 fputc ('\n', stderr);
125}
126
127/* List the supported targets. */
128
129void
2da42df6 130list_supported_targets (const char *name, FILE *f)
252b5132 131{
252b5132 132 int t;
48417c1a 133 const char **targ_names = bfd_target_list ();
252b5132
RH
134
135 if (name == NULL)
136 fprintf (f, _("Supported targets:"));
137 else
138 fprintf (f, _("%s: supported targets:"), name);
48417c1a
AM
139
140 for (t = 0; targ_names[t] != NULL; t++)
141 fprintf (f, " %s", targ_names[t]);
252b5132 142 fprintf (f, "\n");
48417c1a 143 free (targ_names);
252b5132 144}
2f83960e
AM
145
146/* List the supported architectures. */
147
148void
2da42df6 149list_supported_architectures (const char *name, FILE *f)
2f83960e 150{
48417c1a 151 const char **arch;
2f83960e
AM
152
153 if (name == NULL)
154 fprintf (f, _("Supported architectures:"));
155 else
156 fprintf (f, _("%s: supported architectures:"), name);
157
158 for (arch = bfd_arch_list (); *arch; arch++)
159 fprintf (f, " %s", *arch);
160 fprintf (f, "\n");
161}
252b5132 162\f
06d86cf7
NC
163/* The length of the longest architecture name + 1. */
164#define LONGEST_ARCH sizeof ("powerpc:common")
165
166static const char *
2da42df6 167endian_string (enum bfd_endian endian)
06d86cf7
NC
168{
169 switch (endian)
170 {
171 case BFD_ENDIAN_BIG: return "big endian";
172 case BFD_ENDIAN_LITTLE: return "little endian";
173 default: return "endianness unknown";
174 }
175}
176
177/* List the targets that BFD is configured to support, each followed
178 by its endianness and the architectures it supports. */
179
180static int
2da42df6 181display_target_list (void)
06d86cf7
NC
182{
183 char *dummy_name;
184 int t;
185 int ret = 1;
186
187 dummy_name = make_temp_file (NULL);
188 for (t = 0; bfd_target_vector[t]; t++)
189 {
190 const bfd_target *p = bfd_target_vector[t];
191 bfd *abfd = bfd_openw (dummy_name, p->name);
91610c0c 192 enum bfd_architecture a;
06d86cf7
NC
193
194 printf ("%s\n (header %s, data %s)\n", p->name,
195 endian_string (p->header_byteorder),
196 endian_string (p->byteorder));
197
198 if (abfd == NULL)
199 {
200 bfd_nonfatal (dummy_name);
201 ret = 0;
202 continue;
203 }
204
205 if (! bfd_set_format (abfd, bfd_object))
206 {
207 if (bfd_get_error () != bfd_error_invalid_operation)
208 {
209 bfd_nonfatal (p->name);
210 ret = 0;
211 }
212 bfd_close_all_done (abfd);
213 continue;
214 }
215
91610c0c 216 for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
06d86cf7
NC
217 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
218 printf (" %s\n",
219 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
220 bfd_close_all_done (abfd);
221 }
222 unlink (dummy_name);
223 free (dummy_name);
224
225 return ret;
226}
227
228/* Print a table showing which architectures are supported for entries
229 FIRST through LAST-1 of bfd_target_vector (targets across,
230 architectures down). */
231
232static int
2da42df6 233display_info_table (int first, int last)
06d86cf7
NC
234{
235 int t;
06d86cf7
NC
236 int ret = 1;
237 char *dummy_name;
91610c0c 238 enum bfd_architecture a;
06d86cf7
NC
239
240 /* Print heading of target names. */
241 printf ("\n%*s", (int) LONGEST_ARCH, " ");
242 for (t = first; t < last && bfd_target_vector[t]; t++)
243 printf ("%s ", bfd_target_vector[t]->name);
244 putchar ('\n');
245
246 dummy_name = make_temp_file (NULL);
91610c0c 247 for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
06d86cf7
NC
248 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
249 {
250 printf ("%*s ", (int) LONGEST_ARCH - 1,
251 bfd_printable_arch_mach (a, 0));
252 for (t = first; t < last && bfd_target_vector[t]; t++)
253 {
254 const bfd_target *p = bfd_target_vector[t];
255 bfd_boolean ok = TRUE;
256 bfd *abfd = bfd_openw (dummy_name, p->name);
257
258 if (abfd == NULL)
259 {
260 bfd_nonfatal (p->name);
261 ret = 0;
262 ok = FALSE;
263 }
264
265 if (ok)
266 {
267 if (! bfd_set_format (abfd, bfd_object))
268 {
269 if (bfd_get_error () != bfd_error_invalid_operation)
270 {
271 bfd_nonfatal (p->name);
272 ret = 0;
273 }
274 ok = FALSE;
275 }
276 }
277
278 if (ok)
279 {
280 if (! bfd_set_arch_mach (abfd, a, 0))
281 ok = FALSE;
282 }
283
284 if (ok)
285 printf ("%s ", p->name);
286 else
287 {
288 int l = strlen (p->name);
289 while (l--)
290 putchar ('-');
291 putchar (' ');
292 }
293 if (abfd != NULL)
294 bfd_close_all_done (abfd);
295 }
296 putchar ('\n');
297 }
298 unlink (dummy_name);
299 free (dummy_name);
300
301 return ret;
302}
303
304/* Print tables of all the target-architecture combinations that
305 BFD has been configured to support. */
306
307static int
2da42df6 308display_target_tables (void)
06d86cf7
NC
309{
310 int t;
311 int columns;
312 int ret = 1;
313 char *colum;
314
315 columns = 0;
316 colum = getenv ("COLUMNS");
317 if (colum != NULL)
318 columns = atoi (colum);
319 if (columns == 0)
320 columns = 80;
321
322 t = 0;
323 while (bfd_target_vector[t] != NULL)
324 {
325 int oldt = t, wid;
326
327 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
328 ++t;
329 while (wid < columns && bfd_target_vector[t] != NULL)
330 {
331 int newwid;
332
333 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
334 if (newwid >= columns)
335 break;
336 wid = newwid;
337 ++t;
338 }
339 if (! display_info_table (oldt, t))
340 ret = 0;
341 }
342
343 return ret;
344}
345
346int
2da42df6 347display_info (void)
06d86cf7
NC
348{
349 printf (_("BFD header file version %s\n"), BFD_VERSION_STRING);
350 if (! display_target_list () || ! display_target_tables ())
351 return 1;
352 else
353 return 0;
354}
355\f
252b5132
RH
356/* Display the archive header for an element as if it were an ls -l listing:
357
358 Mode User\tGroup\tSize\tDate Name */
359
360void
2da42df6 361print_arelt_descr (FILE *file, bfd *abfd, bfd_boolean verbose)
252b5132
RH
362{
363 struct stat buf;
364
365 if (verbose)
366 {
367 if (bfd_stat_arch_elt (abfd, &buf) == 0)
368 {
369 char modebuf[11];
370 char timebuf[40];
371 time_t when = buf.st_mtime;
b1f88ebe 372 const char *ctime_result = (const char *) ctime (&when);
252b5132
RH
373
374 /* POSIX format: skip weekday and seconds from ctime output. */
375 sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
376
377 mode_string (buf.st_mode, modebuf);
378 modebuf[10] = '\0';
379 /* POSIX 1003.2/D11 says to skip first character (entry type). */
380 fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1,
381 (long) buf.st_uid, (long) buf.st_gid,
382 (long) buf.st_size, timebuf);
383 }
384 }
385
386 fprintf (file, "%s\n", bfd_get_filename (abfd));
387}
388
f9c026a8 389/* Return the name of a created temporary file in the same directory as FILENAME. */
252b5132
RH
390
391char *
2da42df6 392make_tempname (char *filename)
252b5132 393{
f9c026a8
NC
394#if defined(HAVE_MKSTEMP)
395 static char template[] = "stXXXXXXXXXX";
396 int fd;
397#else
252b5132 398 static char template[] = "stXXXXXX";
f9c026a8 399#endif
252b5132
RH
400 char *tmpname;
401 char *slash = strrchr (filename, '/');
402
5af11cab
AM
403#ifdef HAVE_DOS_BASED_FILE_SYSTEM
404 {
405 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
406 char *bslash = strrchr (filename, '\\');
f9c026a8 407
2ab47eed 408 if (slash == NULL || (bslash != NULL && bslash > slash))
5af11cab
AM
409 slash = bslash;
410 if (slash == NULL && filename[0] != '\0' && filename[1] == ':')
a0c0ddf7 411 slash = filename + 1;
5af11cab 412 }
252b5132
RH
413#endif
414
415 if (slash != (char *) NULL)
416 {
417 char c;
418
419 c = *slash;
420 *slash = 0;
5af11cab 421 tmpname = xmalloc (strlen (filename) + sizeof (template) + 2);
252b5132 422 strcpy (tmpname, filename);
5af11cab
AM
423#ifdef HAVE_DOS_BASED_FILE_SYSTEM
424 /* If tmpname is "X:", appending a slash will make it a root
425 directory on drive X, which is NOT the same as the current
426 directory on drive X. */
427 if (tmpname[1] == ':' && tmpname[2] == '\0')
428 strcat (tmpname, ".");
429#endif
252b5132
RH
430 strcat (tmpname, "/");
431 strcat (tmpname, template);
f9c026a8
NC
432#if defined(HAVE_MKSTEMP)
433 fd = mkstemp (tmpname);
434#else
af667503 435 mktemp (tmpname);
f9c026a8 436#endif
252b5132
RH
437 *slash = c;
438 }
439 else
440 {
441 tmpname = xmalloc (sizeof (template));
442 strcpy (tmpname, template);
f9c026a8
NC
443#if defined(HAVE_MKSTEMP)
444 fd = mkstemp (tmpname);
445#endif
446 }
447#if defined(HAVE_MKSTEMP)
448 if (fd == -1)
449 return NULL;
450 close(fd);
451#endif
452 return tmpname;
453}
454
455#if defined(HAVE_MKDTEMP)
456/* Return the name of a created temporary directory inside the directory containing FILENAME. */
457
458char *
459make_tempdir (char *filename)
460{
461 static char template[] = "stXXXXXXXXXX";
462 char *tmpname;
463 char *slash = strrchr (filename, '/');
464
465 if (slash != (char *) NULL)
466 {
467 char c;
468
469 c = *slash;
470 *slash = 0;
471 tmpname = xmalloc (strlen (filename) + sizeof (template) + 1);
472 strcpy (tmpname, filename);
473#ifdef HAVE_DOS_BASED_FILE_SYSTEM
474 /* If tmpname is "X:", appending a slash will make it a root
475 directory on drive X, which is NOT the same as the current
476 directory on drive X. */
477 if (tmpname[1] == ':' && tmpname[2] == '\0')
478 strcat (tmpname, ".");
479#endif
480 strcat (tmpname, "/");
481 strcat (tmpname, template);
482 mkdtemp (tmpname);
483 *slash = c;
484 }
485 else
486 {
487 tmpname = xmalloc (sizeof (template));
488 strcpy (tmpname, template);
489 mkdtemp (tmpname);
252b5132
RH
490 }
491 return tmpname;
492}
f9c026a8 493#endif /* HAVE_MKDTEMP */
252b5132
RH
494
495/* Parse a string into a VMA, with a fatal error if it can't be
496 parsed. */
497
498bfd_vma
2da42df6 499parse_vma (const char *s, const char *arg)
252b5132
RH
500{
501 bfd_vma ret;
502 const char *end;
503
504 ret = bfd_scan_vma (s, &end, 0);
f462a9ea 505
252b5132
RH
506 if (*end != '\0')
507 fatal (_("%s: bad number: %s"), arg, s);
508
509 return ret;
510}
f24ddbdd
NC
511
512/* Returns the size of the named file. If the file does not
513 exist, or if it is not a real file, then a suitable non-fatal
514 error message is printed and zero is returned. */
515
516off_t
517get_file_size (const char * file_name)
518{
519 struct stat statbuf;
520
521 if (stat (file_name, &statbuf) < 0)
522 {
523 if (errno == ENOENT)
524 non_fatal (_("'%s': No such file"), file_name);
525 else
526 non_fatal (_("Warning: could not locate '%s'. reason: %s"),
527 file_name, strerror (errno));
528 }
529 else if (! S_ISREG (statbuf.st_mode))
530 non_fatal (_("Warning: '%s' is not an ordinary file"), file_name);
531 else
532 return statbuf.st_size;
533
534 return 0;
535}
77f762d6
L
536
537/* Return the filename in a static buffer. */
538
539const char *
540bfd_get_archive_filename (bfd *abfd)
541{
542 static size_t curr = 0;
543 static char *buf;
544 size_t needed;
545
546 assert (abfd != NULL);
547
548 if (!abfd->my_archive)
549 return bfd_get_filename (abfd);
550
551 needed = (strlen (bfd_get_filename (abfd->my_archive))
552 + strlen (bfd_get_filename (abfd)) + 3);
553 if (needed > curr)
554 {
555 if (curr)
556 free (buf);
557 curr = needed + (needed >> 1);
558 buf = bfd_malloc (curr);
559 /* If we can't malloc, fail safe by returning just the file name.
560 This function is only used when building error messages. */
561 if (!buf)
562 {
563 curr = 0;
564 return bfd_get_filename (abfd);
565 }
566 }
567 sprintf (buf, "%s(%s)", bfd_get_filename (abfd->my_archive),
568 bfd_get_filename (abfd));
569 return buf;
570}
This page took 0.322874 seconds and 4 git commands to generate.