Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* bucomm.c -- Bin Utils COMmon code. |
b1f88ebe | 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001, 2002 |
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 | |
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" | |
5af11cab | 28 | #include "filenames.h" |
252b5132 RH |
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 | |
252b5132 RH |
38 | \f |
39 | /* Error reporting */ | |
40 | ||
41 | char *program_name; | |
42 | ||
43 | void | |
44 | bfd_nonfatal (string) | |
b1f88ebe | 45 | const char *string; |
252b5132 | 46 | { |
b1f88ebe | 47 | const char *errmsg = bfd_errmsg (bfd_get_error ()); |
252b5132 RH |
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) | |
b1f88ebe | 57 | const char *string; |
252b5132 RH |
58 | { |
59 | bfd_nonfatal (string); | |
60 | xexit (1); | |
61 | } | |
62 | ||
37cc8ec1 | 63 | void |
252b5132 RH |
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 | ||
252b5132 | 73 | void |
451dad9c | 74 | fatal VPARAMS ((const char *format, ...)) |
252b5132 | 75 | { |
451dad9c AM |
76 | VA_OPEN (args, format); |
77 | VA_FIXEDARG (args, const char *, format); | |
252b5132 | 78 | |
252b5132 | 79 | report (format, args); |
451dad9c | 80 | VA_CLOSE (args); |
252b5132 RH |
81 | xexit (1); |
82 | } | |
83 | ||
84 | void | |
451dad9c | 85 | non_fatal VPARAMS ((const char *format, ...)) |
252b5132 | 86 | { |
451dad9c AM |
87 | VA_OPEN (args, format); |
88 | VA_FIXEDARG (args, const char *, format); | |
252b5132 | 89 | |
252b5132 | 90 | report (format, args); |
451dad9c | 91 | VA_CLOSE (args); |
252b5132 | 92 | } |
252b5132 RH |
93 | |
94 | /* Set the default BFD target based on the configured target. Doing | |
95 | this permits the binutils to be configured for a particular target, | |
96 | and linked against a shared BFD library which was configured for a | |
97 | different target. */ | |
98 | ||
99 | void | |
100 | set_default_bfd_target () | |
101 | { | |
102 | /* The macro TARGET is defined by Makefile. */ | |
103 | const char *target = TARGET; | |
104 | ||
105 | if (! bfd_set_default_target (target)) | |
106 | fatal (_("can't set BFD default target to `%s': %s"), | |
107 | target, bfd_errmsg (bfd_get_error ())); | |
108 | } | |
109 | ||
b34976b6 | 110 | /* After a FALSE return from bfd_check_format_matches with |
252b5132 RH |
111 | bfd_get_error () == bfd_error_file_ambiguously_recognized, print |
112 | the possible matching targets. */ | |
113 | ||
114 | void | |
115 | list_matching_formats (p) | |
116 | char **p; | |
117 | { | |
118 | fprintf (stderr, _("%s: Matching formats:"), program_name); | |
119 | while (*p) | |
120 | fprintf (stderr, " %s", *p++); | |
121 | fputc ('\n', stderr); | |
122 | } | |
123 | ||
124 | /* List the supported targets. */ | |
125 | ||
126 | void | |
127 | list_supported_targets (name, f) | |
128 | const char *name; | |
129 | FILE *f; | |
130 | { | |
252b5132 | 131 | int t; |
48417c1a | 132 | const char **targ_names = bfd_target_list (); |
252b5132 RH |
133 | |
134 | if (name == NULL) | |
135 | fprintf (f, _("Supported targets:")); | |
136 | else | |
137 | fprintf (f, _("%s: supported targets:"), name); | |
48417c1a AM |
138 | |
139 | for (t = 0; targ_names[t] != NULL; t++) | |
140 | fprintf (f, " %s", targ_names[t]); | |
252b5132 | 141 | fprintf (f, "\n"); |
48417c1a | 142 | free (targ_names); |
252b5132 | 143 | } |
2f83960e AM |
144 | |
145 | /* List the supported architectures. */ | |
146 | ||
147 | void | |
148 | list_supported_architectures (name, f) | |
149 | const char *name; | |
150 | FILE *f; | |
151 | { | |
48417c1a | 152 | const char **arch; |
2f83960e AM |
153 | |
154 | if (name == NULL) | |
155 | fprintf (f, _("Supported architectures:")); | |
156 | else | |
157 | fprintf (f, _("%s: supported architectures:"), name); | |
158 | ||
159 | for (arch = bfd_arch_list (); *arch; arch++) | |
160 | fprintf (f, " %s", *arch); | |
161 | fprintf (f, "\n"); | |
162 | } | |
252b5132 RH |
163 | \f |
164 | /* Display the archive header for an element as if it were an ls -l listing: | |
165 | ||
166 | Mode User\tGroup\tSize\tDate Name */ | |
167 | ||
168 | void | |
169 | print_arelt_descr (file, abfd, verbose) | |
170 | FILE *file; | |
171 | bfd *abfd; | |
b34976b6 | 172 | bfd_boolean verbose; |
252b5132 RH |
173 | { |
174 | struct stat buf; | |
175 | ||
176 | if (verbose) | |
177 | { | |
178 | if (bfd_stat_arch_elt (abfd, &buf) == 0) | |
179 | { | |
180 | char modebuf[11]; | |
181 | char timebuf[40]; | |
182 | time_t when = buf.st_mtime; | |
b1f88ebe | 183 | const char *ctime_result = (const char *) ctime (&when); |
252b5132 RH |
184 | |
185 | /* POSIX format: skip weekday and seconds from ctime output. */ | |
186 | sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20); | |
187 | ||
188 | mode_string (buf.st_mode, modebuf); | |
189 | modebuf[10] = '\0'; | |
190 | /* POSIX 1003.2/D11 says to skip first character (entry type). */ | |
191 | fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1, | |
192 | (long) buf.st_uid, (long) buf.st_gid, | |
193 | (long) buf.st_size, timebuf); | |
194 | } | |
195 | } | |
196 | ||
197 | fprintf (file, "%s\n", bfd_get_filename (abfd)); | |
198 | } | |
199 | ||
200 | /* Return the name of a temporary file in the same directory as FILENAME. */ | |
201 | ||
202 | char * | |
203 | make_tempname (filename) | |
204 | char *filename; | |
205 | { | |
206 | static char template[] = "stXXXXXX"; | |
207 | char *tmpname; | |
208 | char *slash = strrchr (filename, '/'); | |
209 | ||
5af11cab AM |
210 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
211 | { | |
212 | /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */ | |
213 | char *bslash = strrchr (filename, '\\'); | |
2ab47eed | 214 | if (slash == NULL || (bslash != NULL && bslash > slash)) |
5af11cab AM |
215 | slash = bslash; |
216 | if (slash == NULL && filename[0] != '\0' && filename[1] == ':') | |
a0c0ddf7 | 217 | slash = filename + 1; |
5af11cab | 218 | } |
252b5132 RH |
219 | #endif |
220 | ||
221 | if (slash != (char *) NULL) | |
222 | { | |
223 | char c; | |
224 | ||
225 | c = *slash; | |
226 | *slash = 0; | |
5af11cab | 227 | tmpname = xmalloc (strlen (filename) + sizeof (template) + 2); |
252b5132 | 228 | strcpy (tmpname, filename); |
5af11cab AM |
229 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
230 | /* If tmpname is "X:", appending a slash will make it a root | |
231 | directory on drive X, which is NOT the same as the current | |
232 | directory on drive X. */ | |
233 | if (tmpname[1] == ':' && tmpname[2] == '\0') | |
234 | strcat (tmpname, "."); | |
235 | #endif | |
252b5132 RH |
236 | strcat (tmpname, "/"); |
237 | strcat (tmpname, template); | |
af667503 | 238 | mktemp (tmpname); |
252b5132 RH |
239 | *slash = c; |
240 | } | |
241 | else | |
242 | { | |
243 | tmpname = xmalloc (sizeof (template)); | |
244 | strcpy (tmpname, template); | |
af667503 | 245 | mktemp (tmpname); |
252b5132 RH |
246 | } |
247 | return tmpname; | |
248 | } | |
249 | ||
250 | /* Parse a string into a VMA, with a fatal error if it can't be | |
251 | parsed. */ | |
252 | ||
253 | bfd_vma | |
254 | parse_vma (s, arg) | |
255 | const char *s; | |
256 | const char *arg; | |
257 | { | |
258 | bfd_vma ret; | |
259 | const char *end; | |
260 | ||
261 | ret = bfd_scan_vma (s, &end, 0); | |
f462a9ea | 262 | |
252b5132 RH |
263 | if (*end != '\0') |
264 | fatal (_("%s: bad number: %s"), arg, s); | |
265 | ||
266 | return ret; | |
267 | } |