This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / binutils / bucomm.c
1 /*** bucomm.c -- Bin Utils COMmon code.
2
3      We might put this in a library someday so it could be dynamically
4      loaded, but for now it's not necessary */
5
6 #include "sysdep.h"
7 #include "bfd.h"
8 #include <varargs.h>
9
10 char *target = NULL;            /* default as late as possible */
11
12 /* Yes, this is what atexit is for, but that isn't guaranteed yet.
13    And yes, I know this isn't as good, but it does what is needed just fine */
14 void (*exit_handler) ();
15 \f
16 /** Memory hackery */
17
18 PROTO (char *, malloc, (unsigned size));
19 PROTO (char *, realloc, (char *ptr, unsigned size));
20
21 \f
22 /* Error reporting */
23
24 char *program_name;
25
26 void
27 bfd_fatal (string)
28      char *string;
29 {
30   char *errmsg =  bfd_errmsg (bfd_error);
31   
32   if (string)
33     fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
34   else
35     fprintf (stderr, "%s: %s\n", program_name, errmsg);
36
37   if (NULL != exit_handler) (*exit_handler) ();
38   exit (1);
39 }
40
41 #ifndef NO_STDARG
42 void
43 fatal (Format)
44      const char *Format;
45 {
46   va_list args;
47         
48   va_start (args, Format);
49   vfprintf (stderr, Format, args);
50   va_end (args);
51   (void) putc ('\n', stderr);
52   if (NULL != exit_handler) (*exit_handler) ();
53   exit (1);
54 }
55 #else
56 #ifndef NO_VARARGS
57 void fatal (va_alist)
58      va_dcl
59 {
60         char *Format;
61         va_list args;
62         
63         va_start (args);
64         Format = va_arg(args, char *);
65         vfprintf (stderr, Format, args);
66         va_end (args);
67         (void) putc ('\n', stderr);
68         if (NULL != exit_handler) (*exit_handler) ();
69         exit (1);
70 } /* fatal() */
71 #else
72 /*VARARGS1 */
73 fatal (Format, args)
74      char *Format;
75 {
76   as_where ();
77   _doprnt (Format, &args, stderr); /* not terribly portable, but... */
78   (void) putc ('\n', stderr);
79   if (NULL != exit_handler) (*exit_handler) ();
80   exit (1);
81 }
82 #endif /* not NO_VARARGS */
83 #endif /* not NO_STDARG */
84
85 \f
86 /** Display the archive header for an element as if it were an ls -l listing */
87
88 /* Mode       User\tGroup\tSize\tDate               Name */
89
90 void
91 print_arelt_descr (abfd, verbose)
92      bfd *abfd;
93      boolean verbose;
94 {
95   struct stat buf;
96   char modebuf[11];
97   char timebuf[40];
98   long when;
99   long current_time = time ((long *) 0);
100
101   if (verbose) {
102
103     if (bfd_stat_arch_elt (abfd, &buf) == 0) { /* if not, huh? */
104
105       mode_string (buf.st_mode, modebuf);
106       modebuf[10] = '\0';
107       fputs (modebuf, stdout);
108
109       when = buf.st_mtime;
110       strcpy (timebuf, ctime (&when));
111
112       /* This code comes from gnu ls.  */
113       if ((current_time - when > 6 * 30 * 24 * 60 * 60)
114           || (current_time - when < 0)) {
115         /* The file is fairly old or in the future.
116            POSIX says the cutoff is 6 months old;
117            approximate this by 6*30 days.
118            Show the year instead of the time of day.  */
119         strcpy (timebuf + 11, timebuf + 19);
120       }
121       timebuf[16] = 0;
122
123       printf (" %d\t%d\t%ld\t%s ", buf.st_uid, buf.st_gid, buf.st_size, timebuf);
124     }
125   }
126
127   puts (abfd->filename);
128 }
129
130 /* Like malloc but get fatal error if memory is exhausted.  */
131 char *
132 xmalloc (size)
133      unsigned size;
134 {
135   register char *result = malloc (size);
136   if (result == NULL && size != NULL) fatal ("virtual memory exhausted");
137
138   return result;
139 }
140
141 /* Like realloc but get fatal error if memory is exhausted.  */
142 char *
143 xrealloc (ptr, size)
144      char *ptr;
145      unsigned size;
146 {
147   register char *result = realloc (ptr, size);
148   if (result == 0 && size != 0) fatal ("virtual memory exhausted");
149
150   return result;
151 }