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
24 #ifndef __WINE_CONFIG_H
25 # error You must include config.h to use this header
33 #include <sys/types.h>
39 #ifdef HAVE_SYS_SYSCTL_H
40 # include <sys/sysctl.h>
47 # define mkdir(path,mode) mkdir(path)
49 # define S_ISREG(mod) (((mod) & _S_IFMT) == _S_IFREG)
53 # define pclose _pclose
54 # define strtoll _strtoi64
55 # define strtoull _strtoui64
56 # define strncasecmp _strnicmp
57 # define strcasecmp _stricmp
60 # include <sys/wait.h>
66 # if defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
69 # define __int64 long long
74 #if !defined(__GNUC__) && !defined(__attribute__)
75 #define __attribute__(x)
79 #define max(a,b) (((a) > (b)) ? (a) : (b))
82 #define min(a,b) (((a) < (b)) ? (a) : (b))
86 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
91 enum { CPU_i386
, CPU_x86_64
, CPU_ARM
, CPU_ARM64
} cpu
;
107 static inline void *xmalloc( size_t size
)
109 void *res
= malloc( size
? size
: 1 );
113 fprintf( stderr
, "Virtual memory exhausted.\n" );
119 static inline void *xrealloc (void *ptr
, size_t size
)
121 void *res
= realloc( ptr
, size
);
123 if (size
&& res
== NULL
)
125 fprintf( stderr
, "Virtual memory exhausted.\n" );
131 static inline char *xstrdup( const char *str
)
133 return strcpy( xmalloc( strlen(str
)+1 ), str
);
136 static inline int strendswith( const char *str
, const char *end
)
138 int l
= strlen( str
);
139 int m
= strlen( end
);
140 return l
>= m
&& !strcmp( str
+ l
- m
, end
);
143 static char *strmake( const char* fmt
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
144 static inline char *strmake( const char* fmt
, ... )
152 char *p
= xmalloc( size
);
154 n
= vsnprintf( p
, size
, fmt
, ap
);
156 if (n
== -1) size
*= 2;
157 else if ((size_t)n
>= size
) size
= n
+ 1;
163 /* string array functions */
167 unsigned int count
; /* strings in use */
168 unsigned int size
; /* total allocated size */
172 static const struct strarray empty_strarray
;
174 static inline void strarray_add( struct strarray
*array
, const char *str
)
176 if (array
->count
== array
->size
)
178 if (array
->size
) array
->size
*= 2;
179 else array
->size
= 16;
180 array
->str
= xrealloc( array
->str
, sizeof(array
->str
[0]) * array
->size
);
182 array
->str
[array
->count
++] = str
;
185 static inline void strarray_addall( struct strarray
*array
, struct strarray added
)
189 for (i
= 0; i
< added
.count
; i
++) strarray_add( array
, added
.str
[i
] );
192 static inline int strarray_exists( const struct strarray
*array
, const char *str
)
196 for (i
= 0; i
< array
->count
; i
++) if (!strcmp( array
->str
[i
], str
)) return 1;
200 static inline void strarray_add_uniq( struct strarray
*array
, const char *str
)
202 if (!strarray_exists( array
, str
)) strarray_add( array
, str
);
205 static inline void strarray_addall_uniq( struct strarray
*array
, struct strarray added
)
209 for (i
= 0; i
< added
.count
; i
++) strarray_add_uniq( array
, added
.str
[i
] );
212 static inline struct strarray
strarray_fromstring( const char *str
, const char *delim
)
214 struct strarray array
= empty_strarray
;
215 char *buf
= xstrdup( str
);
218 for (tok
= strtok( buf
, delim
); tok
; tok
= strtok( NULL
, delim
))
219 strarray_add( &array
, xstrdup( tok
));
224 static inline struct strarray
strarray_frompath( const char *path
)
226 if (!path
) return empty_strarray
;
228 return strarray_fromstring( path
, ";" );
230 return strarray_fromstring( path
, ":" );
234 static inline char *strarray_tostring( struct strarray array
, const char *sep
)
237 unsigned int i
, len
= 1 + (array
.count
- 1) * strlen(sep
);
239 if (!array
.count
) return xstrdup("");
240 for (i
= 0; i
< array
.count
; i
++) len
+= strlen( array
.str
[i
] );
241 str
= xmalloc( len
);
242 strcpy( str
, array
.str
[0] );
243 for (i
= 1; i
< array
.count
; i
++)
246 strcat( str
, array
.str
[i
] );
251 static inline void strarray_qsort( struct strarray
*array
, int (*func
)(const char **, const char **) )
253 if (array
->count
) qsort( array
->str
, array
->count
, sizeof(*array
->str
), (void *)func
);
256 static inline const char *strarray_bsearch( const struct strarray
*array
, const char *str
,
257 int (*func
)(const char **, const char **) )
261 if (array
->count
) res
= bsearch( &str
, array
->str
, array
->count
, sizeof(*array
->str
), (void *)func
);
262 return res
? *res
: NULL
;
265 static inline void strarray_trace( struct strarray args
)
269 for (i
= 0; i
< args
.count
; i
++)
271 if (strpbrk( args
.str
[i
], " \t\n\r")) printf( "\"%s\"", args
.str
[i
] );
272 else printf( "%s", args
.str
[i
] );
273 putchar( i
< args
.count
- 1 ? ' ' : '\n' );
277 static inline int strarray_spawn( struct strarray args
)
280 strarray_add( &args
, NULL
);
281 return _spawnvp( _P_WAIT
, args
.str
[0], args
.str
);
288 strarray_add( &args
, NULL
);
289 execvp( args
.str
[0], (char **)args
.str
);
292 if (pid
== -1) return -1;
294 while (pid
!= (wret
= waitpid( pid
, &status
, 0 )))
295 if (wret
== -1 && errno
!= EINTR
) break;
297 if (pid
== wret
&& WIFEXITED(status
)) return WEXITSTATUS(status
);
298 return 255; /* abnormal exit with an abort or an interrupt */
302 static inline char *get_basename( const char *file
)
304 const char *ret
= strrchr( file
, '/' );
305 return xstrdup( ret
? ret
+ 1 : file
);
308 static inline char *get_basename_noext( const char *file
)
310 char *ext
, *ret
= get_basename( file
);
311 if ((ext
= strrchr( ret
, '.' ))) *ext
= 0;
315 static inline char *get_dirname( const char *file
)
317 const char *end
= strrchr( file
, '/' );
318 if (!end
) return xstrdup( "." );
319 if (end
== file
) end
++;
320 return strmake( "%.*s", (int)(end
- file
), file
);
323 static inline char *replace_extension( const char *name
, const char *old_ext
, const char *new_ext
)
325 int name_len
= strlen( name
);
327 if (strendswith( name
, old_ext
)) name_len
-= strlen( old_ext
);
328 return strmake( "%.*s%s", name_len
, name
, new_ext
);
331 /* temp files management */
333 extern const char *temp_dir
;
334 extern struct strarray temp_files
;
336 static inline char *make_temp_dir(void)
338 unsigned int value
= time(NULL
) + getpid();
341 const char *tmpdir
= NULL
;
343 for (count
= 0; count
< 0x8000; count
++)
346 name
= strmake( "%s/tmp%08x", tmpdir
, value
);
348 name
= strmake( "tmp%08x", value
);
349 if (!mkdir( name
, 0700 )) return name
;
351 if (errno
== EACCES
&& !tmpdir
)
353 if (!(tmpdir
= getenv("TMPDIR"))) tmpdir
= "/tmp";
357 fprintf( stderr
, "failed to create directory for temp files\n" );
361 static inline char *make_temp_file( const char *prefix
, const char *suffix
)
363 static unsigned int value
;
367 if (!temp_dir
) temp_dir
= make_temp_dir();
368 if (!suffix
) suffix
= "";
369 if (!prefix
) prefix
= "tmp";
370 else prefix
= get_basename_noext( prefix
);
372 for (count
= 0; count
< 0x8000; count
++)
374 name
= strmake( "%s/%s-%08x%s", temp_dir
, prefix
, value
++, suffix
);
375 fd
= open( name
, O_RDWR
| O_CREAT
| O_EXCL
, 0600 );
378 #ifdef HAVE_SIGPROCMASK /* block signals while manipulating the temp files list */
379 sigset_t mask_set
, old_set
;
381 sigemptyset( &mask_set
);
382 sigaddset( &mask_set
, SIGHUP
);
383 sigaddset( &mask_set
, SIGTERM
);
384 sigaddset( &mask_set
, SIGINT
);
385 sigprocmask( SIG_BLOCK
, &mask_set
, &old_set
);
386 strarray_add( &temp_files
, name
);
387 sigprocmask( SIG_SETMASK
, &old_set
, NULL
);
389 strarray_add( &temp_files
, name
);
396 fprintf( stderr
, "failed to create temp file for %s%s in %s\n", prefix
, suffix
, temp_dir
);
400 static inline void remove_temp_files(void)
404 for (i
= 0; i
< temp_files
.count
; i
++) if (temp_files
.str
[i
]) unlink( temp_files
.str
[i
] );
405 if (temp_dir
) rmdir( temp_dir
);
409 static inline void init_signals( void (*cleanup
)(int) )
411 signal( SIGTERM
, cleanup
);
412 signal( SIGINT
, cleanup
);
414 signal( SIGHUP
, cleanup
);
419 static inline void *read_file( const char *name
, size_t *size
)
425 if ((fd
= open( name
, O_RDONLY
| O_BINARY
)) == -1) return NULL
;
427 data
= xmalloc( st
.st_size
);
428 res
= read( fd
, data
, st
.st_size
);
441 static inline struct target
get_default_target(void)
443 struct target target
;
445 target
.cpu
= CPU_i386
;
446 #elif defined(__x86_64__)
447 target
.cpu
= CPU_x86_64
;
448 #elif defined(__arm__)
449 target
.cpu
= CPU_ARM
;
450 #elif defined(__aarch64__)
451 target
.cpu
= CPU_ARM64
;
453 #error Unsupported CPU
457 target
.platform
= PLATFORM_APPLE
;
458 #elif defined(__ANDROID__)
459 target
.platform
= PLATFORM_ANDROID
;
460 #elif defined(__linux__)
461 target
.platform
= PLATFORM_LINUX
;
462 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
463 target
.platform
= PLATFORM_FREEBSD
;
465 target
.platform
= PLATFORM_SOLARIS
;
466 #elif defined(__CYGWIN__)
467 target
.platform
= PLATFORM_CYGWIN
;
468 #elif defined(_WIN32)
469 target
.platform
= PLATFORM_MINGW
;
471 target
.platform
= PLATFORM_UNSPECIFIED
;
478 static inline unsigned int get_target_ptr_size( struct target target
)
480 static const unsigned int sizes
[] =
487 return sizes
[target
.cpu
];
491 static inline void set_target_ptr_size( struct target
*target
, unsigned int size
)
496 if (size
== 8) target
->cpu
= CPU_x86_64
;
499 if (size
== 4) target
->cpu
= CPU_i386
;
502 if (size
== 8) target
->cpu
= CPU_ARM64
;
505 if (size
== 4) target
->cpu
= CPU_ARM
;
511 static inline int get_cpu_from_name( const char *name
)
519 { "i386", CPU_i386
},
520 { "i486", CPU_i386
},
521 { "i586", CPU_i386
},
522 { "i686", CPU_i386
},
523 { "i786", CPU_i386
},
524 { "x86_64", CPU_x86_64
},
525 { "amd64", CPU_x86_64
},
526 { "aarch64", CPU_ARM64
},
527 { "arm64", CPU_ARM64
},
532 for (i
= 0; i
< ARRAY_SIZE(cpu_names
); i
++)
533 if (!strncmp( cpu_names
[i
].name
, name
, strlen(cpu_names
[i
].name
) )) return cpu_names
[i
].cpu
;
538 static inline int get_platform_from_name( const char *name
)
546 { "macos", PLATFORM_APPLE
},
547 { "darwin", PLATFORM_APPLE
},
548 { "android", PLATFORM_ANDROID
},
549 { "linux", PLATFORM_LINUX
},
550 { "freebsd", PLATFORM_FREEBSD
},
551 { "solaris", PLATFORM_SOLARIS
},
552 { "mingw32", PLATFORM_MINGW
},
553 { "windows-gnu", PLATFORM_MINGW
},
554 { "winnt", PLATFORM_MINGW
},
555 { "windows", PLATFORM_WINDOWS
},
556 { "cygwin", PLATFORM_CYGWIN
},
560 for (i
= 0; i
< ARRAY_SIZE(platform_names
); i
++)
561 if (!strncmp( platform_names
[i
].name
, name
, strlen(platform_names
[i
].name
) ))
562 return platform_names
[i
].platform
;
567 static inline const char *get_arch_dir( struct target target
)
569 static const char *cpu_names
[] =
572 [CPU_x86_64
] = "x86_64",
574 [CPU_ARM64
] = "aarch64"
577 if (!cpu_names
[target
.cpu
]) return "";
579 switch (target
.platform
)
581 case PLATFORM_WINDOWS
:
582 case PLATFORM_CYGWIN
:
584 return strmake( "/%s-windows", cpu_names
[target
.cpu
] );
586 return strmake( "/%s-unix", cpu_names
[target
.cpu
] );
590 static inline int parse_target( const char *name
, struct target
*target
)
593 char *p
, *spec
= xstrdup( name
);
595 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
597 /* get the CPU part */
599 if ((p
= strchr( spec
, '-' )))
602 if ((res
= get_cpu_from_name( spec
)) == -1)
609 else if (!strcmp( spec
, "mingw32" ))
611 target
->cpu
= CPU_i386
;
620 /* get the OS part */
622 target
->platform
= PLATFORM_UNSPECIFIED
; /* default value */
625 if ((res
= get_platform_from_name( p
)) != -1)
627 target
->platform
= res
;
630 if (!(p
= strchr( p
, '-' ))) break;
639 static inline struct target
init_argv0_target( const char *argv0
)
641 char *name
= get_basename( argv0
);
642 struct target target
;
644 if (!strchr( name
, '-' ) || !parse_target( name
, &target
))
645 target
= get_default_target();
652 static inline char *get_argv0_dir( const char *argv0
)
657 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
658 dir
= realpath( "/proc/self/exe", NULL
);
659 #elif defined (__FreeBSD__) || defined(__DragonFly__)
660 static int pathname
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1 };
661 size_t path_size
= PATH_MAX
;
662 char *path
= xmalloc( path_size
);
663 if (!sysctl( pathname
, ARRAY_SIZE(pathname
), path
, &path_size
, NULL
, 0 ))
664 dir
= realpath( path
, NULL
);
667 if (!dir
&& !(dir
= realpath( argv0
, NULL
))) return NULL
;
668 return get_dirname( dir
);
670 return get_dirname( argv0
);
675 /* output buffer management */
677 extern unsigned char *output_buffer
;
678 extern size_t output_buffer_pos
;
679 extern size_t output_buffer_size
;
681 static inline void check_output_buffer_space( size_t size
)
683 if (output_buffer_pos
+ size
>= output_buffer_size
)
685 output_buffer_size
= max( output_buffer_size
* 2, output_buffer_pos
+ size
);
686 output_buffer
= xrealloc( output_buffer
, output_buffer_size
);
690 static inline void init_output_buffer(void)
692 output_buffer_size
= 1024;
693 output_buffer_pos
= 0;
694 output_buffer
= xmalloc( output_buffer_size
);
697 static inline void put_data( const void *data
, size_t size
)
699 check_output_buffer_space( size
);
700 memcpy( output_buffer
+ output_buffer_pos
, data
, size
);
701 output_buffer_pos
+= size
;
704 static inline void put_byte( unsigned char val
)
706 check_output_buffer_space( 1 );
707 output_buffer
[output_buffer_pos
++] = val
;
710 static inline void put_word( unsigned short val
)
712 check_output_buffer_space( 2 );
713 output_buffer
[output_buffer_pos
++] = val
;
714 output_buffer
[output_buffer_pos
++] = val
>> 8;
717 static inline void put_dword( unsigned int val
)
719 check_output_buffer_space( 4 );
720 output_buffer
[output_buffer_pos
++] = val
;
721 output_buffer
[output_buffer_pos
++] = val
>> 8;
722 output_buffer
[output_buffer_pos
++] = val
>> 16;
723 output_buffer
[output_buffer_pos
++] = val
>> 24;
726 static inline void put_qword( unsigned int val
)
732 static inline void align_output( unsigned int align
)
734 size_t size
= align
- (output_buffer_pos
% align
);
736 if (size
== align
) return;
737 check_output_buffer_space( size
);
738 memset( output_buffer
+ output_buffer_pos
, 0, size
);
739 output_buffer_pos
+= size
;
742 static inline void flush_output_buffer( const char *name
)
744 int fd
= open( name
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666 );
746 if (fd
== -1 || write( fd
, output_buffer
, output_buffer_pos
) != output_buffer_pos
)
752 free( output_buffer
);
755 /* command-line option parsing */
756 /* partly based on the Glibc getopt() implementation */
765 static inline struct strarray
parse_options( int argc
, char **argv
, const char *short_opts
,
766 const struct long_option
*long_opts
, int long_only
,
767 void (*callback
)( int, char* ) )
769 struct strarray ret
= empty_strarray
;
774 #define OPT_ERR(fmt) { callback( '?', strmake( fmt, argv[1] )); continue; }
776 for (i
= 1; i
< argc
; i
++)
778 if (argv
[i
][0] != '-' || !argv
[i
][1]) /* not an option */
780 strarray_add( &ret
, argv
[i
] );
783 if (!strcmp( argv
[i
], "--" ))
785 /* add remaining args */
786 while (++i
< argc
) strarray_add( &ret
, argv
[i
] );
789 start
= argv
[i
] + 1 + (argv
[i
][1] == '-');
791 if (argv
[i
][1] == '-' || (long_only
&& (argv
[i
][2] || !strchr( short_opts
, argv
[i
][1] ))))
793 /* handle long option */
794 const struct long_option
*opt
, *found
= NULL
;
797 if (!(end
= strchr( start
, '=' ))) end
= start
+ strlen(start
);
798 for (opt
= long_opts
; opt
&& opt
->name
; opt
++)
800 if (strncmp( opt
->name
, start
, end
- start
)) continue;
801 if (!opt
->name
[end
- start
]) /* exact match */
812 else if (long_only
|| found
->has_arg
!= opt
->has_arg
|| found
->val
!= opt
->val
)
818 if (count
> 1) OPT_ERR( "option '%s' is ambiguous" );
824 if (!found
->has_arg
) OPT_ERR( "argument not allowed in '%s'" );
825 end
++; /* skip '=' */
827 else if (found
->has_arg
== 1)
829 if (i
== argc
- 1) OPT_ERR( "option '%s' requires an argument" );
834 callback( found
->val
, end
);
837 if (argv
[i
][1] == '-' || !long_only
|| !strchr( short_opts
, argv
[i
][1] ))
838 OPT_ERR( "unrecognized option '%s'" );
841 /* handle short option */
842 for ( ; *start
; start
++)
844 if (!(flag
= strchr( short_opts
, *start
))) OPT_ERR( "invalid option '%s'" );
848 if (!*end
) end
= NULL
;
849 if (flag
[2] != ':' && !end
)
851 if (i
== argc
- 1) OPT_ERR( "option '%s' requires an argument" );
854 callback( *start
, end
);
857 callback( *start
, NULL
);
864 #endif /* __WINE_TOOLS_H */