Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / contrib / binutils / binutils / bucomm.c
1 /* bucomm.c -- Bin Utils COMmon code.
2    Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001
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 #include "filenames.h"
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
38 \f
39 /* Error reporting */
40
41 char *program_name;
42
43 void
44 bfd_nonfatal (string)
45      CONST char *string;
46 {
47   CONST char *errmsg = bfd_errmsg (bfd_get_error ());
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)
57      CONST char *string;
58 {
59   bfd_nonfatal (string);
60   xexit (1);
61 }
62
63 void
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
73 void
74 fatal VPARAMS ((const char *format, ...))
75 {
76   VA_OPEN (args, format);
77   VA_FIXEDARG (args, const char *, format);
78
79   report (format, args);
80   VA_CLOSE (args);
81   xexit (1);
82 }
83
84 void
85 non_fatal VPARAMS ((const char *format, ...))
86 {
87   VA_OPEN (args, format);
88   VA_FIXEDARG (args, const char *, format);
89
90   report (format, args);
91   VA_CLOSE (args);
92 }
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
110 /* After a false return from bfd_check_format_matches with
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 {
131   extern const bfd_target *const *bfd_target_vector;
132   int t;
133
134   if (name == NULL)
135     fprintf (f, _("Supported targets:"));
136   else
137     fprintf (f, _("%s: supported targets:"), name);
138   for (t = 0; bfd_target_vector[t] != NULL; t++)
139     fprintf (f, " %s", bfd_target_vector[t]->name);
140   fprintf (f, "\n");
141 }
142
143 /* List the supported architectures.  */
144
145 void
146 list_supported_architectures (name, f)
147      const char *name;
148      FILE *f;
149 {
150   const char** arch;
151
152   if (name == NULL)
153     fprintf (f, _("Supported architectures:"));
154   else
155     fprintf (f, _("%s: supported architectures:"), name);
156
157   for (arch = bfd_arch_list (); *arch; arch++)
158     fprintf (f, " %s", *arch);
159   fprintf (f, "\n");
160 }
161 \f
162 /* Display the archive header for an element as if it were an ls -l listing:
163
164    Mode       User\tGroup\tSize\tDate               Name */
165
166 void
167 print_arelt_descr (file, abfd, verbose)
168      FILE *file;
169      bfd *abfd;
170      boolean verbose;
171 {
172   struct stat buf;
173
174   if (verbose)
175     {
176       if (bfd_stat_arch_elt (abfd, &buf) == 0)
177         {
178           char modebuf[11];
179           char timebuf[40];
180           time_t when = buf.st_mtime;
181           CONST char *ctime_result = (CONST char *) ctime (&when);
182
183           /* POSIX format:  skip weekday and seconds from ctime output.  */
184           sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
185
186           mode_string (buf.st_mode, modebuf);
187           modebuf[10] = '\0';
188           /* POSIX 1003.2/D11 says to skip first character (entry type).  */
189           fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1,
190                    (long) buf.st_uid, (long) buf.st_gid,
191                    (long) buf.st_size, timebuf);
192         }
193     }
194
195   fprintf (file, "%s\n", bfd_get_filename (abfd));
196 }
197
198 /* Return the name of a temporary file in the same directory as FILENAME.  */
199
200 char *
201 make_tempname (filename)
202      char *filename;
203 {
204   static char template[] = "stXXXXXX";
205   char *tmpname;
206   char *slash = strrchr (filename, '/');
207
208 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
209   {
210     /* We could have foo/bar\\baz, or foo\\bar, or d:bar.  */
211     char *bslash = strrchr (filename, '\\');
212     if (slash == NULL || (bslash != NULL && bslash > slash))
213       slash = bslash;
214     if (slash == NULL && filename[0] != '\0' && filename[1] == ':')
215       slash = filename + 1;
216   }
217 #endif
218
219   if (slash != (char *) NULL)
220     {
221       char c;
222
223       c = *slash;
224       *slash = 0;
225       tmpname = xmalloc (strlen (filename) + sizeof (template) + 2);
226       strcpy (tmpname, filename);
227 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
228       /* If tmpname is "X:", appending a slash will make it a root
229          directory on drive X, which is NOT the same as the current
230          directory on drive X.  */
231       if (tmpname[1] == ':' && tmpname[2] == '\0')
232         strcat (tmpname, ".");
233 #endif
234       strcat (tmpname, "/");
235       strcat (tmpname, template);
236       mktemp (tmpname);
237       *slash = c;
238     }
239   else
240     {
241       tmpname = xmalloc (sizeof (template));
242       strcpy (tmpname, template);
243       mktemp (tmpname);
244     }
245   return tmpname;
246 }
247
248 /* Parse a string into a VMA, with a fatal error if it can't be
249    parsed.  */
250
251 bfd_vma
252 parse_vma (s, arg)
253      const char *s;
254      const char *arg;
255 {
256   bfd_vma ret;
257   const char *end;
258
259   ret = bfd_scan_vma (s, &end, 0);
260   
261   if (*end != '\0')
262     fatal (_("%s: bad number: %s"), arg, s);
263
264   return ret;
265 }