Created regedit replacement. Fixed some bugs.
[wine/testsucceed.git] / tools / winebuild / utils.c
blob5f8cfa68bca1429e8cce79f19b5c046842cf816a
1 /*
2 * Small utility functions for winebuild
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "build.h"
31 void *xmalloc (size_t size)
33 void *res;
35 res = malloc (size ? size : 1);
36 if (res == NULL)
38 fprintf (stderr, "Virtual memory exhausted.\n");
39 exit (1);
41 return res;
44 void *xrealloc (void *ptr, size_t size)
46 void *res = realloc (ptr, size);
47 if (res == NULL)
49 fprintf (stderr, "Virtual memory exhausted.\n");
50 exit (1);
52 return res;
55 char *xstrdup( const char *str )
57 char *res = strdup( str );
58 if (!res)
60 fprintf (stderr, "Virtual memory exhausted.\n");
61 exit (1);
63 return res;
66 char *strupper(char *s)
68 char *p;
69 for (p = s; *p; p++) *p = toupper(*p);
70 return s;
73 void fatal_error( const char *msg, ... )
75 va_list valist;
76 va_start( valist, msg );
77 if (input_file_name)
79 fprintf( stderr, "%s:", input_file_name );
80 if (current_line)
81 fprintf( stderr, "%d:", current_line );
82 fputc( ' ', stderr );
84 vfprintf( stderr, msg, valist );
85 va_end( valist );
86 exit(1);
89 void fatal_perror( const char *msg, ... )
91 va_list valist;
92 va_start( valist, msg );
93 if (input_file_name)
95 fprintf( stderr, "%s:", input_file_name );
96 if (current_line)
97 fprintf( stderr, "%d:", current_line );
98 fputc( ' ', stderr );
100 vfprintf( stderr, msg, valist );
101 perror( " " );
102 va_end( valist );
103 exit(1);
106 void warning( const char *msg, ... )
108 va_list valist;
110 if (!display_warnings) return;
111 va_start( valist, msg );
112 if (input_file_name)
114 fprintf( stderr, "%s:", input_file_name );
115 if (current_line)
116 fprintf( stderr, "%d:", current_line );
117 fputc( ' ', stderr );
119 fprintf( stderr, "warning: " );
120 vfprintf( stderr, msg, valist );
121 va_end( valist );
124 /* output a standard header for generated files */
125 void output_standard_file_header( FILE *outfile )
127 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n",
128 input_file_name );
129 fprintf( outfile,
130 "/* This file can be copied, modified and distributed without restriction. */\n\n" );
133 /* dump a byte stream into the assembly code */
134 void dump_bytes( FILE *outfile, const unsigned char *data, int len,
135 const char *label, int constant )
137 int i;
139 fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
140 constant ? "const " : "", label, len );
141 for (i = 0; i < len; i++)
143 if (!(i & 7)) fprintf( outfile, "\n " );
144 fprintf( outfile, "0x%02x", *data++ );
145 if (i < len - 1) fprintf( outfile, "," );
147 fprintf( outfile, "\n};\n" );
150 /*****************************************************************
151 * Function: get_alignment
153 * Description:
154 * According to the info page for gas, the .align directive behaves
155 * differently on different systems. On some architectures, the
156 * argument of a .align directive is the number of bytes to pad to, so
157 * to align on an 8-byte boundary you'd say
158 * .align 8
159 * On other systems, the argument is "the number of low-order zero bits
160 * that the location counter must have after advancement." So to
161 * align on an 8-byte boundary you'd say
162 * .align 3
164 * The reason gas is written this way is that it's trying to mimick
165 * native assemblers for the various architectures it runs on. gas
166 * provides other directives that work consistantly across
167 * architectures, but of course we want to work on all arches with or
168 * without gas. Hence this function.
171 * Parameters:
172 * alignBoundary -- the number of bytes to align to.
173 * If we're on an architecture where
174 * the assembler requires a 'number
175 * of low-order zero bits' as a
176 * .align argument, then this number
177 * must be a power of 2.
180 int get_alignment(int alignBoundary)
182 #ifdef __PPC__
184 int n = 0;
186 switch(alignBoundary)
188 case 2:
189 n = 1;
190 break;
191 case 4:
192 n = 2;
193 break;
194 case 8:
195 n = 3;
196 break;
197 case 16:
198 n = 4;
199 break;
200 case 32:
201 n = 5;
202 break;
203 case 64:
204 n = 6;
205 break;
206 case 128:
207 n = 7;
208 break;
209 case 256:
210 n = 8;
211 break;
212 case 512:
213 n = 9;
214 break;
215 case 1024:
216 n = 10;
217 break;
218 case 2048:
219 n = 11;
220 break;
221 case 4096:
222 n = 12;
223 break;
224 case 8192:
225 n = 13;
226 break;
227 case 16384:
228 n = 14;
229 break;
230 case 32768:
231 n = 15;
232 break;
233 case 65536:
234 n = 16;
235 break;
236 default:
237 fatal_error("Alignment to %d-byte boundary not supported on this architecture.\n",
238 alignBoundary);
240 return n;
242 #elif defined(__i386__) || defined(__sparc__)
244 return alignBoundary;
246 #else
247 #error "How does the '.align' assembler directive work on your architecture?"
248 #endif