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
|
/*******************************************************************/
/* slibtool: a skinny libtool implementation, written in C */
/* Copyright (C) 2016 Z. Gilboa */
/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
/*******************************************************************/
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/wait.h>
#include <slibtool/slibtool.h>
#include "slibtool_spawn_impl.h"
static char * slbt_mri_argument(
char * arg,
char * buf)
{
int i;
char * lnk;
char mricwd[PATH_MAX];
char target[PATH_MAX];
if ((!(strchr(arg,'+'))) && (!(strchr(arg,'-'))))
return arg;
if (!(getcwd(mricwd,sizeof(mricwd))))
return 0;
if ((size_t)snprintf(target,sizeof(target),"%s/%s",
mricwd,arg) >= sizeof(target))
return 0;
for (i=0,lnk=0; i<1024 && !lnk; i++) {
if (!(tmpnam(buf)))
return 0;
if (!(symlink(target,buf)))
lnk = buf;
}
return lnk;
}
static int slbt_archive_import_child(
char * program,
int fd[2])
{
char * argv[3];
argv[0] = program;
argv[1] = "-M";
argv[2] = 0;
close(fd[1]);
close(0);
if (dup(fd[0]) == 0)
execvp(program,argv);
exit(EXIT_FAILURE);
return -1;
}
int slbt_archive_import(
const struct slbt_driver_ctx * dctx,
struct slbt_exec_ctx * ectx,
char * dstarchive,
char * srcarchive)
{
int ret;
pid_t pid;
pid_t rpid;
int fd[2];
FILE * fout;
char * dst;
char * src;
char mridst [L_tmpnam];
char mrisrc [L_tmpnam];
char program[PATH_MAX];
if ((size_t)snprintf(program,sizeof(program),"%s",
dctx->cctx->host.ar) >= sizeof(program))
return -1;
if (pipe(fd))
return -1;
if ((pid = fork()) < 0) {
close(fd[0]);
close(fd[1]);
return -1;
}
if (pid == 0)
return slbt_archive_import_child(
program,
fd);
ectx->pid = pid;
dst = slbt_mri_argument(dstarchive,mridst);
src = slbt_mri_argument(srcarchive,mrisrc);
if ((fout = fdopen(fd[1],"a"))) {
ret = (fprintf(
fout,
"OPEN %s\n"
"ADDLIB %s\n"
"SAVE\n"
"END\n",
dst,
src) < 0)
? -1 : 0;
fclose(fout);
close(fd[0]);
} else {
ret = -1;
close(fd[0]);
close(fd[1]);
}
rpid = waitpid(
pid,
&ectx->exitcode,
0);
if (dst == mridst)
unlink(dst);
if (src == mrisrc)
unlink(src);
return ret || (rpid != pid) || ectx->exitcode
? -1 : 0;
}
|