Implement EnumPrinterDataEx{A|W}.
[wine.git] / tools / winebuild / utils.c
blob93944e7b5286097960722da548460996c607a8f2
1 /* small utility functions for winebuild */
3 #include <ctype.h>
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
10 #include "build.h"
12 void *xmalloc (size_t size)
14 void *res;
16 res = malloc (size ? size : 1);
17 if (res == NULL)
19 fprintf (stderr, "Virtual memory exhausted.\n");
20 exit (1);
22 return res;
25 void *xrealloc (void *ptr, size_t size)
27 void *res = realloc (ptr, size);
28 if (res == NULL)
30 fprintf (stderr, "Virtual memory exhausted.\n");
31 exit (1);
33 return res;
36 char *xstrdup( const char *str )
38 char *res = strdup( str );
39 if (!res)
41 fprintf (stderr, "Virtual memory exhausted.\n");
42 exit (1);
44 return res;
47 char *strupper(char *s)
49 char *p;
50 for (p = s; *p; p++) *p = toupper(*p);
51 return s;
54 void fatal_error( const char *msg, ... )
56 va_list valist;
57 va_start( valist, msg );
58 if (input_file_name)
60 fprintf( stderr, "%s:", input_file_name );
61 if (current_line)
62 fprintf( stderr, "%d:", current_line );
63 fputc( ' ', stderr );
65 vfprintf( stderr, msg, valist );
66 va_end( valist );
67 exit(1);
70 void fatal_perror( const char *msg, ... )
72 va_list valist;
73 va_start( valist, msg );
74 if (input_file_name)
76 fprintf( stderr, "%s:", input_file_name );
77 if (current_line)
78 fprintf( stderr, "%d:", current_line );
79 fputc( ' ', stderr );
81 vfprintf( stderr, msg, valist );
82 perror( " " );
83 va_end( valist );
84 exit(1);
87 void warning( const char *msg, ... )
89 va_list valist;
90 va_start( valist, msg );
91 if (input_file_name)
93 fprintf( stderr, "%s:", input_file_name );
94 if (current_line)
95 fprintf( stderr, "%d:", current_line );
96 fputc( ' ', stderr );
98 fprintf( stderr, "warning: " );
99 vfprintf( stderr, msg, valist );
100 va_end( valist );
103 /* dump a byte stream into the assembly code */
104 void dump_bytes( FILE *outfile, const unsigned char *data, int len,
105 const char *label, int constant )
107 int i;
109 fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
110 constant ? "const " : "", label, len );
111 for (i = 0; i < len; i++)
113 if (!(i & 7)) fprintf( outfile, "\n " );
114 fprintf( outfile, "0x%02x", *data++ );
115 if (i < len - 1) fprintf( outfile, "," );
117 fprintf( outfile, "\n};\n" );
120 /*****************************************************************
121 * Function: get_alignment
123 * Description:
124 * According to the info page for gas, the .align directive behaves
125 * differently on different systems. On some architectures, the
126 * argument of a .align directive is the number of bytes to pad to, so
127 * to align on an 8-byte boundary you'd say
128 * .align 8
129 * On other systems, the argument is "the number of low-order zero bits
130 * that the location counter must have after advancement." So to
131 * align on an 8-byte boundary you'd say
132 * .align 3
134 * The reason gas is written this way is that it's trying to mimick
135 * native assemblers for the various architectures it runs on. gas
136 * provides other directives that work consistantly across
137 * architectures, but of course we want to work on all arches with or
138 * without gas. Hence this function.
141 * Parameters:
142 * alignBoundary -- the number of bytes to align to.
143 * If we're on an architecture where
144 * the assembler requires a 'number
145 * of low-order zero bits' as a
146 * .align argument, then this number
147 * must be a power of 2.
150 int get_alignment(int alignBoundary)
152 #ifdef __PPC__
154 int n = 0;
156 switch(alignBoundary)
158 case 2:
159 n = 1;
160 break;
161 case 4:
162 n = 2;
163 break;
164 case 8:
165 n = 3;
166 break;
167 case 16:
168 n = 4;
169 break;
170 case 32:
171 n = 5;
172 break;
173 case 64:
174 n = 6;
175 break;
176 case 128:
177 n = 7;
178 break;
179 case 256:
180 n = 8;
181 break;
182 case 512:
183 n = 9;
184 break;
185 case 1024:
186 n = 10;
187 break;
188 case 2048:
189 n = 11;
190 break;
191 case 4096:
192 n = 12;
193 break;
194 case 8192:
195 n = 13;
196 break;
197 case 16384:
198 n = 14;
199 break;
200 case 32768:
201 n = 15;
202 break;
203 case 65536:
204 n = 16;
205 break;
206 default:
207 fatal_error("Alignment to %d-byte boundary not supported on this architecture.\n",
208 alignBoundary);
210 return n;
212 #elif defined(__i386__) || defined(__sparc__)
214 return alignBoundary;
216 #else
217 #error "How does the '.align' assembler directive work on your architecture?"
218 #endif