extend comment.
[deliverable/binutils-gdb.git] / binutils / bucomm.c
1 /* bucomm.c -- Bin Utils COMmon code.
2 Copyright (C) 1991, 92, 93, 94, 95, 1997, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU Binutils.
5
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.
10
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.
15
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
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
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. */
23
24 #include "bfd.h"
25 #include "libiberty.h"
26 #include "bucomm.h"
27
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
37 #ifdef ANSI_PROTOTYPES
38 #include <stdarg.h>
39 #else
40 #include <varargs.h>
41 #endif
42 \f
43 /* Error reporting */
44
45 char *program_name;
46
47 void
48 bfd_nonfatal (string)
49 CONST char *string;
50 {
51 CONST char *errmsg = bfd_errmsg (bfd_get_error ());
52
53 if (string)
54 fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
55 else
56 fprintf (stderr, "%s: %s\n", program_name, errmsg);
57 }
58
59 void
60 bfd_fatal (string)
61 CONST char *string;
62 {
63 bfd_nonfatal (string);
64 xexit (1);
65 }
66
67 static void
68 report (format, args)
69 const char * format;
70 va_list args;
71 {
72 fprintf (stderr, "%s: ", program_name);
73 vfprintf (stderr, format, args);
74 putc ('\n', stderr);
75 }
76
77 #ifdef ANSI_PROTOTYPES
78 void
79 fatal (const char *format, ...)
80 {
81 va_list args;
82
83 va_start (args, format);
84 report (format, args);
85 va_end (args);
86 xexit (1);
87 }
88
89 void
90 non_fatal (const char *format, ...)
91 {
92 va_list args;
93
94 va_start (args, format);
95 report (format, args);
96 va_end (args);
97 }
98 #else
99 void
100 fatal (va_alist)
101 va_dcl
102 {
103 char *Format;
104 va_list args;
105
106 va_start (args);
107 Format = va_arg (args, char *);
108 report (Format, args);
109 va_end (args);
110 xexit (1);
111 }
112
113 void
114 non_fatal (va_alist)
115 va_dcl
116 {
117 char *Format;
118 va_list args;
119
120 va_start (args);
121 Format = va_arg (args, char *);
122 report (Format, args);
123 va_end (args);
124 }
125 #endif
126
127 /* Set the default BFD target based on the configured target. Doing
128 this permits the binutils to be configured for a particular target,
129 and linked against a shared BFD library which was configured for a
130 different target. */
131
132 void
133 set_default_bfd_target ()
134 {
135 /* The macro TARGET is defined by Makefile. */
136 const char *target = TARGET;
137
138 if (! bfd_set_default_target (target))
139 fatal (_("can't set BFD default target to `%s': %s"),
140 target, bfd_errmsg (bfd_get_error ()));
141 }
142
143 /* After a false return from bfd_check_format_matches with
144 bfd_get_error () == bfd_error_file_ambiguously_recognized, print
145 the possible matching targets. */
146
147 void
148 list_matching_formats (p)
149 char **p;
150 {
151 fprintf (stderr, _("%s: Matching formats:"), program_name);
152 while (*p)
153 fprintf (stderr, " %s", *p++);
154 fputc ('\n', stderr);
155 }
156
157 /* List the supported targets. */
158
159 void
160 list_supported_targets (name, f)
161 const char *name;
162 FILE *f;
163 {
164 extern bfd_target *bfd_target_vector[];
165 int t;
166
167 if (name == NULL)
168 fprintf (f, _("Supported targets:"));
169 else
170 fprintf (f, _("%s: supported targets:"), name);
171 for (t = 0; bfd_target_vector[t] != NULL; t++)
172 fprintf (f, " %s", bfd_target_vector[t]->name);
173 fprintf (f, "\n");
174 }
175 \f
176 /* Display the archive header for an element as if it were an ls -l listing:
177
178 Mode User\tGroup\tSize\tDate Name */
179
180 void
181 print_arelt_descr (file, abfd, verbose)
182 FILE *file;
183 bfd *abfd;
184 boolean verbose;
185 {
186 struct stat buf;
187
188 if (verbose)
189 {
190 if (bfd_stat_arch_elt (abfd, &buf) == 0)
191 {
192 char modebuf[11];
193 char timebuf[40];
194 time_t when = buf.st_mtime;
195 CONST char *ctime_result = (CONST char *) ctime (&when);
196
197 /* POSIX format: skip weekday and seconds from ctime output. */
198 sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
199
200 mode_string (buf.st_mode, modebuf);
201 modebuf[10] = '\0';
202 /* POSIX 1003.2/D11 says to skip first character (entry type). */
203 fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1,
204 (long) buf.st_uid, (long) buf.st_gid,
205 (long) buf.st_size, timebuf);
206 }
207 }
208
209 fprintf (file, "%s\n", bfd_get_filename (abfd));
210 }
211
212 /* Return the name of a temporary file in the same directory as FILENAME. */
213
214 char *
215 make_tempname (filename)
216 char *filename;
217 {
218 static char template[] = "stXXXXXX";
219 char *tmpname;
220 char *slash = strrchr (filename, '/');
221
222 #if defined (__DJGPP__) || defined (__GO32__) || defined (_WIN32)
223 if (slash == NULL)
224 slash = strrchr (filename, '\\');
225 #endif
226
227 if (slash != (char *) NULL)
228 {
229 char c;
230
231 c = *slash;
232 *slash = 0;
233 tmpname = xmalloc (strlen (filename) + sizeof (template) + 1);
234 strcpy (tmpname, filename);
235 strcat (tmpname, "/");
236 strcat (tmpname, template);
237 mktemp (tmpname);
238 *slash = c;
239 }
240 else
241 {
242 tmpname = xmalloc (sizeof (template));
243 strcpy (tmpname, template);
244 mktemp (tmpname);
245 }
246 return tmpname;
247 }
248
249 /* Parse a string into a VMA, with a fatal error if it can't be
250 parsed. */
251
252 bfd_vma
253 parse_vma (s, arg)
254 const char *s;
255 const char *arg;
256 {
257 bfd_vma ret;
258 const char *end;
259
260 ret = bfd_scan_vma (s, &end, 0);
261
262 if (*end != '\0')
263 fatal (_("%s: bad number: %s"), arg, s);
264
265 return ret;
266 }
This page took 0.035794 seconds and 4 git commands to generate.