forked from FreeRADIUS/freeradius-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jlibtool.c
3172 lines (2677 loc) · 72.3 KB
/
jlibtool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ctype.h>
#include <libgen.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#if !defined(__MINGW32__)
# include <sys/wait.h>
#endif
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <assert.h>
#include <signal.h>
extern char **environ;
#ifndef FALL_THROUGH
/** clang 10 doesn't recognised the FALL-THROUGH comment anymore
*/
# if (defined(__clang__) && (__clang_major__ >= 10)) || (defined(__GNUC__) && __GNUC__ >= 7)
# define FALL_THROUGH __attribute__((fallthrough))
# else
# define FALL_THROUGH ((void)0)
# endif
#endif
#define XSTRINGIFY(x) #x
#define STRINGIFY(x) XSTRINGIFY(x)
#define UNCONST(_type, _ptr) ((_type)((uintptr_t)(_ptr)))
/** The set of executables used
*
*/
typedef struct {
char const *cc; //!< C compiler.
char const *cxx; //!< C++ compiler.
char const *link_c; //!< C linker.
char const *link_cxx; //!< C++ linker.
char const *ranlib; //!< Archiver/indexer.
} toolset_t;
#ifndef BUILD_CC
# define BUILD_CC "clang"
#endif
#ifndef HOST_CXX
# define HOST_CXX "g++"
#endif
#ifndef HOST_LINK_C
# define HOST_LINK_C "clang"
#endif
#ifndef HOST_LINK_CXX
# define HOST_LINK_CXX "g++"
#endif
#ifndef BUILD_RANLIB
# if !defined(__EMX__) && !defined(_OSD_POSIX)
# define BUILD_RANLIB "ranlib"
# endif
#endif
#ifndef TARGET_CC
# define TARGET_CC BUILD_CC
#endif
#ifndef TARGET_CXX
# define TARGET_CXX HOST_CXX
#endif
#ifndef TARGET_LINK_C
# define TARGET_LINK_C HOST_LINK_C
#endif
#ifndef TARGET_LINK_CXX
# define TARGET_LINK_CXX HOST_LINK_CXX
#endif
#ifndef TARGET_RANLIB
# define TARGET_RANLIB "ranlib"
#endif
/*
* Set to true when an exec times out
*/
static bool timeout = false;
static const toolset_t toolset_host = {
.cc = BUILD_CC,
.cxx = HOST_CXX,
.link_c = HOST_LINK_C,
.link_cxx = HOST_LINK_CXX,
#ifdef BUILD_RANLIB
.ranlib = BUILD_RANLIB
#endif
};
static const toolset_t toolset_target = {
.cc = TARGET_CC,
.cxx = TARGET_CXX,
.link_c = TARGET_LINK_C,
.link_cxx = TARGET_LINK_CXX,
#ifdef TARGET_RANLIB
.ranlib = TARGET_RANLIB
#endif
};
/** The default active toolset
*
*/
static const toolset_t *toolset = &toolset_host;
/** A jlibtool build system target
*
*/
typedef struct {
char const *name; //!< Canonical name for this target.
char const *shell_cmd;
char const *gen_exports;
char const *def2implib_cmd;
char const *share_sw;
bool use_omf;
bool truncate_dll_name;
char const *dynamic_lib_ext;
char const *static_lib_ext;
char const *module_lib_ext;
char const *object_ext;
char const *exe_ext;
char const *librarian;
char const *librarian_opts;
char const *pic_flag;
char const *rpath;
char const *shared_opts;
char const *module_opts;
char const *linker_flag_prefix;
bool linker_flag_no_equals;
char const *dynamic_link_opts;
char const *dynamic_link_opts_undefined;
char const *(*dynamic_link_version_func)(char const *version_info);
char const *dynamic_install_name;
char const *dynamic_link_no_install;
bool has_realpath;
bool add_minus_l;
char const *ld_run_path;
char const *ld_library_path;
char const *ld_library_path_local;
} target_t;
static char const *darwin_dynamic_link_function(char const *version_info);
static const target_t target_macos = {
.name = "macos",
.shell_cmd = "/bin/sh",
.dynamic_lib_ext = "dylib",
.module_lib_ext = "bundle",
.static_lib_ext = "a",
.object_ext = "o",
.librarian = "ar",
.librarian_opts = "cr",
/* man libtool(1) documents ranlib option of -c. */
.pic_flag = "-fPIC -fno-common",
.shared_opts = "-dynamiclib",
.module_opts = "-bundle -dynamic",
.dynamic_link_opts = "-bind_at_load",
.dynamic_link_opts_undefined = "-Wl,-w -undefined dynamic_lookup",
.dynamic_link_version_func = darwin_dynamic_link_function,
.dynamic_install_name = "-install_name",
.dynamic_link_no_install = "-dylib_file",
.has_realpath = true,
/*-install_name /Users/jerenk/apache-2.0-cvs/lib/libapr.0.dylib -compatibility_version 1 -current_version 1.0 */
.ld_library_path = "DYLD_LIBRARY_PATH",
.ld_library_path_local = "DYLD_FALLBACK_LIBRARY_PATH",
};
static const target_t target_linux_and_bsd = {
.name = "linux_and_bsd",
.shell_cmd = "/bin/sh",
.dynamic_lib_ext = "so",
.module_lib_ext = "so",
.static_lib_ext = "a",
.object_ext = "o",
.librarian = "ar",
.librarian_opts = "cr",
.pic_flag = "-fPIC",
.rpath = "-rpath",
.shared_opts = "-shared",
.module_opts = "-shared",
.linker_flag_prefix = "-Wl,",
.dynamic_link_opts = "-Wl,-export-dynamic",
.add_minus_l = true,
.ld_run_path = "LD_RUN_PATH",
.ld_library_path = "LD_LIBRARY_PATH",
.ld_library_path_local = "LD_LIBRARY_PATH"
};
static const target_t target_solaris_gnu = {
.name = "solaris_gnu",
.shell_cmd = "/bin/sh",
.dynamic_lib_ext = "so",
.module_lib_ext = "so",
.static_lib_ext = "a",
.object_ext = "o",
.librarian = "ar",
.librarian_opts = "cr",
.pic_flag = "-fPIC",
.rpath = "-rpath",
.shared_opts = "-shared",
.module_opts = "-shared",
.linker_flag_prefix = "-Wl,",
.dynamic_link_opts = "-export-dynamic",
.add_minus_l = true,
.ld_run_path = "LD_RUN_PATH",
.ld_library_path = "LD_LIBRARY_PATH",
.ld_library_path_local = "LD_LIBRARY_PATH"
};
static const target_t target_solaris = {
.name = "solaris",
.shell_cmd = "/bin/sh",
.dynamic_lib_ext = "so",
.module_lib_ext = "so",
.static_lib_ext = "a",
.object_ext = "o",
.librarian = "ar",
.librarian_opts = "cr",
.pic_flag = "-KPIC",
.rpath = "-R",
.shared_opts = "-G",
.module_opts = "-G",
.dynamic_link_opts = "",
.linker_flag_no_equals = true,
.add_minus_l = true,
.has_realpath = true,
.ld_run_path = "LD_RUN_PATH",
.ld_library_path = "LD_LIBRARY_PATH",
.ld_library_path_local = "LD_LIBRARY_PATH"
};
static const target_t target_osd_posix = {
.name = "osd_posix",
.shell_cmd = "/usr/bin/sh",
.dynamic_lib_ext = "so",
.module_lib_ext = "so",
.static_lib_ext = "a",
.object_ext = "o",
.librarian = "ar",
.librarian_opts = "cr",
.shared_opts = "-G",
.module_opts = "-G",
.linker_flag_prefix = "-Wl,",
};
static const target_t target_sinix_mips = {
.name = "sinix_mips",
.shell_cmd = "/usr/bin/sh",
.dynamic_lib_ext = "so",
.module_lib_ext = "so",
.static_lib_ext = "a",
.object_ext = "o",
.librarian = "ar",
.librarian_opts = "cr",
.rpath = "-Brpath",
.shared_opts = "-G",
.module_opts = "-G",
.linker_flag_prefix = "-Wl,",
.dynamic_link_opts = "-Wl,-Blargedynsym",
.ld_run_path = "LD_RUN_PATH",
.ld_library_path = "LD_LIBRARY_PATH",
.ld_library_path_local = "LD_LIBRARY_PATH"
};
static const target_t target_emx_omf = {
.name = "emx_omf",
.shell_cmd = "sh",
.gen_exports = "emxexp",
.def2implib_cmd = "emximp",
.share_sw = "-Zdll -Zmtd",
.use_omf = true,
.truncate_dll_name = true,
.dynamic_lib_ext = "dll",
.exe_ext = ".exe",
.static_lib_ext = "lib",
.object_ext = "obj",
.librarian = "emxomfar",
.librarian_opts = "cr"
};
static const target_t target_emx = {
.name = "emx",
.shell_cmd = "sh",
.gen_exports = "emxexp",
.def2implib_cmd = "emximp",
.share_sw = "-Zdll -Zmtd",
.truncate_dll_name = true,
.dynamic_lib_ext = "dll",
.exe_ext = ".exe",
.static_lib_ext = "a",
.object_ext = "o",
.librarian = "ar",
.librarian_opts = "cr"
};
static const target_t target_ming32 = {
.name = "ming32",
.shell_cmd = "sh",
.dynamic_lib_ext = "dll",
.module_lib_ext = "dll",
.static_lib_ext = "a",
.object_ext = "o",
.librarian = "ar",
.librarian_opts = "cr",
.linker_flag_prefix = "-Wl,",
.shared_opts = "-shared",
.module_opts = "-shared",
.exe_ext = ".exe",
};
static const target_t target_emscripten = {
.name = "emscripten",
.shell_cmd = "/bin/sh",
.dynamic_lib_ext = "wasm",
.module_lib_ext = "wasm",
.static_lib_ext = "a",
.exe_ext = ".js",
.object_ext = "o",
.librarian = "emar",
.librarian_opts = "cr",
.pic_flag = "-fPIC",
.shared_opts = "-shared",
.module_opts = "-shared",
.linker_flag_prefix = "-Wl,",
.dynamic_link_opts = "",
.add_minus_l = true,
.ld_run_path = "LD_RUN_PATH",
.ld_library_path = "LD_LIBRARY_PATH",
.ld_library_path_local = "LD_LIBRARY_PATH"
};
/** jlibtool should be compiled in the host environment
*
* For the vast majority of cases the host environment and the target environment
* are the same, but not always.
*
* Still, it makes more sense to default to the host target, so use various
* preprocessor checks to figure out what system type we're building on
* and set the default target appropriately.
*/
/*
* macOS/Darwin
*/
#if defined(__APPLE__)
static const target_t *target = &target_macos;
/*
* Linux and the BSDs
*/
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
static const target_t *target = &target_linux_and_bsd;
/*
* Solaris with GNUC
*/
#elif defined(__sun) && defined(__GNUC__)
static const target_t *target = &target_solaris_gnu;
/*
* Solaris without GNUC
*/
#elif defined(__sun) && !defined(__GNUC__)
static const target_t *target = &target_solaris;
/*
* OSD POSIX
*/
#elif defined(_OSD_POSIX)
# define NEED_SNPRINTF
static const target_t *target = &target_osd_posix;
/*
* SINIX (mips)
*/
#elif defined(sinix) && defined(mips) && defined(__SNI_TARG_UNIX)
# define NEED_SNPRINTF
static const target_t *target = &target_sinix_mips;
/*
* EMX with OMF format
*/
#elif defined(__EMX__) && defined(USE_OMF)
#include <process.h>
static const target_t *target = &target_emx_omf;
/*
* EMX without OMF format
*/
#elif defined(__EMX__)
#include <process.h>
static const target_t *target = &target_emx;
/*
* Ming32
*/
#elif defined(__MINGW32__)
#define MKDIR_NO_UMASK
static const target_t *target = &target_ming32;
/*
* Emscripten (WASM)
*/
#elif defined(__EMSCRIPTEN__)
static const target_t *target = &target_emscripten;
#else
# error Unsupported platform: Please add a target for your platform.
#endif
typedef struct {
char const *name;
target_t const *target;
} target_map_t;
#define IS_TARGET(_name) (target == &target_ ##_name)
#define TARGET(_name, _struct) { .name = _name, .target = &target_ ## _struct }
/** Mapping values for --target
*
* These allow the users to specify a target system when cross-compiling
*/
static const target_map_t target_map[] = {
TARGET("bsd", linux_and_bsd),
TARGET("emscripten", emscripten),
TARGET("emx", emx),
TARGET("emx-omf", emx_omf),
TARGET("freebsd", linux_and_bsd),
TARGET("linux", linux_and_bsd),
TARGET("macos", macos),
TARGET("darwin", macos),
TARGET("ming32", ming32),
TARGET("netbsd", linux_and_bsd),
TARGET("openbsd", linux_and_bsd),
TARGET("osd-posix", osd_posix),
TARGET("sinix", sinix_mips),
TARGET("solaris", solaris),
TARGET("solaris-gnuc", solaris_gnu),
TARGET("wasm", emscripten),
};
#ifndef LIBDIR
# define LIBDIR "/usr/local/lib"
#endif
#ifndef OBJDIR
# define OBJDIR ".libs"
#endif
#ifdef NEED_SNPRINTF
#include <stdarg.h>
#endif
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
/* We want to say we are libtool 1.4 for shlibtool compatibility. */
#define VERSION "1.4"
#define DEBUG(fmt, ...) if (cmd->options.debug) printf(fmt, ## __VA_ARGS__)
#define NOTICE(fmt, ...) if (!cmd->options.silent) printf(fmt, ## __VA_ARGS__)
#define ERROR(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
enum tool_mode {
MODE_UNKNOWN,
MODE_COMPILE,
MODE_LINK,
MODE_EXECUTE,
MODE_INSTALL,
};
enum output_type {
OUT_GENERAL,
OUT_OBJECT,
OUT_PROGRAM,
OUT_LIB,
OUT_STATIC_LIB_ONLY,
OUT_DYNAMIC_LIB_ONLY,
OUT_MODULE,
};
enum pic_mode {
PIC_UNKNOWN,
PIC_PREFER,
PIC_AVOID,
};
enum shared_mode {
SHARE_UNSET,
SHARE_STATIC,
SHARE_SHARED,
};
enum lib_type {
TYPE_UKNOWN,
TYPE_STATIC_LIB,
TYPE_DYNAMIC_LIB,
TYPE_MODULE_LIB,
TYPE_OBJECT,
};
typedef struct {
char const **vals;
int num;
} count_chars;
typedef struct {
char const *normal;
char const *install;
} library_name;
typedef struct {
count_chars *normal;
count_chars *install;
count_chars *dependencies;
} library_opts;
typedef struct {
int silent;
int debug;
enum shared_mode shared;
int export_all;
int dry_run;
enum pic_mode pic_mode;
int export_dynamic;
int no_install;
} options_t;
typedef struct {
enum tool_mode mode;
enum output_type output;
options_t options;
char const *output_name;
char const *fake_output_name;
char const *basename;
char const *install_path;
char const *compiler;
char const *program;
count_chars *program_opts;
count_chars *arglist;
count_chars *tmp_dirs;
count_chars *obj_files;
count_chars *dep_rpaths;
count_chars *rpaths;
library_name static_name;
library_name shared_name;
library_name module_name;
library_opts static_opts;
library_opts shared_opts;
char const *version_info;
char const *undefined_flag;
unsigned int timeout;
} command_t;
static void add_rpath(count_chars *cc, char const *path);
static pid_t spawn_pid;
static char const *program = NULL;
static void __attribute__((noreturn)) usage(int code)
{
printf("Usage: jlibtool [OPTIONS...] COMMANDS...\n");
printf("jlibtool is a replacement for GNU libtool with similar functionality.\n\n");
printf(" --config show all configuration variables\n");
printf(" --debug enable verbose shell tracing\n");
printf(" --dry-run display commands without modifying any files\n");
printf(" --help display this help message and exit\n");
printf(" --target=TARGET specify a target for cross-compilation\n");
printf(" --toolset=(host|target) which set of utilities we use\n");
printf(" --mode=MODE use operational mode MODE (you *must* set mode)\n");
printf(" --silent don't print informational messages\n");
printf(" --tag=TAG Ignored for libtool compatibility\n");
printf(" --version print version information\n");
printf(" --shared Build shared libraries when using --mode=link\n");
printf(" --export-all Try to export 'def' file on some platforms\n");
printf("\nMODE must be one of the following:\n\n");
printf(" compile compile a source file into a jlibtool object\n");
printf(" execute automatically set library path, then run a program\n");
printf(" install install libraries or executables\n");
printf(" link create a library or an executable\n");
printf("\nMODE-ARGS can be the following:\n\n");
printf(" -export-dynamic accepted and ignored\n");
printf(" -module create a module when linking\n");
printf(" -shared create a shared library when linking\n");
printf(" -prefer-pic prefer position-independent-code when compiling\n");
printf(" -prefer-non-pic prefer non position-independent-code when compiling\n");
printf(" -static create a static library when linking\n");
printf(" -no-install link libraries locally\n");
printf(" -rpath arg Set install path for shared libraries\n");
printf(" -l arg pass '-l arg' to the link stage\n");
printf(" -L arg pass '-L arg' to the link stage\n");
printf(" -R dir add 'dir' to runtime library search path.\n");
printf(" -Zexe accepted and ignored\n");
printf(" -avoid-version accepted and ignored\n");
exit(code);
}
#if defined(NEED_SNPRINTF)
/* Write at most n characters to the buffer in str, return the
* number of chars written or -1 if the buffer would have been
* overflowed.
*
* This is portable to any POSIX-compliant system has /dev/null
*/
static FILE *f = NULL;
static int vsnprintf(char *str, size_t n, char const *fmt, va_list ap)
{
int res;
if (!f) {
f = fopen("/dev/null","w");
}
if (!f) {
return -1;
}
setvbuf(f, str, _IOFBF, n);
res = vfprintf(f, fmt, ap);
if ((res > 0) && (res < n)) {
res = vsprintf( str, fmt, ap );
}
return res;
}
static int snprintf(char *str, size_t n, char const *fmt, ...)
{
va_list ap;
int res;
va_start(ap, fmt);
res = vsnprintf(str, n, fmt, ap);
va_end(ap);
return res;
}
#endif
static void strip_double_chars(char *str, char c)
{
size_t len = strlen(str);
char *p = str;
char *out = str;
char *end = p + len;
while (p < end) {
while ((p[0] == c) && (p[1] == c)) p++;
*out++ = *p++;
}
*out = '\0';
}
static void *lt_alloc_check(void *out)
{
if (!out) {
ERROR("Failed to allocate, OOM\n");
exit(1);
}
return out;
}
static void *lt_malloc(size_t size)
{
return lt_alloc_check(malloc(size));
}
static char *lt_strdup(char const *str)
{
return lt_alloc_check(strdup(str));
}
static void lt_const_free(const void *ptr)
{
void *tmp;
memcpy(&tmp, &ptr, sizeof(tmp));
free(tmp);
}
static void init_count_chars(count_chars *cc)
{
cc->vals = (char const**) lt_malloc(PATH_MAX*sizeof(char*));
cc->num = 0;
}
static count_chars *alloc_countchars(void)
{
count_chars *out;
out = lt_malloc(sizeof(count_chars));
init_count_chars(out);
return out;
}
static void clear_count_chars(count_chars *cc)
{
int i;
for (i = 0; i < cc->num; i++) {
cc->vals[i] = NULL;
}
cc->num = 0;
}
static void push_count_chars(count_chars *cc, char const *newval)
{
cc->vals[cc->num++] = newval;
}
static char const *pop_count_chars(count_chars *cc)
{
if (!cc->num) {
return NULL;
}
return cc->vals[--cc->num];
}
static void insert_count_chars(count_chars *cc, char const *newval, int position)
{
int i;
for (i = cc->num; i > position; i--) {
cc->vals[i] = cc->vals[i-1];
}
cc->vals[position] = newval;
cc->num++;
}
static void append_count_chars(count_chars *cc, count_chars *cctoadd)
{
int i;
for (i = 0; i < cctoadd->num; i++) {
if (cctoadd->vals[i]) {
push_count_chars(cc, cctoadd->vals[i]);
}
}
}
static char const *flatten_count_chars(count_chars *cc, char delim)
{
int i, size;
char *newval;
size = 0;
for (i = 0; i < cc->num; i++) {
if (cc->vals[i]) {
size += strlen(cc->vals[i]) + 1;
if (delim) {
size++;
}
}
}
newval = (char*)lt_malloc(size + 1);
newval[0] = '\0';
for (i = 0; i < cc->num; i++) {
if (cc->vals[i]) {
strcat(newval, cc->vals[i]);
if (delim) {
size_t len = strlen(newval);
newval[len] = delim;
newval[len + 1] = '\0';
}
}
}
return newval;
}
static char *shell_esc(char const *str)
{
int in_quote = 0;
char *cmd;
uint8_t *d;
uint8_t const *s;
cmd = (char *)lt_malloc(2 * strlen(str) + 3);
d = (unsigned char *)cmd;
s = (const unsigned char *)str;
if (IS_TARGET(ming32)) {
*d++ = '\"';
}
for (; *s; ++s) {
if (*s == '"') {
*d++ = '\\';
in_quote++;
}
else if (*s == '\\' || (*s == ' ' && (in_quote % 2))) {
*d++ = '\\';
}
*d++ = *s;
}
if (IS_TARGET(ming32)) {
*d++ = '\"';
}
*d = '\0';
return cmd;
}
static void external_spawn_sig_handler(int signo)
{
kill(spawn_pid, signo); /* Forward the signal to the process we're executing */
}
static void external_spawn_timeout(__attribute__((unused)) int pid)
{
timeout = true;
}
static int external_spawn(command_t *cmd, __attribute__((unused)) char const *file, char const **argv)
{
if (!cmd->options.silent) {
char const **argument = argv;
char **env_p = environ;
while (*env_p) {
NOTICE("Environment: %s\n", *env_p);
env_p++;
}
NOTICE("Executing: ");
while (*argument) {
NOTICE("%s ", *argument);
argument++;
}
puts("");
}
if (cmd->options.dry_run) {
return 0;
}
#if defined(__EMX__) || defined(__MINGW32__)
return spawnvp(P_WAIT, argv[0], argv);
#else
{
/*
* Signals we forward to our executing process
*/
spawn_pid = fork();
if (spawn_pid == 0) {
return execvp(argv[0], UNCONST(char **, argv));
}
else if (spawn_pid < 0) {
fprintf(stderr, "Failed fork: %s\n", strerror(errno));
return -1;
}
else {
int status;
#define SIGNAL_FORWARD(_sig) if (signal(_sig, external_spawn_sig_handler) == SIG_ERR) \
do { \
fprintf(stderr, "Failed setting signal handler for %i: %s\n", _sig, strerror(errno)); \
exit(EXIT_FAILURE); \
} while(0)
#define SIGNAL_RESET(_sig) signal(_sig, SIG_DFL)
SIGNAL_FORWARD(SIGHUP);
SIGNAL_FORWARD(SIGINT);
SIGNAL_FORWARD(SIGQUIT);
SIGNAL_FORWARD(SIGTRAP);
SIGNAL_FORWARD(SIGPIPE);
SIGNAL_FORWARD(SIGTERM);
SIGNAL_FORWARD(SIGUSR1);
SIGNAL_FORWARD(SIGUSR2);
/*
* Deliver's SIGALRM after N seconds
*/
if (cmd->timeout > 0) {
/*
* Seems like SA_RESTART is set
* implicitly when signal() is
* used, which is NOT what we want.
*/
sigaction(SIGALRM,
&(struct sigaction){
.sa_handler = external_spawn_timeout,
.sa_flags = SA_RESETHAND
}, NULL);
alarm(cmd->timeout);
}
waitpid(spawn_pid, &status, 0);
if (cmd->timeout > 0) {
signal(SIGALRM, NULL);
alarm(0);
}
SIGNAL_RESET(SIGHUP);
SIGNAL_RESET(SIGINT);
SIGNAL_RESET(SIGQUIT);
SIGNAL_RESET(SIGTRAP);
SIGNAL_RESET(SIGPIPE);
SIGNAL_RESET(SIGTERM);
SIGNAL_RESET(SIGUSR1);
SIGNAL_RESET(SIGUSR2);
/*
* We're assuming waitpid was delivered
* because of the alarm we set.
*
* Kill the child and clean it up.
*/
if (timeout) {
NOTICE("exec timeout\n");
kill(spawn_pid, SIGALRM);
waitpid(spawn_pid, &status, 0); /* Cleanup child state */
timeout = false; /* Reset */
}
/*
* Exited via exit(status)
*/
if (WIFEXITED(status)) {
return WEXITSTATUS(status);
}
#ifdef WTERMSIG
if (WIFSIGNALED(status)) {
return WTERMSIG(status);
}
#endif
/*
* Some other failure.
*/
return 1;
}
}
#endif
}
static int run_command(command_t *cmd, count_chars *cc)
{
int ret;