Commit | Line | Data |
---|---|---|
b886a6e3 | 1 | /* bucomm.c -- Bin Utils COMmon code. |
fa0cd59b | 2 | Copyright (C) 1991, 92, 93, 94, 95, 1997 Free Software Foundation, Inc. |
2fa0b342 | 3 | |
096aefc0 | 4 | This file is part of GNU Binutils. |
b886a6e3 | 5 | |
096aefc0 KR |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
b886a6e3 | 10 | |
096aefc0 KR |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
b886a6e3 | 15 | |
096aefc0 KR |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
fa0cd59b ILT |
18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
19 | 02111-1307, USA. */ | |
096aefc0 KR |
20 | \f |
21 | /* We might put this in a library someday so it could be dynamically | |
22 | loaded, but for now it's not necessary. */ | |
2fa0b342 | 23 | |
2fa0b342 | 24 | #include "bfd.h" |
fb3f84c7 | 25 | #include "libiberty.h" |
0886e098 SS |
26 | #include "bucomm.h" |
27 | ||
fa0cd59b ILT |
28 | #include <sys/stat.h> |
29 | #include <time.h> /* ctime, maybe time_t */ | |
30 | ||
31 | #ifndef HAVE_TIME_T_IN_TIME_H | |
32 | #ifndef HAVE_TIME_T_IN_TYPES_H | |
33 | typedef long time_t; | |
34 | #endif | |
35 | #endif | |
36 | ||
fb3f84c7 | 37 | #ifdef ANSI_PROTOTYPES |
0886e098 SS |
38 | #include <stdarg.h> |
39 | #else | |
2fa0b342 | 40 | #include <varargs.h> |
0886e098 | 41 | #endif |
2fa0b342 DHW |
42 | |
43 | char *target = NULL; /* default as late as possible */ | |
fb3f84c7 | 44 | \f |
2fa0b342 DHW |
45 | /* Error reporting */ |
46 | ||
47 | char *program_name; | |
48 | ||
49 | void | |
096aefc0 | 50 | bfd_nonfatal (string) |
0886e098 | 51 | CONST char *string; |
2fa0b342 | 52 | { |
fb3f84c7 | 53 | CONST char *errmsg = bfd_errmsg (bfd_get_error ()); |
096aefc0 | 54 | |
2fa0b342 DHW |
55 | if (string) |
56 | fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg); | |
57 | else | |
58 | fprintf (stderr, "%s: %s\n", program_name, errmsg); | |
096aefc0 | 59 | } |
2fa0b342 | 60 | |
096aefc0 KR |
61 | void |
62 | bfd_fatal (string) | |
0886e098 | 63 | CONST char *string; |
096aefc0 KR |
64 | { |
65 | bfd_nonfatal (string); | |
fb3f84c7 | 66 | xexit (1); |
2fa0b342 DHW |
67 | } |
68 | ||
fb3f84c7 | 69 | #ifdef ANSI_PROTOTYPES |
2fa0b342 | 70 | void |
0886e098 | 71 | fatal (const char *format, ...) |
2fa0b342 DHW |
72 | { |
73 | va_list args; | |
096aefc0 KR |
74 | |
75 | fprintf (stderr, "%s: ", program_name); | |
0886e098 SS |
76 | va_start (args, format); |
77 | vfprintf (stderr, format, args); | |
2fa0b342 | 78 | va_end (args); |
096aefc0 | 79 | putc ('\n', stderr); |
fb3f84c7 | 80 | xexit (1); |
2fa0b342 DHW |
81 | } |
82 | #else | |
096aefc0 KR |
83 | void |
84 | fatal (va_alist) | |
2fa0b342 DHW |
85 | va_dcl |
86 | { | |
096aefc0 KR |
87 | char *Format; |
88 | va_list args; | |
2fa0b342 | 89 | |
096aefc0 KR |
90 | fprintf (stderr, "%s: ", program_name); |
91 | va_start (args); | |
92 | Format = va_arg (args, char *); | |
93 | vfprintf (stderr, Format, args); | |
94 | va_end (args); | |
95 | putc ('\n', stderr); | |
fb3f84c7 | 96 | xexit (1); |
096aefc0 KR |
97 | } |
98 | #endif | |
fb3f84c7 | 99 | |
fa0cd59b ILT |
100 | /* Set the default BFD target based on the configured target. Doing |
101 | this permits the binutils to be configured for a particular target, | |
102 | and linked against a shared BFD library which was configured for a | |
103 | different target. */ | |
104 | ||
105 | void | |
106 | set_default_bfd_target () | |
107 | { | |
108 | /* The macro TARGET is defined by Makefile. */ | |
109 | const char *target = TARGET; | |
110 | ||
111 | if (! bfd_set_default_target (target)) | |
112 | { | |
113 | char *errmsg; | |
114 | ||
115 | errmsg = (char *) xmalloc (100 + strlen (target)); | |
116 | sprintf (errmsg, "can't set BFD default target to `%s'", target); | |
117 | bfd_fatal (errmsg); | |
118 | } | |
119 | } | |
120 | ||
fb3f84c7 | 121 | /* After a false return from bfd_check_format_matches with |
fa0cd59b ILT |
122 | bfd_get_error () == bfd_error_file_ambiguously_recognized, print |
123 | the possible matching targets. */ | |
fb3f84c7 ILT |
124 | |
125 | void | |
126 | list_matching_formats (p) | |
127 | char **p; | |
128 | { | |
129 | fprintf(stderr, "%s: Matching formats:", program_name); | |
130 | while (*p) | |
131 | fprintf(stderr, " %s", *p++); | |
132 | fprintf(stderr, "\n"); | |
133 | } | |
134 | ||
135 | /* List the supported targets. */ | |
136 | ||
137 | void | |
138 | list_supported_targets (name, f) | |
139 | const char *name; | |
140 | FILE *f; | |
141 | { | |
142 | extern bfd_target *bfd_target_vector[]; | |
143 | int t; | |
144 | ||
145 | if (name == NULL) | |
146 | fprintf (f, "Supported targets:"); | |
147 | else | |
148 | fprintf (f, "%s: supported targets:", name); | |
149 | for (t = 0; bfd_target_vector[t] != NULL; t++) | |
150 | fprintf (f, " %s", bfd_target_vector[t]->name); | |
151 | fprintf (f, "\n"); | |
152 | } | |
2fa0b342 | 153 | \f |
096aefc0 KR |
154 | /* Display the archive header for an element as if it were an ls -l listing: |
155 | ||
156 | Mode User\tGroup\tSize\tDate Name */ | |
2fa0b342 DHW |
157 | |
158 | void | |
096aefc0 KR |
159 | print_arelt_descr (file, abfd, verbose) |
160 | FILE *file; | |
161 | bfd *abfd; | |
162 | boolean verbose; | |
2fa0b342 DHW |
163 | { |
164 | struct stat buf; | |
2fa0b342 | 165 | |
096aefc0 KR |
166 | if (verbose) |
167 | { | |
168 | if (bfd_stat_arch_elt (abfd, &buf) == 0) | |
169 | { | |
170 | char modebuf[11]; | |
171 | char timebuf[40]; | |
0886e098 | 172 | time_t when = buf.st_mtime; |
096aefc0 KR |
173 | CONST char *ctime_result = (CONST char *) ctime (&when); |
174 | ||
175 | /* POSIX format: skip weekday and seconds from ctime output. */ | |
176 | sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20); | |
177 | ||
178 | mode_string (buf.st_mode, modebuf); | |
179 | modebuf[10] = '\0'; | |
180 | /* POSIX 1003.2/D11 says to skip first character (entry type). */ | |
fb3f84c7 ILT |
181 | fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1, |
182 | (long) buf.st_uid, (long) buf.st_gid, | |
183 | (long) buf.st_size, timebuf); | |
096aefc0 | 184 | } |
2fa0b342 | 185 | } |
2fa0b342 | 186 | |
fb3f84c7 | 187 | fprintf (file, "%s\n", bfd_get_filename (abfd)); |
2fa0b342 | 188 | } |
fa0cd59b ILT |
189 | |
190 | /* Return the name of a temporary file in the same directory as FILENAME. */ | |
191 | ||
192 | char * | |
193 | make_tempname (filename) | |
194 | char *filename; | |
195 | { | |
196 | static char template[] = "stXXXXXX"; | |
197 | char *tmpname; | |
198 | char *slash = strrchr (filename, '/'); | |
199 | ||
200 | #if defined (__DJGPP__) || defined (__GO32__) || defined (_WIN32) | |
201 | if (slash == NULL) | |
202 | slash = strrchr (filename, '\\'); | |
203 | #endif | |
204 | ||
205 | if (slash != (char *) NULL) | |
206 | { | |
207 | char c; | |
208 | ||
209 | c = *slash; | |
210 | *slash = 0; | |
211 | tmpname = xmalloc (strlen (filename) + sizeof (template) + 1); | |
212 | strcpy (tmpname, filename); | |
213 | strcat (tmpname, "/"); | |
214 | strcat (tmpname, template); | |
215 | mktemp (tmpname); | |
216 | *slash = c; | |
217 | } | |
218 | else | |
219 | { | |
220 | tmpname = xmalloc (sizeof (template)); | |
221 | strcpy (tmpname, template); | |
222 | mktemp (tmpname); | |
223 | } | |
224 | return tmpname; | |
225 | } | |
226 | ||
227 | /* Parse a string into a VMA, with a fatal error if it can't be | |
228 | parsed. */ | |
229 | ||
230 | bfd_vma | |
231 | parse_vma (s, arg) | |
232 | const char *s; | |
233 | const char *arg; | |
234 | { | |
235 | bfd_vma ret; | |
236 | const char *end; | |
237 | ||
238 | ret = bfd_scan_vma (s, &end, 0); | |
239 | if (*end != '\0') | |
240 | { | |
241 | fprintf (stderr, "%s: %s: bad number: %s\n", program_name, arg, s); | |
242 | exit (1); | |
243 | } | |
244 | return ret; | |
245 | } |