Commit 84f40464 authored by Alexandre Julliard's avatar Alexandre Julliard

winebuild: Copy the strmake function from winegcc to simplify string formatting.

parent a4704d3d
...@@ -199,6 +199,7 @@ extern void *xrealloc (void *ptr, size_t size); ...@@ -199,6 +199,7 @@ extern void *xrealloc (void *ptr, size_t size);
extern char *xstrdup( const char *str ); extern char *xstrdup( const char *str );
extern char *strupper(char *s); extern char *strupper(char *s);
extern int strendswith(const char* str, const char* end); extern int strendswith(const char* str, const char* end);
extern char *strmake(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2 )));
extern DECLSPEC_NORETURN void fatal_error( const char *msg, ... ) extern DECLSPEC_NORETURN void fatal_error( const char *msg, ... )
__attribute__ ((__format__ (__printf__, 1, 2))); __attribute__ ((__format__ (__printf__, 1, 2)));
extern DECLSPEC_NORETURN void fatal_perror( const char *msg, ... ) extern DECLSPEC_NORETURN void fatal_perror( const char *msg, ... )
......
...@@ -193,8 +193,7 @@ static char *try_library_path( const char *path, const char *name ) ...@@ -193,8 +193,7 @@ static char *try_library_path( const char *path, const char *name )
char *buffer; char *buffer;
int fd; int fd;
buffer = xmalloc( strlen(path) + strlen(name) + 9 ); buffer = strmake( "%s/lib%s.def", path, name );
sprintf( buffer, "%s/lib%s.def", path, name );
/* check if the file exists */ /* check if the file exists */
if ((fd = open( buffer, O_RDONLY )) != -1) if ((fd = open( buffer, O_RDONLY )) != -1)
...@@ -564,8 +563,7 @@ void read_undef_symbols( DLLSPEC *spec, char **argv ) ...@@ -564,8 +563,7 @@ void read_undef_symbols( DLLSPEC *spec, char **argv )
name = ldcombine_files( spec, argv ); name = ldcombine_files( spec, argv );
cmd = xmalloc( strlen(prog) + strlen(name) + 5 ); cmd = strmake( "%s -u %s", prog, name );
sprintf( cmd, "%s -u %s", prog, name );
if (!(f = popen( cmd, "r" ))) if (!(f = popen( cmd, "r" )))
fatal_error( "Cannot execute '%s'\n", cmd ); fatal_error( "Cannot execute '%s'\n", cmd );
...@@ -1188,9 +1186,9 @@ static void output_external_link_imports( DLLSPEC *spec ) ...@@ -1188,9 +1186,9 @@ static void output_external_link_imports( DLLSPEC *spec )
for (i = pos = 0; i < ext_link_imports.count; i++) for (i = pos = 0; i < ext_link_imports.count; i++)
{ {
char buffer[256]; char *buffer = strmake( "__wine_spec_ext_link_%s", ext_link_imports.names[i] );
sprintf( buffer, "__wine_spec_ext_link_%s", ext_link_imports.names[i] );
output_import_thunk( buffer, ".L__wine_spec_external_links", pos ); output_import_thunk( buffer, ".L__wine_spec_external_links", pos );
free( buffer );
pos += get_ptr_size(); pos += get_ptr_size();
} }
output_function_size( "__wine_spec_external_link_thunks" ); output_function_size( "__wine_spec_external_link_thunks" );
......
...@@ -195,14 +195,15 @@ static void output_resident_name( const char *string, int ordinal ) ...@@ -195,14 +195,15 @@ static void output_resident_name( const char *string, int ordinal )
*/ */
static const char *get_callfrom16_name( const ORDDEF *odp ) static const char *get_callfrom16_name( const ORDDEF *odp )
{ {
static char buffer[80]; static char *buffer;
sprintf( buffer, "%s_%s_%s", free( buffer );
(odp->type == TYPE_PASCAL) ? "p" : buffer = strmake( "%s_%s_%s",
(odp->type == TYPE_VARARGS) ? "v" : "c", (odp->type == TYPE_PASCAL) ? "p" :
(odp->flags & FLAG_REGISTER) ? "regs" : (odp->type == TYPE_VARARGS) ? "v" : "c",
(odp->flags & FLAG_RET16) ? "word" : "long", (odp->flags & FLAG_REGISTER) ? "regs" :
odp->u.func.arg_types ); (odp->flags & FLAG_RET16) ? "word" : "long",
odp->u.func.arg_types );
return buffer; return buffer;
} }
...@@ -301,13 +302,13 @@ static int get_function_argsize( const ORDDEF *odp ) ...@@ -301,13 +302,13 @@ static int get_function_argsize( const ORDDEF *odp )
*/ */
static void output_call16_function( ORDDEF *odp ) static void output_call16_function( ORDDEF *odp )
{ {
char name[256]; char *name;
int i, pos, stack_words; int i, pos, stack_words;
const char *args = odp->u.func.arg_types; const char *args = odp->u.func.arg_types;
int argsize = get_function_argsize( odp ); int argsize = get_function_argsize( odp );
int needs_ldt = strchr( args, 'p' ) || strchr( args, 't' ); int needs_ldt = strchr( args, 'p' ) || strchr( args, 't' );
sprintf( name, ".L__wine_spec_call16_%s", get_relay_name(odp) ); name = strmake( ".L__wine_spec_call16_%s", get_relay_name(odp) );
output( "\t.align %d\n", get_alignment(4) ); output( "\t.align %d\n", get_alignment(4) );
output( "\t%s\n", func_declaration(name) ); output( "\t%s\n", func_declaration(name) );
...@@ -406,6 +407,7 @@ static void output_call16_function( ORDDEF *odp ) ...@@ -406,6 +407,7 @@ static void output_call16_function( ORDDEF *odp )
output( "\tret\n" ); output( "\tret\n" );
output_cfi( ".cfi_endproc" ); output_cfi( ".cfi_endproc" );
output_function_size( name ); output_function_size( name );
free( name );
} }
......
...@@ -119,6 +119,25 @@ int strendswith(const char* str, const char* end) ...@@ -119,6 +119,25 @@ int strendswith(const char* str, const char* end)
return l >= m && strcmp(str + l - m, end) == 0; return l >= m && strcmp(str + l - m, end) == 0;
} }
char *strmake( const char* fmt, ... )
{
int n;
size_t size = 100;
va_list ap;
for (;;)
{
char *p = xmalloc( size );
va_start( ap, fmt );
n = vsnprintf( p, size, fmt, ap );
va_end( ap );
if (n == -1) size *= 2;
else if ((size_t)n >= size) size = n + 1;
else return p;
free( p );
}
}
void fatal_error( const char *msg, ... ) void fatal_error( const char *msg, ... )
{ {
va_list valist; va_list valist;
...@@ -210,14 +229,7 @@ char *find_tool( const char *name, const char * const *names ) ...@@ -210,14 +229,7 @@ char *find_tool( const char *name, const char * const *names )
unsigned int i, len; unsigned int i, len;
struct stat st; struct stat st;
if (target_alias) if (target_alias) return strmake( "%s-%s", target_alias, name );
{
file = xmalloc( strlen(target_alias) + strlen(name) + 2 );
strcpy( file, target_alias );
strcat( file, "-" );
strcat( file, name );
return file;
}
if (!dirs) if (!dirs)
{ {
...@@ -548,10 +560,7 @@ FILE *open_input_file( const char *srcdir, const char *name ) ...@@ -548,10 +560,7 @@ FILE *open_input_file( const char *srcdir, const char *name )
if (!file && srcdir) if (!file && srcdir)
{ {
fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 ); fullname = strmake( "%s/%s", srcdir, name );
strcpy( fullname, srcdir );
strcat( fullname, "/" );
strcat( fullname, name );
file = fopen( fullname, "r" ); file = fopen( fullname, "r" );
} }
else fullname = xstrdup( name ); else fullname = xstrdup( name );
...@@ -708,16 +717,19 @@ const char *make_c_identifier( const char *str ) ...@@ -708,16 +717,19 @@ const char *make_c_identifier( const char *str )
*/ */
const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec ) const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
{ {
static char buffer[256]; static char *buffer;
free( buffer );
if (odp->name || odp->export_name) if (odp->name || odp->export_name)
{ {
char *p; char *p;
sprintf( buffer, "__wine_stub_%s", odp->name ? odp->name : odp->export_name ); buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
/* make sure name is a legal C identifier */ /* make sure name is a legal C identifier */
for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break; for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
if (!*p) return buffer; if (!*p) return buffer;
free( buffer );
} }
sprintf( buffer, "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal ); buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
return buffer; return buffer;
} }
...@@ -819,15 +831,15 @@ unsigned int get_ptr_size(void) ...@@ -819,15 +831,15 @@ unsigned int get_ptr_size(void)
/* return the assembly name for a C symbol */ /* return the assembly name for a C symbol */
const char *asm_name( const char *sym ) const char *asm_name( const char *sym )
{ {
static char buffer[256]; static char *buffer;
switch (target_platform) switch (target_platform)
{ {
case PLATFORM_APPLE: case PLATFORM_APPLE:
case PLATFORM_WINDOWS: case PLATFORM_WINDOWS:
if (sym[0] == '.' && sym[1] == 'L') return sym; if (sym[0] == '.' && sym[1] == 'L') return sym;
buffer[0] = '_'; free( buffer );
strcpy( buffer + 1, sym ); buffer = strmake( "_%s", sym );
return buffer; return buffer;
default: default:
return sym; return sym;
...@@ -837,23 +849,25 @@ const char *asm_name( const char *sym ) ...@@ -837,23 +849,25 @@ const char *asm_name( const char *sym )
/* return an assembly function declaration for a C function name */ /* return an assembly function declaration for a C function name */
const char *func_declaration( const char *func ) const char *func_declaration( const char *func )
{ {
static char buffer[256]; static char *buffer;
switch (target_platform) switch (target_platform)
{ {
case PLATFORM_APPLE: case PLATFORM_APPLE:
return ""; return "";
case PLATFORM_WINDOWS: case PLATFORM_WINDOWS:
sprintf( buffer, ".def _%s; .scl 2; .type 32; .endef", func ); free( buffer );
buffer = strmake( ".def _%s; .scl 2; .type 32; .endef", func );
break; break;
default: default:
free( buffer );
switch(target_cpu) switch(target_cpu)
{ {
case CPU_ARM: case CPU_ARM:
sprintf( buffer, ".type %s,%%function", func ); buffer = strmake( ".type %s,%%function", func );
break; break;
default: default:
sprintf( buffer, ".type %s,@function", func ); buffer = strmake( ".type %s,@function", func );
break; break;
} }
break; break;
...@@ -913,20 +927,22 @@ void output_gnu_stack_note(void) ...@@ -913,20 +927,22 @@ void output_gnu_stack_note(void)
/* return a global symbol declaration for an assembly symbol */ /* return a global symbol declaration for an assembly symbol */
const char *asm_globl( const char *func ) const char *asm_globl( const char *func )
{ {
static char buffer[256]; static char *buffer;
free( buffer );
switch (target_platform) switch (target_platform)
{ {
case PLATFORM_APPLE: case PLATFORM_APPLE:
sprintf( buffer, "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func ); buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
return buffer; break;
case PLATFORM_WINDOWS: case PLATFORM_WINDOWS:
sprintf( buffer, "\t.globl _%s\n_%s:", func, func ); buffer = strmake( "\t.globl _%s\n_%s:", func, func );
return buffer; break;
default: default:
sprintf( buffer, "\t.globl %s\n\t.hidden %s\n%s:", func, func, func ); buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
return buffer; break;
} }
return buffer;
} }
const char *get_asm_ptr_keyword(void) const char *get_asm_ptr_keyword(void)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment