1/*- 2 * Copyright (c) 2002 Tim J. Robbins. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include "namespace.h" 28#include <sys/cdefs.h> 29#include <sys/types.h> 30#include <sys/wait.h> 31#include <fcntl.h> 32#include <paths.h> 33#include <stdio.h> 34#include <stdlib.h> 35#include <string.h> 36#include <unistd.h> 37#include <wordexp.h> 38#include "un-namespace.h" 39
|
41 42static int we_askshell(const char *, wordexp_t *, int); 43static int we_check(const char *, int); 44 45/* 46 * wordexp -- 47 * Perform shell word expansion on `words' and place the resulting list 48 * of words in `we'. See wordexp(3). 49 * 50 * Specified by IEEE Std. 1003.1-2001. 51 */ 52int 53wordexp(const char * __restrict words, wordexp_t * __restrict we, int flags) 54{ 55 int error; 56 57 if (flags & WRDE_REUSE) 58 wordfree(we); 59 if ((flags & WRDE_APPEND) == 0) { 60 we->we_wordc = 0; 61 we->we_wordv = NULL; 62 we->we_strings = NULL; 63 we->we_nbytes = 0; 64 } 65 if ((error = we_check(words, flags)) != 0) { 66 wordfree(we); 67 return (error); 68 } 69 if ((error = we_askshell(words, we, flags)) != 0) { 70 wordfree(we); 71 return (error); 72 } 73 return (0); 74} 75 76/* 77 * we_askshell -- 78 * Use the `wordexp' /bin/sh builtin function to do most of the work 79 * in expanding the word string. This function is complicated by 80 * memory management. 81 */ 82static int 83we_askshell(const char *words, wordexp_t *we, int flags) 84{ 85 int pdes[2]; /* Pipe to child */ 86 char bbuf[9]; /* Buffer for byte count */ 87 char wbuf[9]; /* Buffer for word count */ 88 long nwords, nbytes; /* Number of words, bytes from child */ 89 long i; /* Handy integer */ 90 size_t sofs; /* Offset into we->we_strings */ 91 size_t vofs; /* Offset into we->we_wordv */ 92 pid_t pid; /* Process ID of child */ 93 int status; /* Child exit status */ 94 char *ifs; /* IFS env. var. */ 95 char *np, *p; /* Handy pointers */ 96 char *nstrings; /* Temporary for realloc() */ 97 char **nwv; /* Temporary for realloc() */ 98 99 if ((ifs = getenv("IFS")) == NULL) 100 ifs = " \t\n"; 101 102 if (pipe(pdes) < 0) 103 return (WRDE_NOSPACE); /* XXX */ 104 if ((pid = fork()) < 0) { 105 close(pdes[0]); 106 close(pdes[1]); 107 return (WRDE_NOSPACE); /* XXX */ 108 } 109 else if (pid == 0) { 110 /* 111 * We are the child; just get /bin/sh to run the wordexp 112 * builtin on `words'. 113 */ 114 int devnull; 115 char *cmd; 116 117 close(pdes[0]); 118 if (dup2(pdes[1], STDOUT_FILENO) < 0) 119 _exit(1); 120 close(pdes[1]); 121 if (asprintf(&cmd, "wordexp%c%s\n", *ifs, words) < 0) 122 _exit(1); 123 if ((flags & WRDE_SHOWERR) == 0) { 124 if ((devnull = open(_PATH_DEVNULL, O_RDWR, 0666)) < 0) 125 _exit(1); 126 if (dup2(devnull, STDERR_FILENO) < 0) 127 _exit(1); 128 close(devnull); 129 } 130 execl(_PATH_BSHELL, "sh", flags & WRDE_UNDEF ? "-u" : "+u", 131 "-c", cmd, NULL); 132 _exit(1); 133 } 134 135 /* 136 * We are the parent; read the output of the shell wordexp function, 137 * which is a 32-bit hexadecimal word count, a 32-bit hexadecimal 138 * byte count (not including terminating null bytes), followed by 139 * the expanded words separated by nulls. 140 */ 141 close(pdes[1]); 142 if (read(pdes[0], wbuf, 8) != 8 || read(pdes[0], bbuf, 8) != 8) { 143 close(pdes[0]); 144 return (WRDE_NOSPACE); /* XXX */ 145 } 146 wbuf[8] = bbuf[8] = '\0'; 147 nwords = strtol(wbuf, NULL, 16); 148 nbytes = strtol(bbuf, NULL, 16) + nwords; 149 150 /* 151 * Allocate or reallocate (when flags & WRDE_APPEND) the word vector 152 * and string storage buffers for the expanded words we're about to 153 * read from the child. 154 */ 155 sofs = we->we_nbytes; 156 vofs = we->we_wordc; 157 we->we_wordc += nwords; 158 we->we_nbytes += nbytes; 159 if ((nwv = realloc(we->we_wordv, (we->we_wordc + 1 + 160 (flags & WRDE_DOOFS ? we->we_offs : 0)) * 161 sizeof(char *))) == NULL) { 162 close(pdes[0]); 163 return (WRDE_NOSPACE); 164 } 165 we->we_wordv = nwv; 166 if ((nstrings = realloc(we->we_strings, we->we_nbytes)) == NULL) { 167 close(pdes[0]); 168 return (WRDE_NOSPACE); 169 } 170 for (i = 0; i < vofs; i++) 171 if (we->we_wordv[i] != NULL) 172 we->we_wordv[i] += nstrings - we->we_strings; 173 we->we_strings = nstrings; 174 175 if (read(pdes[0], we->we_strings + sofs, nbytes) != nbytes) { 176 close(pdes[0]); 177 return (WRDE_NOSPACE); /* XXX */ 178 } 179 180 if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || 181 WEXITSTATUS(status) != 0) { 182 close(pdes[0]); 183 return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX); 184 } 185 close(pdes[0]); 186 187 /* 188 * Break the null-terminated expanded word strings out into 189 * the vector. 190 */ 191 if (vofs == 0 && flags & WRDE_DOOFS) 192 while (vofs < we->we_offs) 193 we->we_wordv[vofs++] = NULL; 194 p = we->we_strings + sofs; 195 while (nwords-- != 0) { 196 we->we_wordv[vofs++] = p; 197 if ((np = memchr(p, '\0', nbytes)) == NULL) 198 return (WRDE_NOSPACE); /* XXX */ 199 nbytes -= np - p + 1; 200 p = np + 1; 201 } 202 we->we_wordv[vofs] = NULL; 203 204 return (0); 205} 206 207/* 208 * we_check -- 209 * Check that the string contains none of the following unquoted 210 * special characters: <newline> |&;<>(){} 211 * or command substitutions when WRDE_NOCMD is set in flags. 212 */
|