00001 /* bucomm.c -- Bin Utils COMmon code. 00002 Copyright (C) 1991, 92, 93, 94, 95, 1997 Free Software Foundation, Inc. 00003 00004 This file is part of GNU Binutils. 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00019 02111-1307, USA. */ 00020 00021 /* We might put this in a library someday so it could be dynamically 00022 loaded, but for now it's not necessary. */ 00023 00024 #include <bfd.h> 00025 #include <libiberty.h> 00026 #include "bucomm.h" 00027 00028 #include <sys/stat.h> 00029 #include <time.h> /* ctime, maybe time_t */ 00030 00031 #ifdef ANSI_PROTOTYPES 00032 #include <stdarg.h> 00033 #else 00034 #include <varargs.h> 00035 #endif 00036 00037 /* Error reporting */ 00038 00039 char *program_name; 00040 00041 void 00042 bfd_nonfatal (string) 00043 CONST char *string; 00044 { 00045 CONST char *errmsg = bfd_errmsg (bfd_get_error ()); 00046 00047 if (string) 00048 fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg); 00049 else 00050 fprintf (stderr, "%s: %s\n", program_name, errmsg); 00051 } 00052 00053 void 00054 bfd_fatal (string) 00055 CONST char *string; 00056 { 00057 bfd_nonfatal (string); 00058 xexit (1); 00059 } 00060 00061 #ifdef ANSI_PROTOTYPES 00062 void 00063 fatal (const char *format, ...) 00064 { 00065 va_list args; 00066 00067 fprintf (stderr, "%s: ", program_name); 00068 va_start (args, format); 00069 vfprintf (stderr, format, args); 00070 va_end (args); 00071 putc ('\n', stderr); 00072 xexit (1); 00073 } 00074 #else 00075 void 00076 fatal (va_alist) 00077 va_dcl 00078 { 00079 char *Format; 00080 va_list args; 00081 00082 fprintf (stderr, "%s: ", program_name); 00083 va_start (args); 00084 Format = va_arg (args, char *); 00085 vfprintf (stderr, Format, args); 00086 va_end (args); 00087 putc ('\n', stderr); 00088 xexit (1); 00089 } 00090 #endif 00091 00092 /* Set the default BFD target based on the configured target. Doing 00093 this permits the binutils to be configured for a particular target, 00094 and linked against a shared BFD library which was configured for a 00095 different target. */ 00096 00097 #define TARGET "elf32-i386" /* FIXME: hard-coded! */ 00098 void 00099 set_default_bfd_target () 00100 { 00101 /* The macro TARGET is defined by Makefile. */ 00102 const char *target = TARGET; 00103 00104 if (! bfd_set_default_target (target)) 00105 { 00106 char *errmsg; 00107 00108 errmsg = (char *) xmalloc (100 + strlen (target)); 00109 sprintf (errmsg, "can't set BFD default target to `%s'", target); 00110 bfd_fatal (errmsg); 00111 } 00112 } 00113 00114 /* After a false return from bfd_check_format_matches with 00115 bfd_get_error () == bfd_error_file_ambiguously_recognized, print 00116 the possible matching targets. */ 00117 00118 void 00119 list_matching_formats (p) 00120 char **p; 00121 { 00122 fprintf(stderr, "%s: Matching formats:", program_name); 00123 while (*p) 00124 fprintf(stderr, " %s", *p++); 00125 fprintf(stderr, "\n"); 00126 } 00127 00128 /* List the supported targets. */ 00129 00130 void 00131 list_supported_targets (name, f) 00132 const char *name; 00133 FILE *f; 00134 { 00135 extern bfd_target *bfd_target_vector[]; 00136 int t; 00137 00138 if (name == NULL) 00139 fprintf (f, "Supported targets:"); 00140 else 00141 fprintf (f, "%s: supported targets:", name); 00142 for (t = 0; bfd_target_vector[t] != NULL; t++) 00143 fprintf (f, " %s", bfd_target_vector[t]->name); 00144 fprintf (f, "\n"); 00145 } 00146 00147 /* Display the archive header for an element as if it were an ls -l listing: 00148 00149 Mode User\tGroup\tSize\tDate Name */ 00150 00151 void 00152 print_arelt_descr (file, abfd, verbose) 00153 FILE *file; 00154 bfd *abfd; 00155 boolean verbose; 00156 { 00157 struct stat buf; 00158 00159 if (verbose) 00160 { 00161 if (bfd_stat_arch_elt (abfd, &buf) == 0) 00162 { 00163 char modebuf[11]; 00164 char timebuf[40]; 00165 time_t when = buf.st_mtime; 00166 CONST char *ctime_result = (CONST char *) ctime (&when); 00167 00168 /* POSIX format: skip weekday and seconds from ctime output. */ 00169 sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20); 00170 00171 mode_string (buf.st_mode, modebuf); 00172 modebuf[10] = '\0'; 00173 /* POSIX 1003.2/D11 says to skip first character (entry type). */ 00174 fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1, 00175 (long) buf.st_uid, (long) buf.st_gid, 00176 (long) buf.st_size, timebuf); 00177 } 00178 } 00179 00180 fprintf (file, "%s\n", bfd_get_filename (abfd)); 00181 } 00182 00183 /* Return the name of a temporary file in the same directory as FILENAME. */ 00184 00185 char * 00186 make_tempname (filename) 00187 char *filename; 00188 { 00189 static char template[] = "stXXXXXX"; 00190 char *tmpname; 00191 char *slash = strrchr (filename, '/'); 00192 00193 #if defined (__DJGPP__) || defined (__GO32__) || defined (_WIN32) 00194 if (slash == NULL) 00195 slash = strrchr (filename, '\\'); 00196 #endif 00197 00198 if (slash != (char *) NULL) 00199 { 00200 char c; 00201 00202 c = *slash; 00203 *slash = 0; 00204 tmpname = xmalloc (strlen (filename) + sizeof (template) + 1); 00205 strcpy (tmpname, filename); 00206 strcat (tmpname, "/"); 00207 strcat (tmpname, template); 00208 mkstemp (tmpname); 00209 *slash = c; 00210 } 00211 else 00212 { 00213 tmpname = xmalloc (sizeof (template)); 00214 strcpy (tmpname, template); 00215 mkstemp (tmpname); 00216 } 00217 return tmpname; 00218 } 00219 00220 /* Parse a string into a VMA, with a fatal error if it can't be 00221 parsed. */ 00222 00223 bfd_vma 00224 parse_vma (s, arg) 00225 const char *s; 00226 const char *arg; 00227 { 00228 bfd_vma ret; 00229 const char *end; 00230 00231 ret = bfd_scan_vma (s, &end, 0); 00232 if (*end != '\0') 00233 { 00234 fprintf (stderr, "%s: %s: bad number: %s\n", program_name, arg, s); 00235 exit (1); 00236 } 00237 return ret; 00238 }
1.4.7

