2 * Helper functions for the Wine tools
4 * Copyright 2021 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifndef __WINE_TOOLS_H
22 #define __WINE_TOOLS_H
28 #include <sys/types.h>
41 # define mkdir(path,mode) mkdir(path)
43 # define S_ISREG(mod) (((mod) & _S_IFMT) == _S_IFREG)
47 # define pclose _pclose
48 # define strtoll _strtoi64
49 # define strtoull _strtoui64
50 # define strncasecmp _strnicmp
51 # define strcasecmp _stricmp
54 # include <sys/wait.h>
60 # if defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
63 # define __int64 long long
68 #if !defined(__GNUC__) && !defined(__attribute__)
69 #define __attribute__(x)
73 #define max(a,b) (((a) > (b)) ? (a) : (b))
76 #define min(a,b) (((a) < (b)) ? (a) : (b))
80 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
85 enum { CPU_i386
, CPU_x86_64
, CPU_ARM
, CPU_ARM64
} cpu
;
101 static inline void *xmalloc( size_t size
)
103 void *res
= malloc( size
? size
: 1 );
107 fprintf( stderr
, "Virtual memory exhausted.\n" );
113 static inline void *xrealloc (void *ptr
, size_t size
)
115 void *res
= realloc( ptr
, size
);
117 if (size
&& res
== NULL
)
119 fprintf( stderr
, "Virtual memory exhausted.\n" );
125 static inline char *xstrdup( const char *str
)
127 return strcpy( xmalloc( strlen(str
)+1 ), str
);
130 static inline int strendswith( const char *str
, const char *end
)
132 int l
= strlen( str
);
133 int m
= strlen( end
);
134 return l
>= m
&& !strcmp( str
+ l
- m
, end
);
137 static char *strmake( const char* fmt
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
138 static inline char *strmake( const char* fmt
, ... )
146 char *p
= xmalloc( size
);
148 n
= vsnprintf( p
, size
, fmt
, ap
);
150 if (n
== -1) size
*= 2;
151 else if ((size_t)n
>= size
) size
= n
+ 1;
157 /* string array functions */
161 unsigned int count
; /* strings in use */
162 unsigned int size
; /* total allocated size */
166 static const struct strarray empty_strarray
;
168 static inline void strarray_add( struct strarray
*array
, const char *str
)
170 if (array
->count
== array
->size
)
172 if (array
->size
) array
->size
*= 2;
173 else array
->size
= 16;
174 array
->str
= xrealloc( array
->str
, sizeof(array
->str
[0]) * array
->size
);
176 array
->str
[array
->count
++] = str
;
179 static inline void strarray_addall( struct strarray
*array
, struct strarray added
)
183 for (i
= 0; i
< added
.count
; i
++) strarray_add( array
, added
.str
[i
] );
186 static inline int strarray_exists( const struct strarray
*array
, const char *str
)
190 for (i
= 0; i
< array
->count
; i
++) if (!strcmp( array
->str
[i
], str
)) return 1;
194 static inline void strarray_add_uniq( struct strarray
*array
, const char *str
)
196 if (!strarray_exists( array
, str
)) strarray_add( array
, str
);
199 static inline void strarray_addall_uniq( struct strarray
*array
, struct strarray added
)
203 for (i
= 0; i
< added
.count
; i
++) strarray_add_uniq( array
, added
.str
[i
] );
206 static inline struct strarray
strarray_fromstring( const char *str
, const char *delim
)
208 struct strarray array
= empty_strarray
;
209 char *buf
= xstrdup( str
);
212 for (tok
= strtok( buf
, delim
); tok
; tok
= strtok( NULL
, delim
))
213 strarray_add( &array
, xstrdup( tok
));
218 static inline struct strarray
strarray_frompath( const char *path
)
220 if (!path
) return empty_strarray
;
222 return strarray_fromstring( path
, ";" );
224 return strarray_fromstring( path
, ":" );
228 static inline char *strarray_tostring( struct strarray array
, const char *sep
)
231 unsigned int i
, len
= 1 + (array
.count
- 1) * strlen(sep
);
233 if (!array
.count
) return xstrdup("");
234 for (i
= 0; i
< array
.count
; i
++) len
+= strlen( array
.str
[i
] );
235 str
= xmalloc( len
);
236 strcpy( str
, array
.str
[0] );
237 for (i
= 1; i
< array
.count
; i
++)
240 strcat( str
, array
.str
[i
] );
245 static inline void strarray_qsort( struct strarray
*array
, int (*func
)(const char **, const char **) )
247 if (array
->count
) qsort( array
->str
, array
->count
, sizeof(*array
->str
), (void *)func
);
250 static inline const char *strarray_bsearch( const struct strarray
*array
, const char *str
,
251 int (*func
)(const char **, const char **) )
255 if (array
->count
) res
= bsearch( &str
, array
->str
, array
->count
, sizeof(*array
->str
), (void *)func
);
256 return res
? *res
: NULL
;
259 static inline void strarray_trace( struct strarray args
)
263 for (i
= 0; i
< args
.count
; i
++)
265 if (strpbrk( args
.str
[i
], " \t\n\r")) printf( "\"%s\"", args
.str
[i
] );
266 else printf( "%s", args
.str
[i
] );
267 putchar( i
< args
.count
- 1 ? ' ' : '\n' );
271 static inline int strarray_spawn( struct strarray args
)
274 strarray_add( &args
, NULL
);
275 return _spawnvp( _P_WAIT
, args
.str
[0], args
.str
);
282 strarray_add( &args
, NULL
);
283 execvp( args
.str
[0], (char **)args
.str
);
286 if (pid
== -1) return -1;
288 while (pid
!= (wret
= waitpid( pid
, &status
, 0 )))
289 if (wret
== -1 && errno
!= EINTR
) break;
291 if (pid
== wret
&& WIFEXITED(status
)) return WEXITSTATUS(status
);
292 return 255; /* abnormal exit with an abort or an interrupt */
296 static inline char *get_basename( const char *file
)
298 const char *ret
= strrchr( file
, '/' );
299 return xstrdup( ret
? ret
+ 1 : file
);
302 static inline char *get_basename_noext( const char *file
)
304 char *ext
, *ret
= get_basename( file
);
305 if ((ext
= strrchr( ret
, '.' ))) *ext
= 0;
309 static inline char *get_dirname( const char *file
)
311 const char *end
= strrchr( file
, '/' );
312 if (!end
) return xstrdup( "." );
313 if (end
== file
) end
++;
314 return strmake( "%.*s", (int)(end
- file
), file
);
317 static inline char *replace_extension( const char *name
, const char *old_ext
, const char *new_ext
)
319 int name_len
= strlen( name
);
321 if (strendswith( name
, old_ext
)) name_len
-= strlen( old_ext
);
322 return strmake( "%.*s%s", name_len
, name
, new_ext
);
326 static inline int make_temp_file( const char *prefix
, const char *suffix
, char **name
)
328 static unsigned int value
;
330 const char *tmpdir
= NULL
;
332 if (!prefix
) prefix
= "tmp";
333 if (!suffix
) suffix
= "";
334 value
+= time(NULL
) + getpid();
336 for (count
= 0; count
< 0x8000; count
++)
339 *name
= strmake( "%s/%s-%08x%s", tmpdir
, prefix
, value
, suffix
);
341 *name
= strmake( "%s-%08x%s", prefix
, value
, suffix
);
342 fd
= open( *name
, O_RDWR
| O_CREAT
| O_EXCL
, 0600 );
343 if (fd
>= 0) return fd
;
345 if (errno
== EACCES
&& !tmpdir
&& !strchr( prefix
, '/' ))
347 if (!(tmpdir
= getenv("TMPDIR"))) tmpdir
= "/tmp";
351 fprintf( stderr
, "failed to create temp file for %s%s\n", prefix
, suffix
);
356 static inline struct target
get_default_target(void)
358 struct target target
;
360 target
.cpu
= CPU_i386
;
361 #elif defined(__x86_64__)
362 target
.cpu
= CPU_x86_64
;
363 #elif defined(__arm__)
364 target
.cpu
= CPU_ARM
;
365 #elif defined(__aarch64__)
366 target
.cpu
= CPU_ARM64
;
368 #error Unsupported CPU
372 target
.platform
= PLATFORM_APPLE
;
373 #elif defined(__ANDROID__)
374 target
.platform
= PLATFORM_ANDROID
;
375 #elif defined(__linux__)
376 target
.platform
= PLATFORM_LINUX
;
377 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
378 target
.platform
= PLATFORM_FREEBSD
;
380 target
.platform
= PLATFORM_SOLARIS
;
381 #elif defined(__CYGWIN__)
382 target
.platform
= PLATFORM_CYGWIN
;
383 #elif defined(_WIN32)
384 target
.platform
= PLATFORM_MINGW
;
386 target
.platform
= PLATFORM_UNSPECIFIED
;
393 static inline unsigned int get_target_ptr_size( struct target target
)
395 static const unsigned int sizes
[] =
402 return sizes
[target
.cpu
];
406 static inline void set_target_ptr_size( struct target
*target
, unsigned int size
)
411 if (size
== 8) target
->cpu
= CPU_x86_64
;
414 if (size
== 4) target
->cpu
= CPU_i386
;
417 if (size
== 8) target
->cpu
= CPU_ARM64
;
420 if (size
== 4) target
->cpu
= CPU_ARM
;
426 static inline int get_cpu_from_name( const char *name
)
434 { "i386", CPU_i386
},
435 { "i486", CPU_i386
},
436 { "i586", CPU_i386
},
437 { "i686", CPU_i386
},
438 { "i786", CPU_i386
},
439 { "x86_64", CPU_x86_64
},
440 { "amd64", CPU_x86_64
},
441 { "aarch64", CPU_ARM64
},
442 { "arm64", CPU_ARM64
},
447 for (i
= 0; i
< ARRAY_SIZE(cpu_names
); i
++)
448 if (!strncmp( cpu_names
[i
].name
, name
, strlen(cpu_names
[i
].name
) )) return cpu_names
[i
].cpu
;
453 static inline int get_platform_from_name( const char *name
)
461 { "macos", PLATFORM_APPLE
},
462 { "darwin", PLATFORM_APPLE
},
463 { "android", PLATFORM_ANDROID
},
464 { "linux", PLATFORM_LINUX
},
465 { "freebsd", PLATFORM_FREEBSD
},
466 { "solaris", PLATFORM_SOLARIS
},
467 { "mingw32", PLATFORM_MINGW
},
468 { "windows-gnu", PLATFORM_MINGW
},
469 { "winnt", PLATFORM_MINGW
},
470 { "windows", PLATFORM_WINDOWS
},
471 { "cygwin", PLATFORM_CYGWIN
},
475 for (i
= 0; i
< ARRAY_SIZE(platform_names
); i
++)
476 if (!strncmp( platform_names
[i
].name
, name
, strlen(platform_names
[i
].name
) ))
477 return platform_names
[i
].platform
;
482 static inline const char *get_arch_dir( struct target target
)
484 static const char *cpu_names
[] =
487 [CPU_x86_64
] = "x86_64",
489 [CPU_ARM64
] = "aarch64"
492 if (!cpu_names
[target
.cpu
]) return "";
494 switch (target
.platform
)
496 case PLATFORM_WINDOWS
:
497 case PLATFORM_CYGWIN
:
499 return strmake( "/%s-windows", cpu_names
[target
.cpu
] );
501 return strmake( "/%s-unix", cpu_names
[target
.cpu
] );
505 static inline int parse_target( const char *name
, struct target
*target
)
508 char *p
, *spec
= xstrdup( name
);
510 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
512 /* get the CPU part */
514 if ((p
= strchr( spec
, '-' )))
517 if ((res
= get_cpu_from_name( spec
)) == -1)
524 else if (!strcmp( spec
, "mingw32" ))
526 target
->cpu
= CPU_i386
;
535 /* get the OS part */
537 target
->platform
= PLATFORM_UNSPECIFIED
; /* default value */
540 if ((res
= get_platform_from_name( p
)) != -1)
542 target
->platform
= res
;
545 if (!(p
= strchr( p
, '-' ))) break;
554 static inline struct target
init_argv0_target( const char *argv0
)
556 char *name
= get_basename( argv0
);
557 struct target target
;
559 if (!strchr( name
, '-' ) || !parse_target( name
, &target
))
560 target
= get_default_target();
567 /* command-line option parsing */
568 /* partly based on the Glibc getopt() implementation */
577 static inline struct strarray
parse_options( int argc
, char **argv
, const char *short_opts
,
578 const struct long_option
*long_opts
, int long_only
,
579 void (*callback
)( int, char* ) )
581 struct strarray ret
= empty_strarray
;
586 #define OPT_ERR(fmt) { callback( '?', strmake( fmt, argv[1] )); continue; }
588 for (i
= 1; i
< argc
; i
++)
590 if (argv
[i
][0] != '-' || !argv
[i
][1]) /* not an option */
592 strarray_add( &ret
, argv
[i
] );
595 if (!strcmp( argv
[i
], "--" ))
597 /* add remaining args */
598 while (++i
< argc
) strarray_add( &ret
, argv
[i
] );
601 start
= argv
[i
] + 1 + (argv
[i
][1] == '-');
603 if (argv
[i
][1] == '-' || (long_only
&& (argv
[i
][2] || !strchr( short_opts
, argv
[i
][1] ))))
605 /* handle long option */
606 const struct long_option
*opt
, *found
= NULL
;
609 if (!(end
= strchr( start
, '=' ))) end
= start
+ strlen(start
);
610 for (opt
= long_opts
; opt
&& opt
->name
; opt
++)
612 if (strncmp( opt
->name
, start
, end
- start
)) continue;
613 if (!opt
->name
[end
- start
]) /* exact match */
624 else if (long_only
|| found
->has_arg
!= opt
->has_arg
|| found
->val
!= opt
->val
)
630 if (count
> 1) OPT_ERR( "option '%s' is ambiguous" );
636 if (!found
->has_arg
) OPT_ERR( "argument not allowed in '%s'" );
637 end
++; /* skip '=' */
639 else if (found
->has_arg
== 1)
641 if (i
== argc
- 1) OPT_ERR( "option '%s' requires an argument" );
646 callback( found
->val
, end
);
649 if (argv
[i
][1] == '-' || !long_only
|| !strchr( short_opts
, argv
[i
][1] ))
650 OPT_ERR( "unrecognized option '%s'" );
653 /* handle short option */
654 for ( ; *start
; start
++)
656 if (!(flag
= strchr( short_opts
, *start
))) OPT_ERR( "invalid option '%s'" );
660 if (!*end
) end
= NULL
;
661 if (flag
[2] != ':' && !end
)
663 if (i
== argc
- 1) OPT_ERR( "option '%s' requires an argument" );
666 callback( *start
, end
);
669 callback( *start
, NULL
);
676 #endif /* __WINE_TOOLS_H */