X-Git-Url: https://git.distorted.org.uk/~mdw/mLib/blobdiff_plain/8128dcdfa57d0a792c4e221aae5b4886eaef72d0..d056fbdff1c5a26be055c38eee4c273ee6a0cba7:/sys/mdup.3 diff --git a/sys/mdup.3 b/sys/mdup.3 index 9ce3b85..567da6a 100644 --- a/sys/mdup.3 +++ b/sys/mdup.3 @@ -17,15 +17,31 @@ \h'-\w'\\$1\ 'u'\\$1\ \c .ft P .. -.ie t .ds o \(bu -.el .ds o o +.ie t \{\ +. ds o \(bu +. de VP +. sp .4v +.. +\} +.el \{\ +. ds o o +. de VP +. sp +.. +\} .TH mdup 3 "4 January" "Straylight/Edgeware" "mLib utilities library" .SH NAME mdup \- renumber file descriptors .SH SYNOPSIS .nf .B "#include " - +.PP +.ta 2n +.B "typedef struct {" +.B " int cur;" +.B " int want;" +.B "} mdup_fd;" +.PP .BI "int mdup(mdup_fd *" v ", size_t " n ");" .fi .SH DESCRIPTION @@ -41,16 +57,7 @@ to a vector of .B mdup_fd structures, and the length .I n -of this vector, in elements. The -.B mdup_fd -structure is defined as -.VS -typedef struct mdup_fd { - int cur; - int want; -} mdup_fd; -.VE -Each `slot' (element) in the vector +of this vector, in elements. Each `slot' (element) in the vector .I v represents a file. The slot's .B cur @@ -116,17 +123,18 @@ this. int p_in[2] = P_INIT, p_out[2] = P_INIT, p_err[2] = P_INIT; pid_t kid = -1; int i; - +.VP if (pipe(p_in) || pipe(p_out) || pipe(p_err)) goto error; if ((kid = fork()) < 0) goto error; if (!kid) { - if (dup2(p_in[0], STDIN_FILENO) < 0 || - dup2(p_out[1], STDOUT_FILENO) < 0 || - dup2(p_err[2], STDERR_FILENO) < 0 || - close(p_in[0]) || close(p_out[0]) || close(p_err[0]) || - close(p_in[1]) || close(p_out[1]) || close(p_err[1])) - _exit(127); - execvp("/bin/sh", "sh", "-c", "...", (char *)0); +.ta 2n 4n 2n+\w'\fBif ('u + if (dup2(p_in[0], STDIN_FILENO) < 0 || + dup2(p_out[1], STDOUT_FILENO) < 0 || + dup2(p_err[2], STDERR_FILENO) < 0 || + close(p_in[0]) || close(p_out[0]) || close(p_err[0]) || + close(p_in[1]) || close(p_out[1]) || close(p_err[1])) + _exit(127); + execvp("/bin/sh", "sh", "-c", "...", (char *)0); } \&... .VE @@ -149,22 +157,23 @@ the child. Here's how to rewrite the above function using .BR mdup . .VS +.ta 2n 4n 2n+\w'\fBmd[0].cur = p_out[1]; 'u #define P_INIT { \-1, \-1 } int p_in[2] = P_INIT, p_out[2] = P_INIT, p_err[2] = P_INIT; pid_t kid = -1; mdup_fd md[3]; int i; - +.VP if (pipe(p_in) || pipe(p_out) || pipe(p_err)) goto error; if ((kid = fork()) < 0) goto error; if (!kid) { - if (close(p_in[1] || close(p_out[0]) || close(p_err[0])) - goto _exit(127); - md[0].cur = p_in[0]; md[0].want = STDIN_FILENO; - md[1].cur = p_out[1]; md[1].want = STDOUT_FILENO; - md[2].cur = p_err[1]; md[2].want = STDERR_FILENO; - if (mdup(md, 3)) _exit(127); - execvp("/bin/sh", "sh", "-c", "...", (char *)0); + if (close(p_in[1] || close(p_out[0]) || close(p_err[0])) + goto _exit(127); + md[0].cur = p_in[0]; md[0].want = STDIN_FILENO; + md[1].cur = p_out[1]; md[1].want = STDOUT_FILENO; + md[2].cur = p_err[1]; md[2].want = STDERR_FILENO; + if (mdup(md, 3)) _exit(127); + execvp("/bin/sh", "sh", "-c", "...", (char *)0); } \&... .VE @@ -185,4 +194,3 @@ wanted ends of the pipes. .BR mLib (3). .SH AUTHOR Mark Wooding, -