]>
Commit | Line | Data |
---|---|---|
b886a6e3 JG |
1 | /* bucomm.c -- Bin Utils COMmon code. |
2 | Copyright (C) 1991 Free Software Foundation, Inc. | |
2fa0b342 | 3 | |
b886a6e3 JG |
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., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | /* We might put this in a library someday so it could be dynamically | |
2fa0b342 DHW |
21 | loaded, but for now it's not necessary */ |
22 | ||
2fa0b342 | 23 | #include "bfd.h" |
b886a6e3 | 24 | #include "sysdep.h" |
2fa0b342 DHW |
25 | #include <varargs.h> |
26 | ||
27 | char *target = NULL; /* default as late as possible */ | |
28 | ||
29 | /* Yes, this is what atexit is for, but that isn't guaranteed yet. | |
30 | And yes, I know this isn't as good, but it does what is needed just fine */ | |
31 | void (*exit_handler) (); | |
2fa0b342 | 32 | |
2fa0b342 | 33 | |
b886a6e3 JG |
34 | |
35 | ||
2fa0b342 DHW |
36 | /* Error reporting */ |
37 | ||
38 | char *program_name; | |
39 | ||
40 | void | |
b886a6e3 JG |
41 | DEFUN(bfd_fatal,(string), |
42 | char *string) | |
2fa0b342 | 43 | { |
b886a6e3 | 44 | const char *errmsg = bfd_errmsg (bfd_error); |
2fa0b342 DHW |
45 | |
46 | if (string) | |
47 | fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg); | |
48 | else | |
49 | fprintf (stderr, "%s: %s\n", program_name, errmsg); | |
50 | ||
51 | if (NULL != exit_handler) (*exit_handler) (); | |
52 | exit (1); | |
53 | } | |
54 | ||
b886a6e3 | 55 | #if 0 /* !defined(NO_STDARG) */ |
2fa0b342 DHW |
56 | void |
57 | fatal (Format) | |
58 | const char *Format; | |
59 | { | |
60 | va_list args; | |
61 | ||
62 | va_start (args, Format); | |
63 | vfprintf (stderr, Format, args); | |
64 | va_end (args); | |
65 | (void) putc ('\n', stderr); | |
66 | if (NULL != exit_handler) (*exit_handler) (); | |
67 | exit (1); | |
68 | } | |
69 | #else | |
2fa0b342 DHW |
70 | void fatal (va_alist) |
71 | va_dcl | |
72 | { | |
73 | char *Format; | |
74 | va_list args; | |
75 | ||
76 | va_start (args); | |
77 | Format = va_arg(args, char *); | |
78 | vfprintf (stderr, Format, args); | |
79 | va_end (args); | |
80 | (void) putc ('\n', stderr); | |
81 | if (NULL != exit_handler) (*exit_handler) (); | |
82 | exit (1); | |
83 | } /* fatal() */ | |
b886a6e3 | 84 | #endif |
2fa0b342 DHW |
85 | |
86 | \f | |
87 | /** Display the archive header for an element as if it were an ls -l listing */ | |
88 | ||
89 | /* Mode User\tGroup\tSize\tDate Name */ | |
90 | ||
91 | void | |
b886a6e3 JG |
92 | DEFUN(print_arelt_descr,(file, abfd, verbose), |
93 | FILE *file AND | |
94 | bfd *abfd AND | |
95 | boolean verbose) | |
2fa0b342 | 96 | { |
b886a6e3 | 97 | void mode_string (); |
2fa0b342 | 98 | struct stat buf; |
2fa0b342 DHW |
99 | |
100 | if (verbose) { | |
101 | ||
102 | if (bfd_stat_arch_elt (abfd, &buf) == 0) { /* if not, huh? */ | |
b886a6e3 JG |
103 | char modebuf[11]; |
104 | char timebuf[40]; | |
105 | long when = buf.st_mtime; | |
106 | CONST char *ctime_result = (CONST char *)ctime (&when); | |
107 | ||
108 | /* Posix format: skip weekday and seconds from ctime output. */ | |
109 | sprintf(timebuf, "%.12s %.4s", ctime_result+4, ctime_result+20); | |
2fa0b342 DHW |
110 | |
111 | mode_string (buf.st_mode, modebuf); | |
112 | modebuf[10] = '\0'; | |
b886a6e3 JG |
113 | /* Posix 1003.2/D11 says to skip first character (entry type). */ |
114 | fprintf (file, "%s %d/%d %6ld %s ", modebuf+1, buf.st_uid, buf.st_gid, buf.st_size, timebuf); | |
2fa0b342 DHW |
115 | } |
116 | } | |
117 | ||
b886a6e3 | 118 | fprintf (file, "%s\n",abfd->filename); |
2fa0b342 DHW |
119 | } |
120 | ||
121 | /* Like malloc but get fatal error if memory is exhausted. */ | |
122 | char * | |
123 | xmalloc (size) | |
124 | unsigned size; | |
125 | { | |
126 | register char *result = malloc (size); | |
b886a6e3 JG |
127 | if (result == (char *) NULL && size != 0) { |
128 | fatal ("virtual memory exhausted"); | |
129 | } | |
2fa0b342 DHW |
130 | |
131 | return result; | |
132 | } | |
133 | ||
134 | /* Like realloc but get fatal error if memory is exhausted. */ | |
135 | char * | |
136 | xrealloc (ptr, size) | |
137 | char *ptr; | |
138 | unsigned size; | |
139 | { | |
140 | register char *result = realloc (ptr, size); | |
b886a6e3 JG |
141 | if (result == 0 && size != 0) { |
142 | fatal ("virtual memory exhausted"); | |
143 | } | |
2fa0b342 DHW |
144 | |
145 | return result; | |
146 | } |