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]))
84 static inline void *xmalloc( size_t size
)
86 void *res
= malloc( size
? size
: 1 );
90 fprintf( stderr
, "Virtual memory exhausted.\n" );
96 static inline void *xrealloc (void *ptr
, size_t size
)
98 void *res
= realloc( ptr
, size
);
100 if (size
&& res
== NULL
)
102 fprintf( stderr
, "Virtual memory exhausted.\n" );
108 static inline char *xstrdup( const char *str
)
110 return strcpy( xmalloc( strlen(str
)+1 ), str
);
113 static inline int strendswith( const char *str
, const char *end
)
115 int l
= strlen( str
);
116 int m
= strlen( end
);
117 return l
>= m
&& !strcmp( str
+ l
- m
, end
);
120 static char *strmake( const char* fmt
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
121 static inline char *strmake( const char* fmt
, ... )
129 char *p
= xmalloc( size
);
131 n
= vsnprintf( p
, size
, fmt
, ap
);
133 if (n
== -1) size
*= 2;
134 else if ((size_t)n
>= size
) size
= n
+ 1;
140 /* string array functions */
144 unsigned int count
; /* strings in use */
145 unsigned int size
; /* total allocated size */
149 static const struct strarray empty_strarray
;
151 static inline void strarray_add( struct strarray
*array
, const char *str
)
153 if (array
->count
== array
->size
)
155 if (array
->size
) array
->size
*= 2;
156 else array
->size
= 16;
157 array
->str
= xrealloc( array
->str
, sizeof(array
->str
[0]) * array
->size
);
159 array
->str
[array
->count
++] = str
;
162 static inline void strarray_addall( struct strarray
*array
, struct strarray added
)
166 for (i
= 0; i
< added
.count
; i
++) strarray_add( array
, added
.str
[i
] );
169 static inline int strarray_exists( const struct strarray
*array
, const char *str
)
173 for (i
= 0; i
< array
->count
; i
++) if (!strcmp( array
->str
[i
], str
)) return 1;
177 static inline void strarray_add_uniq( struct strarray
*array
, const char *str
)
179 if (!strarray_exists( array
, str
)) strarray_add( array
, str
);
182 static inline void strarray_addall_uniq( struct strarray
*array
, struct strarray added
)
186 for (i
= 0; i
< added
.count
; i
++) strarray_add_uniq( array
, added
.str
[i
] );
189 static inline struct strarray
strarray_fromstring( const char *str
, const char *delim
)
191 struct strarray array
= empty_strarray
;
192 char *buf
= xstrdup( str
);
195 for (tok
= strtok( buf
, delim
); tok
; tok
= strtok( NULL
, delim
))
196 strarray_add( &array
, xstrdup( tok
));
201 static inline struct strarray
strarray_frompath( const char *path
)
203 if (!path
) return empty_strarray
;
205 return strarray_fromstring( path
, ";" );
207 return strarray_fromstring( path
, ":" );
211 static inline char *strarray_tostring( struct strarray array
, const char *sep
)
214 unsigned int i
, len
= 1 + (array
.count
- 1) * strlen(sep
);
216 if (!array
.count
) return xstrdup("");
217 for (i
= 0; i
< array
.count
; i
++) len
+= strlen( array
.str
[i
] );
218 str
= xmalloc( len
);
219 strcpy( str
, array
.str
[0] );
220 for (i
= 1; i
< array
.count
; i
++)
223 strcat( str
, array
.str
[i
] );
228 static inline void strarray_qsort( struct strarray
*array
, int (*func
)(const char **, const char **) )
230 if (array
->count
) qsort( array
->str
, array
->count
, sizeof(*array
->str
), (void *)func
);
233 static inline const char *strarray_bsearch( const struct strarray
*array
, const char *str
,
234 int (*func
)(const char **, const char **) )
238 if (array
->count
) res
= bsearch( &str
, array
->str
, array
->count
, sizeof(*array
->str
), (void *)func
);
239 return res
? *res
: NULL
;
242 static inline void strarray_trace( struct strarray args
)
246 for (i
= 0; i
< args
.count
; i
++)
248 if (strpbrk( args
.str
[i
], " \t\n\r")) printf( "\"%s\"", args
.str
[i
] );
249 else printf( "%s", args
.str
[i
] );
250 putchar( i
< args
.count
- 1 ? ' ' : '\n' );
254 static inline int strarray_spawn( struct strarray args
)
257 strarray_add( &args
, NULL
);
258 return _spawnvp( _P_WAIT
, args
.str
[0], args
.str
);
265 strarray_add( &args
, NULL
);
266 execvp( args
.str
[0], (char **)args
.str
);
269 if (pid
== -1) return -1;
271 while (pid
!= (wret
= waitpid( pid
, &status
, 0 )))
272 if (wret
== -1 && errno
!= EINTR
) break;
274 if (pid
== wret
&& WIFEXITED(status
)) return WEXITSTATUS(status
);
275 return 255; /* abnormal exit with an abort or an interrupt */
279 static inline char *get_basename( const char *file
)
281 const char *ret
= strrchr( file
, '/' );
282 return xstrdup( ret
? ret
+ 1 : file
);
285 static inline char *get_basename_noext( const char *file
)
287 char *ext
, *ret
= get_basename( file
);
288 if ((ext
= strrchr( ret
, '.' ))) *ext
= 0;
292 static inline char *get_dirname( const char *file
)
294 const char *end
= strrchr( file
, '/' );
295 if (!end
) return xstrdup( "." );
296 if (end
== file
) end
++;
297 return strmake( "%.*s", (int)(end
- file
), file
);
300 static inline char *replace_extension( const char *name
, const char *old_ext
, const char *new_ext
)
302 int name_len
= strlen( name
);
304 if (strendswith( name
, old_ext
)) name_len
-= strlen( old_ext
);
305 return strmake( "%.*s%s", name_len
, name
, new_ext
);
309 static inline int make_temp_file( const char *prefix
, const char *suffix
, char **name
)
311 static unsigned int value
;
313 const char *tmpdir
= NULL
;
315 if (!prefix
) prefix
= "tmp";
316 if (!suffix
) suffix
= "";
317 value
+= time(NULL
) + getpid();
319 for (count
= 0; count
< 0x8000; count
++)
322 *name
= strmake( "%s/%s-%08x%s", tmpdir
, prefix
, value
, suffix
);
324 *name
= strmake( "%s-%08x%s", prefix
, value
, suffix
);
325 fd
= open( *name
, O_RDWR
| O_CREAT
| O_EXCL
, 0600 );
326 if (fd
>= 0) return fd
;
328 if (errno
== EACCES
&& !tmpdir
&& !strchr( prefix
, '/' ))
330 if (!(tmpdir
= getenv("TMPDIR"))) tmpdir
= "/tmp";
334 fprintf( stderr
, "failed to create temp file for %s%s\n", prefix
, suffix
);
339 /* command-line option parsing */
340 /* partly based on the Glibc getopt() implementation */
349 static inline struct strarray
parse_options( int argc
, char **argv
, const char *short_opts
,
350 const struct long_option
*long_opts
, int long_only
,
351 void (*callback
)( int, char* ) )
353 struct strarray ret
= empty_strarray
;
358 #define OPT_ERR(fmt) { callback( '?', strmake( fmt, argv[1] )); continue; }
360 for (i
= 1; i
< argc
; i
++)
362 if (argv
[i
][0] != '-' || !argv
[i
][1]) /* not an option */
364 strarray_add( &ret
, argv
[i
] );
367 if (!strcmp( argv
[i
], "--" ))
369 /* add remaining args */
370 while (++i
< argc
) strarray_add( &ret
, argv
[i
] );
373 start
= argv
[i
] + 1 + (argv
[i
][1] == '-');
375 if (argv
[i
][1] == '-' || (long_only
&& (argv
[i
][2] || !strchr( short_opts
, argv
[i
][1] ))))
377 /* handle long option */
378 const struct long_option
*opt
, *found
= NULL
;
381 if (!(end
= strchr( start
, '=' ))) end
= start
+ strlen(start
);
382 for (opt
= long_opts
; opt
&& opt
->name
; opt
++)
384 if (strncmp( opt
->name
, start
, end
- start
)) continue;
385 if (!opt
->name
[end
- start
]) /* exact match */
396 else if (long_only
|| found
->has_arg
!= opt
->has_arg
|| found
->val
!= opt
->val
)
402 if (count
> 1) OPT_ERR( "option '%s' is ambiguous" );
408 if (!found
->has_arg
) OPT_ERR( "argument not allowed in '%s'" );
409 end
++; /* skip '=' */
411 else if (found
->has_arg
== 1)
413 if (i
== argc
- 1) OPT_ERR( "option '%s' requires an argument" );
418 callback( found
->val
, end
);
421 if (argv
[i
][1] == '-' || !long_only
|| !strchr( short_opts
, argv
[i
][1] ))
422 OPT_ERR( "unrecognized option '%s'" );
425 /* handle short option */
426 for ( ; *start
; start
++)
428 if (!(flag
= strchr( short_opts
, *start
))) OPT_ERR( "invalid option '%s'" );
432 if (!*end
) end
= NULL
;
433 if (flag
[2] != ':' && !end
)
435 if (i
== argc
- 1) OPT_ERR( "option '%s' requires an argument" );
438 callback( *start
, end
);
441 callback( *start
, NULL
);
448 #endif /* __WINE_TOOLS_H */