Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* bucomm.c -- Bin Utils COMmon code. |
8c2bc687 | 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001 |
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 | |
a6a25652 NC |
38 | |
39 | /* Ought to be defined in libiberty.h... */ | |
40 | extern int mkstemps PARAMS ((char *, int)); | |
252b5132 RH |
41 | \f |
42 | /* Error reporting */ | |
43 | ||
44 | char *program_name; | |
45 | ||
46 | void | |
47 | bfd_nonfatal (string) | |
48 | CONST char *string; | |
49 | { | |
50 | CONST char *errmsg = bfd_errmsg (bfd_get_error ()); | |
51 | ||
52 | if (string) | |
53 | fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg); | |
54 | else | |
55 | fprintf (stderr, "%s: %s\n", program_name, errmsg); | |
56 | } | |
57 | ||
58 | void | |
59 | bfd_fatal (string) | |
60 | CONST char *string; | |
61 | { | |
62 | bfd_nonfatal (string); | |
63 | xexit (1); | |
64 | } | |
65 | ||
37cc8ec1 | 66 | void |
252b5132 RH |
67 | report (format, args) |
68 | const char * format; | |
69 | va_list args; | |
70 | { | |
71 | fprintf (stderr, "%s: ", program_name); | |
72 | vfprintf (stderr, format, args); | |
73 | putc ('\n', stderr); | |
74 | } | |
75 | ||
252b5132 | 76 | void |
451dad9c | 77 | fatal VPARAMS ((const char *format, ...)) |
252b5132 | 78 | { |
451dad9c AM |
79 | VA_OPEN (args, format); |
80 | VA_FIXEDARG (args, const char *, format); | |
252b5132 | 81 | |
252b5132 | 82 | report (format, args); |
451dad9c | 83 | VA_CLOSE (args); |
252b5132 RH |
84 | xexit (1); |
85 | } | |
86 | ||
87 | void | |
451dad9c | 88 | non_fatal VPARAMS ((const char *format, ...)) |
252b5132 | 89 | { |
451dad9c AM |
90 | VA_OPEN (args, format); |
91 | VA_FIXEDARG (args, const char *, format); | |
252b5132 | 92 | |
252b5132 | 93 | report (format, args); |
451dad9c | 94 | VA_CLOSE (args); |
252b5132 | 95 | } |
252b5132 RH |
96 | |
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 | |
100 | different target. */ | |
101 | ||
102 | void | |
103 | set_default_bfd_target () | |
104 | { | |
105 | /* The macro TARGET is defined by Makefile. */ | |
106 | const char *target = TARGET; | |
107 | ||
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 ())); | |
111 | } | |
112 | ||
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. */ | |
116 | ||
117 | void | |
118 | list_matching_formats (p) | |
119 | char **p; | |
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 | ||
129 | void | |
130 | list_supported_targets (name, f) | |
131 | const char *name; | |
132 | FILE *f; | |
133 | { | |
7340082d | 134 | extern const bfd_target *const *bfd_target_vector; |
252b5132 RH |
135 | int t; |
136 | ||
137 | if (name == NULL) | |
138 | fprintf (f, _("Supported targets:")); | |
139 | else | |
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); | |
143 | fprintf (f, "\n"); | |
144 | } | |
2f83960e AM |
145 | |
146 | /* List the supported architectures. */ | |
147 | ||
148 | void | |
149 | list_supported_architectures (name, f) | |
150 | const char *name; | |
151 | FILE *f; | |
152 | { | |
153 | const char** arch; | |
154 | ||
155 | if (name == NULL) | |
156 | fprintf (f, _("Supported architectures:")); | |
157 | else | |
158 | fprintf (f, _("%s: supported architectures:"), name); | |
159 | ||
160 | for (arch = bfd_arch_list (); *arch; arch++) | |
161 | fprintf (f, " %s", *arch); | |
162 | fprintf (f, "\n"); | |
163 | } | |
252b5132 RH |
164 | \f |
165 | /* Display the archive header for an element as if it were an ls -l listing: | |
166 | ||
167 | Mode User\tGroup\tSize\tDate Name */ | |
168 | ||
169 | void | |
170 | print_arelt_descr (file, abfd, verbose) | |
171 | FILE *file; | |
172 | bfd *abfd; | |
173 | boolean verbose; | |
174 | { | |
175 | struct stat buf; | |
176 | ||
177 | if (verbose) | |
178 | { | |
179 | if (bfd_stat_arch_elt (abfd, &buf) == 0) | |
180 | { | |
181 | char modebuf[11]; | |
182 | char timebuf[40]; | |
183 | time_t when = buf.st_mtime; | |
184 | CONST char *ctime_result = (CONST char *) ctime (&when); | |
185 | ||
186 | /* POSIX format: skip weekday and seconds from ctime output. */ | |
187 | sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20); | |
188 | ||
189 | mode_string (buf.st_mode, modebuf); | |
190 | modebuf[10] = '\0'; | |
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); | |
195 | } | |
196 | } | |
197 | ||
198 | fprintf (file, "%s\n", bfd_get_filename (abfd)); | |
199 | } | |
200 | ||
201 | /* Return the name of a temporary file in the same directory as FILENAME. */ | |
202 | ||
203 | char * | |
204 | make_tempname (filename) | |
205 | char *filename; | |
206 | { | |
207 | static char template[] = "stXXXXXX"; | |
208 | char *tmpname; | |
209 | char *slash = strrchr (filename, '/'); | |
210 | ||
5af11cab AM |
211 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
212 | { | |
213 | /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */ | |
214 | char *bslash = strrchr (filename, '\\'); | |
2ab47eed | 215 | if (slash == NULL || (bslash != NULL && bslash > slash)) |
5af11cab AM |
216 | slash = bslash; |
217 | if (slash == NULL && filename[0] != '\0' && filename[1] == ':') | |
a0c0ddf7 | 218 | slash = filename + 1; |
5af11cab | 219 | } |
252b5132 RH |
220 | #endif |
221 | ||
222 | if (slash != (char *) NULL) | |
223 | { | |
224 | char c; | |
225 | ||
226 | c = *slash; | |
227 | *slash = 0; | |
5af11cab | 228 | tmpname = xmalloc (strlen (filename) + sizeof (template) + 2); |
252b5132 | 229 | strcpy (tmpname, filename); |
5af11cab AM |
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, "."); | |
236 | #endif | |
252b5132 RH |
237 | strcat (tmpname, "/"); |
238 | strcat (tmpname, template); | |
a6a25652 | 239 | close (mkstemps (tmpname, 0)); |
252b5132 RH |
240 | *slash = c; |
241 | } | |
242 | else | |
243 | { | |
244 | tmpname = xmalloc (sizeof (template)); | |
245 | strcpy (tmpname, template); | |
a6a25652 | 246 | close (mkstemps (tmpname, 0)); |
252b5132 RH |
247 | } |
248 | return tmpname; | |
249 | } | |
250 | ||
251 | /* Parse a string into a VMA, with a fatal error if it can't be | |
252 | parsed. */ | |
253 | ||
254 | bfd_vma | |
255 | parse_vma (s, arg) | |
256 | const char *s; | |
257 | const char *arg; | |
258 | { | |
259 | bfd_vma ret; | |
260 | const char *end; | |
261 | ||
262 | ret = bfd_scan_vma (s, &end, 0); | |
263 | ||
264 | if (*end != '\0') | |
265 | fatal (_("%s: bad number: %s"), arg, s); | |
266 | ||
267 | return ret; | |
268 | } |