explorer: Avoid crashing when the desktop launchers are not initialized.
[wine.git] / tools / make_ctests.c
blob54f74841175c85f1406fc5c8fd87183e91195a35
1 /*
2 * Generate a C file containing a list of tests
4 * Copyright 2002, 2005 Alexandre Julliard
5 * Copyright 2002 Dimitrie O. Paun
6 * Copyright 2005 Royce Mitchell III for the ReactOS Project
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 ****** Keep in sync with tools/winapi/msvcmaker:_generate_testlist_c *****
25 #include "config.h"
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
36 static const char *output_file;
38 static void cleanup_files(void)
40 if (output_file) unlink( output_file );
43 static void exit_on_signal( int sig )
45 exit(1); /* this will call the atexit functions */
48 static void fatal_error( const char *msg, ... )
50 va_list valist;
51 va_start( valist, msg );
52 fprintf( stderr, "make_ctests: " );
53 vfprintf( stderr, msg, valist );
54 va_end( valist );
55 exit(1);
58 static void fatal_perror( const char *msg, ... )
60 va_list valist;
61 va_start( valist, msg );
62 fprintf( stderr, "make_ctests: " );
63 vfprintf( stderr, msg, valist );
64 perror( " " );
65 va_end( valist );
66 exit(1);
69 static void *xmalloc( size_t size )
71 void *res = malloc (size ? size : 1);
72 if (!res) fatal_error( "virtual memory exhausted.\n" );
73 return res;
76 static char* basename( const char* filename )
78 const char *p, *p2;
79 char *out;
80 size_t out_len;
82 p = strrchr ( filename, '/' );
83 if ( !p )
84 p = filename;
85 else
86 ++p;
88 /* look for backslashes, too... */
89 p2 = strrchr ( p, '\\' );
90 if ( p2 ) p = p2 + 1;
92 /* find extension... */
93 p2 = strrchr ( p, '.' );
94 if ( !p2 )
95 p2 = p + strlen(p);
97 /* malloc a copy */
98 out_len = p2-p;
99 out = xmalloc ( out_len+1 );
100 memcpy ( out, p, out_len );
101 out[out_len] = '\0';
102 return out;
105 int main( int argc, const char** argv )
107 int i, count = 0;
108 FILE *out = stdout;
109 char **tests = xmalloc( argc * sizeof(*tests) );
111 for (i = 1; i < argc; i++)
113 if (!strcmp( argv[i], "-o" ) && i < argc-1)
115 output_file = argv[++i];
116 continue;
118 tests[count++] = basename( argv[i] );
121 atexit( cleanup_files );
122 signal( SIGTERM, exit_on_signal );
123 signal( SIGINT, exit_on_signal );
124 #ifdef SIGHUP
125 signal( SIGHUP, exit_on_signal );
126 #endif
128 if (output_file)
130 if (!(out = fopen( output_file, "w" )))
131 fatal_perror( "cannot create %s", output_file );
134 fprintf( out,
135 "/* Automatically generated file; DO NOT EDIT!! */\n"
136 "\n"
137 "#define WIN32_LEAN_AND_MEAN\n"
138 "#include <windows.h>\n\n"
139 "#define STANDALONE\n"
140 "#include \"wine/test.h\"\n\n" );
142 for (i = 0; i < count; i++) fprintf( out, "extern void func_%s(void);\n", tests[i] );
144 fprintf( out,
145 "\n"
146 "const struct test winetest_testlist[] =\n"
147 "{\n" );
149 for (i = 0; i < count; i++) fprintf( out, " { \"%s\", func_%s },\n", tests[i], tests[i] );
151 fprintf( out,
152 " { 0, 0 }\n"
153 "};\n" );
155 if (output_file && fclose( out ))
156 fatal_perror( "error writing to %s", output_file );
158 output_file = NULL;
159 return 0;