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
|
/* ISC license. */
#include <string.h>
#include <unistd.h>
#include <skalibs/sgetopt.h>
#include <skalibs/bytestr.h>
#include <skalibs/types.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/strerr2.h>
#include <skalibs/djbunix.h>
#define USAGE "heredoc [ -d ] fd string command..."
#define dieusage() strerr_dieusage(100, USAGE)
int main (int argc, char const *const *argv, char const *const *envp)
{
int df = 0 ;
PROG = "heredoc" ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
int opt = subgetopt_r(argc, argv, "d", &l) ;
if (opt == -1) break ;
switch (opt)
{
case 'd' : df = 1 ; break ;
default : dieusage() ;
}
}
argc -= l.ind ; argv += l.ind ;
}
if (argc < 3) dieusage() ;
{
int fd[2] ;
unsigned int fdr ;
pid_t pid ;
if (!uint0_scan(argv[0], &fdr)) strerr_dieusage(100, USAGE) ;
if (pipe(fd) < 0) strerr_diefu1sys(111, "pipe") ;
pid = df ? doublefork() : fork() ;
switch (pid)
{
case -1: strerr_diefu2sys(111, df ? "double" : "", "fork") ;
case 0:
{
size_t len = strlen(argv[1]) ;
PROG = "heredoc (child)" ;
fd_close(fd[0]) ;
if (allwrite(fd[1], argv[1], len) < len)
strerr_diefu1sys(111, "allwrite") ;
return 0 ;
}
}
fd_close(fd[1]) ;
if (fd_move((int)fdr, fd[0]) == -1)
strerr_diefu2sys(111, "read on fd ", argv[0]) ;
}
pathexec_run(argv[2], argv+2, envp) ;
strerr_dieexec(111, argv[2]) ;
}
|