Changeset 3449 for trunk/src/kash/redir.c
- Timestamp:
- Sep 13, 2020, 1:17:09 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/redir.c
r3445 r3449 43 43 #include <sys/types.h> 44 44 #include <limits.h> /* PIPE_BUF */ 45 #include <assert.h> 45 46 #include <string.h> 46 47 #include <errno.h> 48 #include <stddef.h> 47 49 #include <stdlib.h> 48 50 … … 88 90 //int fd0_redirected = 0; 89 91 90 STATIC void openredirect(shinstance *, union node *, char[10], int );92 STATIC void openredirect(shinstance *, union node *, char[10], int, const char *); 91 93 STATIC int openhere(shinstance *, union node *); 92 94 … … 114 116 psh->fd0_redirected = inherit->fd0_redirected; 115 117 } 118 119 /* Copy the expanded redirection filenames (stack), but only the last entry 120 as the subshell does not have the ability to unwind stack in the parent 121 and therefore cannot get to the earlier redirection stuff: */ 122 if (inherit->expfnames) 123 { 124 redirexpfnames * const expfnamesrc = inherit->expfnames; 125 unsigned i = expfnamesrc->count; 126 redirexpfnames *dst = stalloc(psh, offsetof(redirexpfnames, names) + sizeof(dst->names[0]) * i); 127 dst->count = i; 128 dst->depth = 1; 129 dst->prev = NULL; 130 while (i-- > 0) 131 dst->names[i] = stsavestr(psh, expfnamesrc->names[i]); 132 psh->expfnames = dst; 133 } 116 134 } 117 135 #endif /* !SH_FORKED_MODE */ … … 135 153 int try; 136 154 char memory[10]; /* file descriptors to write to memory */ 155 unsigned idxexpfname; 137 156 138 157 for (i = 10 ; --i >= 0 ; ) … … 146 165 psh->redirlist = sv; 147 166 } 148 for (n = redir ; n ; n = n->nfile.next) { 167 idxexpfname = 0; 168 for (n = redir, idxexpfname = 0 ; n ; n = n->nfile.next, idxexpfname++) { 169 assert(idxexpfname < psh->expfnames->count); 149 170 fd = n->nfile.fd; 150 171 try = 0; … … 160 181 case EBADF: 161 182 if (!try) { 162 openredirect(psh, n, memory, flags );183 openredirect(psh, n, memory, flags, psh->expfnames->names[idxexpfname]); 163 184 try++; 164 185 goto again; … … 182 203 psh->fd0_redirected++; 183 204 if (!try) 184 openredirect(psh, n, memory, flags); 185 } 205 openredirect(psh, n, memory, flags, psh->expfnames->names[idxexpfname]); 206 } 207 assert(!redir || idxexpfname == psh->expfnames->count); 186 208 if (memory[1]) 187 209 psh->out1 = &psh->memout; … … 192 214 193 215 STATIC void 194 openredirect(shinstance *psh, union node *redir, char memory[10], int flags )216 openredirect(shinstance *psh, union node *redir, char memory[10], int flags, const char *fname) 195 217 { 196 218 int fd = redir->nfile.fd; 197 char *fname;198 219 int f; 199 220 int oflags = O_WRONLY|O_CREAT|O_TRUNC; … … 208 229 switch (redir->nfile.type) { 209 230 case NFROM: 210 fname = redir->nfile.expfname;211 231 if ((f = shfile_open(&psh->fdtab, fname, O_RDONLY, 0)) < 0) 212 232 goto eopen; 213 233 break; 214 234 case NFROMTO: 215 fname = redir->nfile.expfname;216 235 if ((f = shfile_open(&psh->fdtab, fname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) 217 236 goto ecreate; … … 222 241 /* FALLTHROUGH */ 223 242 case NCLOBBER: 224 fname = redir->nfile.expfname;225 243 if ((f = shfile_open(&psh->fdtab, fname, oflags, 0666)) < 0) 226 244 goto ecreate; 227 245 break; 228 246 case NAPPEND: 229 fname = redir->nfile.expfname;230 247 if ((f = shfile_open(&psh->fdtab, fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) 231 248 goto ecreate;
Note:
See TracChangeset
for help on using the changeset viewer.