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