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