wordexp.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282
  1. /* vi: set sw=4 ts=4: */
  2. /* POSIX.2 wordexp implementation.
  3. Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Tim Waugh <tim@cyberelk.demon.co.uk>.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB. If not,
  16. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA. */
  18. #define _GNU_SOURCE
  19. #include <features.h>
  20. #include <sys/types.h>
  21. #include <sys/wait.h>
  22. #include <fcntl.h>
  23. #include <paths.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <pwd.h>
  29. #include <errno.h>
  30. #include <assert.h>
  31. #include <fnmatch.h>
  32. #include <glob.h>
  33. #include <wordexp.h>
  34. libc_hidden_proto(mempcpy)
  35. libc_hidden_proto(stpcpy)
  36. libc_hidden_proto(strchr)
  37. libc_hidden_proto(strcpy)
  38. libc_hidden_proto(strdup)
  39. libc_hidden_proto(strlen)
  40. libc_hidden_proto(strndup)
  41. libc_hidden_proto(strspn)
  42. libc_hidden_proto(strcspn)
  43. libc_hidden_proto(setenv)
  44. libc_hidden_proto(unsetenv)
  45. libc_hidden_proto(waitpid)
  46. libc_hidden_proto(kill)
  47. libc_hidden_proto(getuid)
  48. libc_hidden_proto(getpwnam_r)
  49. libc_hidden_proto(getpwuid_r)
  50. libc_hidden_proto(execve)
  51. libc_hidden_proto(dup2)
  52. libc_hidden_proto(atoi)
  53. libc_hidden_proto(fnmatch)
  54. libc_hidden_proto(pipe)
  55. libc_hidden_proto(fork)
  56. libc_hidden_proto(open)
  57. libc_hidden_proto(close)
  58. libc_hidden_proto(read)
  59. libc_hidden_proto(getenv)
  60. libc_hidden_proto(getpid)
  61. libc_hidden_proto(sprintf)
  62. libc_hidden_proto(fprintf)
  63. libc_hidden_proto(abort)
  64. extern void __wordfree (wordexp_t *__wordexp) __THROW attribute_hidden;
  65. extern int __glob (__const char *__restrict __pattern, int __flags,
  66. int (*__errfunc) (__const char *, int),
  67. glob_t *__restrict __pglob) __THROW attribute_hidden;
  68. extern void __globfree (glob_t *__pglob) __THROW attribute_hidden;
  69. #define __WORDEXP_FULL
  70. //#undef __WORDEXP_FULL
  71. /*
  72. * This is a recursive-descent-style word expansion routine.
  73. */
  74. /* These variables are defined and initialized in the startup code. */
  75. //extern int __libc_argc;
  76. //extern char **__libc_argv;
  77. /* FIXME!!!! */
  78. int __libc_argc;
  79. char **__libc_argv;
  80. /* Some forward declarations */
  81. static int parse_dollars(char **word, size_t * word_length,
  82. size_t * max_length, const char *words,
  83. size_t * offset, int flags, wordexp_t * pwordexp,
  84. const char *ifs, const char *ifs_white,
  85. int quoted);
  86. static int parse_backtick(char **word, size_t * word_length,
  87. size_t * max_length, const char *words,
  88. size_t * offset, int flags, wordexp_t * pwordexp,
  89. const char *ifs, const char *ifs_white);
  90. static int parse_dquote(char **word, size_t * word_length,
  91. size_t * max_length, const char *words,
  92. size_t * offset, int flags, wordexp_t * pwordexp,
  93. const char *ifs, const char *ifs_white);
  94. /* The w_*() functions manipulate word lists. */
  95. #define W_CHUNK (100)
  96. /* Result of w_newword will be ignored if it's the last word. */
  97. static inline char *w_newword(size_t * actlen, size_t * maxlen)
  98. {
  99. *actlen = *maxlen = 0;
  100. return NULL;
  101. }
  102. /* Add a character to the buffer, allocating room for it if needed. */
  103. static inline char *w_addchar(char *buffer, size_t * actlen,
  104. size_t * maxlen, char ch)
  105. /* (lengths exclude trailing zero) */
  106. {
  107. if (*actlen == *maxlen) {
  108. char *old_buffer = buffer;
  109. assert(buffer == NULL || *maxlen != 0);
  110. *maxlen += W_CHUNK;
  111. buffer = realloc(buffer, 1 + *maxlen);
  112. if (buffer == NULL)
  113. free(old_buffer);
  114. }
  115. if (buffer != NULL) {
  116. buffer[*actlen] = ch;
  117. buffer[++(*actlen)] = '\0';
  118. }
  119. return buffer;
  120. }
  121. #define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
  122. static char *w_addmem(char *buffer, size_t * actlen, size_t * maxlen,
  123. const char *str, size_t len)
  124. {
  125. /* Add a string to the buffer, allocating room for it if needed.
  126. */
  127. if (*actlen + len > *maxlen) {
  128. char *old_buffer = buffer;
  129. assert(buffer == NULL || *maxlen != 0);
  130. *maxlen += MAX(2 * len, W_CHUNK);
  131. buffer = realloc(old_buffer, 1 + *maxlen);
  132. if (buffer == NULL)
  133. free(old_buffer);
  134. }
  135. if (buffer != NULL) {
  136. *((char *) mempcpy(&buffer[*actlen], str, len)) = '\0';
  137. *actlen += len;
  138. }
  139. return buffer;
  140. }
  141. /* Add a string to the buffer, allocating room for it if needed. */
  142. static char *w_addstr(char *buffer, size_t * actlen, size_t * maxlen,
  143. const char *str)
  144. /* (lengths exclude trailing zero) */
  145. {
  146. size_t len;
  147. assert(str != NULL); /* w_addstr only called from this file */
  148. len = strlen(str);
  149. return w_addmem(buffer, actlen, maxlen, str, len);
  150. }
  151. /* Add a word to the wordlist */
  152. static int w_addword(wordexp_t * pwordexp, char *word)
  153. {
  154. size_t num_p;
  155. char **new_wordv;
  156. /* Internally, NULL acts like "". Convert NULLs to "" before
  157. * the caller sees them.
  158. */
  159. if (word == NULL) {
  160. word = strdup("");
  161. if (word == NULL)
  162. goto no_space;
  163. }
  164. num_p = 2 + pwordexp->we_wordc + pwordexp->we_offs;
  165. new_wordv = realloc(pwordexp->we_wordv, sizeof(char *) * num_p);
  166. if (new_wordv != NULL) {
  167. pwordexp->we_wordv = new_wordv;
  168. pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc++] = word;
  169. pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc] = NULL;
  170. return 0;
  171. }
  172. no_space:
  173. return WRDE_NOSPACE;
  174. }
  175. /* The parse_*() functions should leave *offset being the offset in 'words'
  176. * to the last character processed.
  177. */
  178. static int
  179. parse_backslash(char **word, size_t * word_length, size_t * max_length,
  180. const char *words, size_t * offset)
  181. {
  182. /* We are poised _at_ a backslash, not in quotes */
  183. switch (words[1 + *offset]) {
  184. case 0:
  185. /* Backslash is last character of input words */
  186. return WRDE_SYNTAX;
  187. case '\n':
  188. ++(*offset);
  189. break;
  190. default:
  191. *word = w_addchar(*word, word_length, max_length, words[1 + *offset]);
  192. if (*word == NULL)
  193. return WRDE_NOSPACE;
  194. ++(*offset);
  195. break;
  196. }
  197. return 0;
  198. }
  199. static int
  200. parse_qtd_backslash(char **word, size_t * word_length, size_t * max_length,
  201. const char *words, size_t * offset)
  202. {
  203. /* We are poised _at_ a backslash, inside quotes */
  204. switch (words[1 + *offset]) {
  205. case 0:
  206. /* Backslash is last character of input words */
  207. return WRDE_SYNTAX;
  208. case '\n':
  209. ++(*offset);
  210. break;
  211. case '$':
  212. case '`':
  213. case '"':
  214. case '\\':
  215. *word =
  216. w_addchar(*word, word_length, max_length, words[1 + *offset]);
  217. if (*word == NULL)
  218. return WRDE_NOSPACE;
  219. ++(*offset);
  220. break;
  221. default:
  222. *word = w_addchar(*word, word_length, max_length, words[*offset]);
  223. if (*word != NULL)
  224. *word =
  225. w_addchar(*word, word_length, max_length,
  226. words[1 + *offset]);
  227. if (*word == NULL)
  228. return WRDE_NOSPACE;
  229. ++(*offset);
  230. break;
  231. }
  232. return 0;
  233. }
  234. static int
  235. parse_tilde(char **word, size_t * word_length, size_t * max_length,
  236. const char *words, size_t * offset, size_t wordc)
  237. {
  238. /* We are poised _at_ a tilde */
  239. size_t i;
  240. if (*word_length != 0) {
  241. if (!((*word)[*word_length - 1] == '=' && wordc == 0)) {
  242. if (!((*word)[*word_length - 1] == ':'
  243. && strchr(*word, '=') && wordc == 0)) {
  244. *word = w_addchar(*word, word_length, max_length, '~');
  245. return *word ? 0 : WRDE_NOSPACE;
  246. }
  247. }
  248. }
  249. for (i = 1 + *offset; words[i]; i++) {
  250. if (words[i] == ':' || words[i] == '/' || words[i] == ' ' ||
  251. words[i] == '\t' || words[i] == 0)
  252. break;
  253. if (words[i] == '\\') {
  254. *word = w_addchar(*word, word_length, max_length, '~');
  255. return *word ? 0 : WRDE_NOSPACE;
  256. }
  257. }
  258. if (i == 1 + *offset) {
  259. /* Tilde appears on its own */
  260. uid_t uid;
  261. struct passwd pwd, *tpwd;
  262. int buflen = 1000;
  263. char *home;
  264. char *buffer;
  265. int result;
  266. /* POSIX.2 says ~ expands to $HOME and if HOME is unset the
  267. results are unspecified. We do a lookup on the uid if
  268. HOME is unset. */
  269. home = getenv("HOME");
  270. if (home != NULL) {
  271. *word = w_addstr(*word, word_length, max_length, home);
  272. if (*word == NULL)
  273. return WRDE_NOSPACE;
  274. } else {
  275. uid = getuid();
  276. buffer = alloca(buflen);
  277. while ((result = getpwuid_r(uid, &pwd, buffer, buflen, &tpwd))
  278. != 0 && errno == ERANGE)
  279. {
  280. buflen += 1000;
  281. buffer = alloca(buflen);
  282. }
  283. if (result == 0 && tpwd != NULL && pwd.pw_dir != NULL) {
  284. *word = w_addstr(*word, word_length, max_length, pwd.pw_dir);
  285. if (*word == NULL)
  286. return WRDE_NOSPACE;
  287. } else {
  288. *word = w_addchar(*word, word_length, max_length, '~');
  289. if (*word == NULL)
  290. return WRDE_NOSPACE;
  291. }
  292. }
  293. } else {
  294. /* Look up user name in database to get home directory */
  295. char *user = strndup(&words[1 + *offset], i - (1 + *offset));
  296. struct passwd pwd, *tpwd;
  297. int buflen = 1000;
  298. char *buffer = alloca(buflen);
  299. int result;
  300. while ((result = getpwnam_r(user, &pwd, buffer, buflen, &tpwd)) != 0
  301. && errno == ERANGE) {
  302. buflen += 1000;
  303. buffer = alloca(buflen);
  304. }
  305. if (result == 0 && tpwd != NULL && pwd.pw_dir)
  306. *word = w_addstr(*word, word_length, max_length, pwd.pw_dir);
  307. else {
  308. /* (invalid login name) */
  309. *word = w_addchar(*word, word_length, max_length, '~');
  310. if (*word != NULL)
  311. *word = w_addstr(*word, word_length, max_length, user);
  312. }
  313. *offset = i - 1;
  314. }
  315. return *word ? 0 : WRDE_NOSPACE;
  316. }
  317. static int
  318. do_parse_glob(const char *glob_word, char **word, size_t * word_length,
  319. size_t * max_length, wordexp_t * pwordexp, const char *ifs,
  320. const char *ifs_white)
  321. {
  322. int error;
  323. int match;
  324. glob_t globbuf;
  325. error = __glob(glob_word, GLOB_NOCHECK, NULL, &globbuf);
  326. if (error != 0) {
  327. /* We can only run into memory problems. */
  328. assert(error == GLOB_NOSPACE);
  329. return WRDE_NOSPACE;
  330. }
  331. if (ifs && !*ifs) {
  332. /* No field splitting allowed. */
  333. assert(globbuf.gl_pathv[0] != NULL);
  334. *word = w_addstr(*word, word_length, max_length, globbuf.gl_pathv[0]);
  335. for (match = 1; match < globbuf.gl_pathc && *word != NULL; ++match) {
  336. *word = w_addchar(*word, word_length, max_length, ' ');
  337. if (*word != NULL)
  338. *word = w_addstr(*word, word_length, max_length,
  339. globbuf.gl_pathv[match]);
  340. }
  341. __globfree(&globbuf);
  342. return *word ? 0 : WRDE_NOSPACE;
  343. }
  344. assert(ifs == NULL || *ifs != '\0');
  345. if (*word != NULL) {
  346. free(*word);
  347. *word = w_newword(word_length, max_length);
  348. }
  349. for (match = 0; match < globbuf.gl_pathc; ++match) {
  350. char *matching_word = strdup(globbuf.gl_pathv[match]);
  351. if (matching_word == NULL || w_addword(pwordexp, matching_word)) {
  352. __globfree(&globbuf);
  353. return WRDE_NOSPACE;
  354. }
  355. }
  356. __globfree(&globbuf);
  357. return 0;
  358. }
  359. static int
  360. parse_glob(char **word, size_t * word_length, size_t * max_length,
  361. const char *words, size_t * offset, int flags,
  362. wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
  363. {
  364. /* We are poised just after a '*', a '[' or a '?'. */
  365. int error = WRDE_NOSPACE;
  366. int quoted = 0; /* 1 if singly-quoted, 2 if doubly */
  367. int i;
  368. wordexp_t glob_list; /* List of words to glob */
  369. glob_list.we_wordc = 0;
  370. glob_list.we_wordv = NULL;
  371. glob_list.we_offs = 0;
  372. for (; words[*offset] != '\0'; ++*offset) {
  373. if ((ifs && strchr(ifs, words[*offset])) ||
  374. (!ifs && strchr(" \t\n", words[*offset])))
  375. /* Reached IFS */
  376. break;
  377. /* Sort out quoting */
  378. if (words[*offset] == '\'') {
  379. if (quoted == 0) {
  380. quoted = 1;
  381. continue;
  382. } else if (quoted == 1) {
  383. quoted = 0;
  384. continue;
  385. }
  386. } else if (words[*offset] == '"') {
  387. if (quoted == 0) {
  388. quoted = 2;
  389. continue;
  390. } else if (quoted == 2) {
  391. quoted = 0;
  392. continue;
  393. }
  394. }
  395. /* Sort out other special characters */
  396. if (quoted != 1 && words[*offset] == '$') {
  397. error = parse_dollars(word, word_length, max_length, words,
  398. offset, flags, &glob_list, ifs,
  399. ifs_white, quoted == 2);
  400. if (error)
  401. goto tidy_up;
  402. continue;
  403. } else if (words[*offset] == '\\') {
  404. if (quoted)
  405. error = parse_qtd_backslash(word, word_length, max_length,
  406. words, offset);
  407. else
  408. error = parse_backslash(word, word_length, max_length,
  409. words, offset);
  410. if (error)
  411. goto tidy_up;
  412. continue;
  413. }
  414. *word = w_addchar(*word, word_length, max_length, words[*offset]);
  415. if (*word == NULL)
  416. goto tidy_up;
  417. }
  418. /* Don't forget to re-parse the character we stopped at. */
  419. --*offset;
  420. /* Glob the words */
  421. error = w_addword(&glob_list, *word);
  422. *word = w_newword(word_length, max_length);
  423. for (i = 0; error == 0 && i < glob_list.we_wordc; i++)
  424. error = do_parse_glob(glob_list.we_wordv[i], word, word_length,
  425. max_length, pwordexp, ifs, ifs_white);
  426. /* Now tidy up */
  427. tidy_up:
  428. __wordfree(&glob_list);
  429. return error;
  430. }
  431. static int
  432. parse_squote(char **word, size_t * word_length, size_t * max_length,
  433. const char *words, size_t * offset)
  434. {
  435. /* We are poised just after a single quote */
  436. for (; words[*offset]; ++(*offset)) {
  437. if (words[*offset] != '\'') {
  438. *word = w_addchar(*word, word_length, max_length, words[*offset]);
  439. if (*word == NULL)
  440. return WRDE_NOSPACE;
  441. } else
  442. return 0;
  443. }
  444. /* Unterminated string */
  445. return WRDE_SYNTAX;
  446. }
  447. #ifdef __WORDEXP_FULL
  448. static int eval_expr(char *expr, long int *result);
  449. static char *_itoa(unsigned long long int value, char *buflim)
  450. {
  451. sprintf(buflim, "%llu", value);
  452. return buflim;
  453. }
  454. /* Functions to evaluate an arithmetic expression */
  455. static int eval_expr_val(char **expr, long int *result)
  456. {
  457. int sgn = +1;
  458. char *digit;
  459. /* Skip white space */
  460. for (digit = *expr; digit && *digit && isspace(*digit); ++digit);
  461. switch (*digit) {
  462. case '(':
  463. /* Scan for closing paren */
  464. for (++digit; **expr && **expr != ')'; ++(*expr));
  465. /* Is there one? */
  466. if (!**expr)
  467. return WRDE_SYNTAX;
  468. *(*expr)++ = 0;
  469. if (eval_expr(digit, result))
  470. return WRDE_SYNTAX;
  471. return 0;
  472. case '+': /* Positive value */
  473. ++digit;
  474. break;
  475. case '-': /* Negative value */
  476. ++digit;
  477. sgn = -1;
  478. break;
  479. default:
  480. if (!isdigit(*digit))
  481. return WRDE_SYNTAX;
  482. }
  483. *result = 0;
  484. for (; *digit && isdigit(*digit); ++digit)
  485. *result = (*result * 10) + (*digit - '0');
  486. *expr = digit;
  487. *result *= sgn;
  488. return 0;
  489. }
  490. static int eval_expr_multdiv(char **expr, long int *result)
  491. {
  492. long int arg;
  493. /* Read a Value */
  494. if (eval_expr_val(expr, result) != 0)
  495. return WRDE_SYNTAX;
  496. while (**expr) {
  497. /* Skip white space */
  498. for (; *expr && **expr && isspace(**expr); ++(*expr));
  499. if (**expr == '*') {
  500. ++(*expr);
  501. if (eval_expr_val(expr, &arg) != 0)
  502. return WRDE_SYNTAX;
  503. *result *= arg;
  504. } else if (**expr == '/') {
  505. ++(*expr);
  506. if (eval_expr_val(expr, &arg) != 0)
  507. return WRDE_SYNTAX;
  508. *result /= arg;
  509. } else
  510. break;
  511. }
  512. return 0;
  513. }
  514. static int eval_expr(char *expr, long int *result)
  515. {
  516. long int arg;
  517. /* Read a Multdiv */
  518. if (eval_expr_multdiv(&expr, result) != 0)
  519. return WRDE_SYNTAX;
  520. while (*expr) {
  521. /* Skip white space */
  522. for (; expr && *expr && isspace(*expr); ++expr);
  523. if (*expr == '+') {
  524. ++expr;
  525. if (eval_expr_multdiv(&expr, &arg) != 0)
  526. return WRDE_SYNTAX;
  527. *result += arg;
  528. } else if (*expr == '-') {
  529. ++expr;
  530. if (eval_expr_multdiv(&expr, &arg) != 0)
  531. return WRDE_SYNTAX;
  532. *result -= arg;
  533. } else
  534. break;
  535. }
  536. return 0;
  537. }
  538. static int
  539. parse_arith(char **word, size_t * word_length, size_t * max_length,
  540. const char *words, size_t * offset, int flags, int bracket)
  541. {
  542. /* We are poised just after "$((" or "$[" */
  543. int error;
  544. int paren_depth = 1;
  545. size_t expr_length;
  546. size_t expr_maxlen;
  547. char *expr;
  548. expr = w_newword(&expr_length, &expr_maxlen);
  549. for (; words[*offset]; ++(*offset)) {
  550. switch (words[*offset]) {
  551. case '$':
  552. error = parse_dollars(&expr, &expr_length, &expr_maxlen,
  553. words, offset, flags, NULL, NULL, NULL,
  554. 1);
  555. /* The ``1'' here is to tell parse_dollars not to
  556. * split the fields.
  557. */
  558. if (error) {
  559. free(expr);
  560. return error;
  561. }
  562. break;
  563. case '`':
  564. (*offset)++;
  565. error = parse_backtick(&expr, &expr_length, &expr_maxlen,
  566. words, offset, flags, NULL, NULL, NULL);
  567. /* The first NULL here is to tell parse_backtick not to
  568. * split the fields.
  569. */
  570. if (error) {
  571. free(expr);
  572. return error;
  573. }
  574. break;
  575. case '\\':
  576. error = parse_qtd_backslash(&expr, &expr_length, &expr_maxlen,
  577. words, offset);
  578. if (error) {
  579. free(expr);
  580. return error;
  581. }
  582. /* I think that a backslash within an
  583. * arithmetic expansion is bound to
  584. * cause an error sooner or later anyway though.
  585. */
  586. break;
  587. case ')':
  588. if (--paren_depth == 0) {
  589. char result[21]; /* 21 = ceil(log10(2^64)) + 1 */
  590. long int numresult = 0;
  591. long long int convertme;
  592. if (bracket || words[1 + *offset] != ')') {
  593. free(expr);
  594. return WRDE_SYNTAX;
  595. }
  596. ++(*offset);
  597. /* Go - evaluate. */
  598. if (*expr && eval_expr(expr, &numresult) != 0) {
  599. free(expr);
  600. return WRDE_SYNTAX;
  601. }
  602. if (numresult < 0) {
  603. convertme = -numresult;
  604. *word = w_addchar(*word, word_length, max_length, '-');
  605. if (!*word) {
  606. free(expr);
  607. return WRDE_NOSPACE;
  608. }
  609. } else
  610. convertme = numresult;
  611. result[20] = '\0';
  612. *word = w_addstr(*word, word_length, max_length,
  613. _itoa(convertme, &result[20]));
  614. free(expr);
  615. return *word ? 0 : WRDE_NOSPACE;
  616. }
  617. expr =
  618. w_addchar(expr, &expr_length, &expr_maxlen,
  619. words[*offset]);
  620. if (expr == NULL)
  621. return WRDE_NOSPACE;
  622. break;
  623. case ']':
  624. if (bracket && paren_depth == 1) {
  625. char result[21]; /* 21 = ceil(log10(2^64)) + 1 */
  626. long int numresult = 0;
  627. /* Go - evaluate. */
  628. if (*expr && eval_expr(expr, &numresult) != 0) {
  629. free(expr);
  630. return WRDE_SYNTAX;
  631. }
  632. result[20] = '\0';
  633. *word = w_addstr(*word, word_length, max_length,
  634. _itoa(numresult, &result[20]));
  635. free(expr);
  636. return *word ? 0 : WRDE_NOSPACE;
  637. }
  638. free(expr);
  639. return WRDE_SYNTAX;
  640. case '\n':
  641. case ';':
  642. case '{':
  643. case '}':
  644. free(expr);
  645. return WRDE_BADCHAR;
  646. case '(':
  647. ++paren_depth;
  648. default:
  649. expr =
  650. w_addchar(expr, &expr_length, &expr_maxlen,
  651. words[*offset]);
  652. if (expr == NULL)
  653. return WRDE_NOSPACE;
  654. }
  655. }
  656. /* Premature end */
  657. free(expr);
  658. return WRDE_SYNTAX;
  659. }
  660. /* Function called by child process in exec_comm() */
  661. static void
  662. exec_comm_child(char *comm, int *fildes, int showerr, int noexec)
  663. {
  664. const char *args[4] = { _PATH_BSHELL, "-c", comm, NULL };
  665. /* Execute the command, or just check syntax? */
  666. if (noexec)
  667. args[1] = "-nc";
  668. /* Redirect output. */
  669. dup2(fildes[1], 1);
  670. close(fildes[1]);
  671. /* Redirect stderr to /dev/null if we have to. */
  672. if (showerr == 0) {
  673. int fd;
  674. close(2);
  675. fd = open(_PATH_DEVNULL, O_WRONLY);
  676. if (fd >= 0 && fd != 2) {
  677. dup2(fd, 2);
  678. close(fd);
  679. }
  680. }
  681. /* Make sure the subshell doesn't field-split on our behalf. */
  682. unsetenv("IFS");
  683. close(fildes[0]);
  684. execve(_PATH_BSHELL, (char *const *) args, __environ);
  685. /* Bad. What now? */
  686. abort();
  687. }
  688. /* Function to execute a command and retrieve the results */
  689. /* pwordexp contains NULL if field-splitting is forbidden */
  690. static int
  691. exec_comm(char *comm, char **word, size_t * word_length,
  692. size_t * max_length, int flags, wordexp_t * pwordexp,
  693. const char *ifs, const char *ifs_white)
  694. {
  695. int fildes[2];
  696. int bufsize = 128;
  697. int buflen;
  698. int i;
  699. int status = 0;
  700. size_t maxnewlines = 0;
  701. char *buffer;
  702. pid_t pid;
  703. /* Don't fork() unless necessary */
  704. if (!comm || !*comm)
  705. return 0;
  706. if (pipe(fildes))
  707. /* Bad */
  708. return WRDE_NOSPACE;
  709. if ((pid = fork()) < 0) {
  710. /* Bad */
  711. close(fildes[0]);
  712. close(fildes[1]);
  713. return WRDE_NOSPACE;
  714. }
  715. if (pid == 0)
  716. exec_comm_child(comm, fildes, (flags & WRDE_SHOWERR), 0);
  717. /* Parent */
  718. close(fildes[1]);
  719. buffer = alloca(bufsize);
  720. if (!pwordexp)
  721. /* Quoted - no field splitting */
  722. {
  723. while (1) {
  724. if ((buflen = read(fildes[0], buffer, bufsize)) < 1) {
  725. if (waitpid(pid, &status, WNOHANG) == 0)
  726. continue;
  727. if ((buflen = read(fildes[0], buffer, bufsize)) < 1)
  728. break;
  729. }
  730. maxnewlines += buflen;
  731. *word = w_addmem(*word, word_length, max_length, buffer, buflen);
  732. if (*word == NULL)
  733. goto no_space;
  734. }
  735. } else
  736. /* Not quoted - split fields */
  737. {
  738. int copying = 0;
  739. /* 'copying' is:
  740. * 0 when searching for first character in a field not IFS white space
  741. * 1 when copying the text of a field
  742. * 2 when searching for possible non-whitespace IFS
  743. * 3 when searching for non-newline after copying field
  744. */
  745. while (1) {
  746. if ((buflen = read(fildes[0], buffer, bufsize)) < 1) {
  747. if (waitpid(pid, &status, WNOHANG) == 0)
  748. continue;
  749. if ((buflen = read(fildes[0], buffer, bufsize)) < 1)
  750. break;
  751. }
  752. for (i = 0; i < buflen; ++i) {
  753. if (strchr(ifs, buffer[i]) != NULL) {
  754. /* Current character is IFS */
  755. if (strchr(ifs_white, buffer[i]) == NULL) {
  756. /* Current character is IFS but not whitespace */
  757. if (copying == 2) {
  758. /* current character
  759. * |
  760. * V
  761. * eg: text<space><comma><space>moretext
  762. *
  763. * So, strip whitespace IFS (like at the start)
  764. */
  765. copying = 0;
  766. continue;
  767. }
  768. copying = 0;
  769. /* fall through and delimit field.. */
  770. } else {
  771. if (buffer[i] == '\n') {
  772. /* Current character is (IFS) newline */
  773. /* If copying a field, this is the end of it,
  774. but maybe all that's left is trailing newlines.
  775. So start searching for a non-newline. */
  776. if (copying == 1)
  777. copying = 3;
  778. continue;
  779. } else {
  780. /* Current character is IFS white space, but
  781. not a newline */
  782. /* If not either copying a field or searching
  783. for non-newline after a field, ignore it */
  784. if (copying != 1 && copying != 3)
  785. continue;
  786. /* End of field (search for non-ws IFS afterwards) */
  787. copying = 2;
  788. }
  789. }
  790. /* First IFS white space (non-newline), or IFS non-whitespace.
  791. * Delimit the field. Nulls are converted by w_addword. */
  792. if (w_addword(pwordexp, *word) == WRDE_NOSPACE)
  793. goto no_space;
  794. *word = w_newword(word_length, max_length);
  795. maxnewlines = 0;
  796. /* fall back round the loop.. */
  797. } else {
  798. /* Not IFS character */
  799. if (copying == 3) {
  800. /* Nothing but (IFS) newlines since the last field,
  801. so delimit it here before starting new word */
  802. if (w_addword(pwordexp, *word) == WRDE_NOSPACE)
  803. goto no_space;
  804. *word = w_newword(word_length, max_length);
  805. }
  806. copying = 1;
  807. if (buffer[i] == '\n') /* happens if newline not in IFS */
  808. maxnewlines++;
  809. else
  810. maxnewlines = 0;
  811. *word = w_addchar(*word, word_length, max_length,
  812. buffer[i]);
  813. if (*word == NULL)
  814. goto no_space;
  815. }
  816. }
  817. }
  818. }
  819. /* Chop off trailing newlines (required by POSIX.2) */
  820. /* Ensure we don't go back further than the beginning of the
  821. substitution (i.e. remove maxnewlines bytes at most) */
  822. while (maxnewlines-- != 0 &&
  823. *word_length > 0 && (*word)[*word_length - 1] == '\n') {
  824. (*word)[--*word_length] = '\0';
  825. /* If the last word was entirely newlines, turn it into a new word
  826. * which can be ignored if there's nothing following it. */
  827. if (*word_length == 0) {
  828. free(*word);
  829. *word = w_newword(word_length, max_length);
  830. break;
  831. }
  832. }
  833. close(fildes[0]);
  834. /* Check for syntax error (re-execute but with "-n" flag) */
  835. if (buflen < 1 && status != 0) {
  836. if ((pid = fork()) < 0) {
  837. /* Bad */
  838. return WRDE_NOSPACE;
  839. }
  840. if (pid == 0) {
  841. fildes[0] = fildes[1] = -1;
  842. exec_comm_child(comm, fildes, 0, 1);
  843. }
  844. if (waitpid(pid, &status, 0) == pid && status != 0)
  845. return WRDE_SYNTAX;
  846. }
  847. return 0;
  848. no_space:
  849. kill(pid, SIGKILL);
  850. waitpid(pid, NULL, 0);
  851. close(fildes[0]);
  852. return WRDE_NOSPACE;
  853. }
  854. static int
  855. parse_comm(char **word, size_t * word_length, size_t * max_length,
  856. const char *words, size_t * offset, int flags,
  857. wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
  858. {
  859. /* We are poised just after "$(" */
  860. int paren_depth = 1;
  861. int error = 0;
  862. int quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
  863. size_t comm_length;
  864. size_t comm_maxlen;
  865. char *comm = w_newword(&comm_length, &comm_maxlen);
  866. for (; words[*offset]; ++(*offset)) {
  867. switch (words[*offset]) {
  868. case '\'':
  869. if (quoted == 0)
  870. quoted = 1;
  871. else if (quoted == 1)
  872. quoted = 0;
  873. break;
  874. case '"':
  875. if (quoted == 0)
  876. quoted = 2;
  877. else if (quoted == 2)
  878. quoted = 0;
  879. break;
  880. case ')':
  881. if (!quoted && --paren_depth == 0) {
  882. /* Go -- give script to the shell */
  883. if (comm) {
  884. error = exec_comm(comm, word, word_length, max_length,
  885. flags, pwordexp, ifs, ifs_white);
  886. free(comm);
  887. }
  888. return error;
  889. }
  890. /* This is just part of the script */
  891. break;
  892. case '(':
  893. if (!quoted)
  894. ++paren_depth;
  895. }
  896. comm = w_addchar(comm, &comm_length, &comm_maxlen, words[*offset]);
  897. if (comm == NULL)
  898. return WRDE_NOSPACE;
  899. }
  900. /* Premature end */
  901. if (comm)
  902. free(comm);
  903. return WRDE_SYNTAX;
  904. }
  905. static int
  906. parse_backtick(char **word, size_t * word_length, size_t * max_length,
  907. const char *words, size_t * offset, int flags,
  908. wordexp_t * pwordexp, const char *ifs,
  909. const char *ifs_white)
  910. {
  911. /* We are poised just after "`" */
  912. int error;
  913. int squoting = 0;
  914. size_t comm_length;
  915. size_t comm_maxlen;
  916. char *comm = w_newword(&comm_length, &comm_maxlen);
  917. for (; words[*offset]; ++(*offset)) {
  918. switch (words[*offset]) {
  919. case '`':
  920. /* Go -- give the script to the shell */
  921. error = exec_comm(comm, word, word_length, max_length, flags,
  922. pwordexp, ifs, ifs_white);
  923. free(comm);
  924. return error;
  925. case '\\':
  926. if (squoting) {
  927. error = parse_qtd_backslash(&comm, &comm_length, &comm_maxlen,
  928. words, offset);
  929. if (error) {
  930. free(comm);
  931. return error;
  932. }
  933. break;
  934. }
  935. ++(*offset);
  936. error = parse_backslash(&comm, &comm_length, &comm_maxlen, words,
  937. offset);
  938. if (error) {
  939. free(comm);
  940. return error;
  941. }
  942. break;
  943. case '\'':
  944. squoting = 1 - squoting;
  945. default:
  946. comm = w_addchar(comm, &comm_length, &comm_maxlen,
  947. words[*offset]);
  948. if (comm == NULL)
  949. return WRDE_NOSPACE;
  950. }
  951. }
  952. /* Premature end */
  953. free(comm);
  954. return WRDE_SYNTAX;
  955. }
  956. static int
  957. parse_param(char **word, size_t * word_length, size_t * max_length,
  958. const char *words, size_t * offset, int flags,
  959. wordexp_t * pwordexp, const char *ifs, const char *ifs_white,
  960. int quoted)
  961. {
  962. /* We are poised just after "$" */
  963. enum action {
  964. ACT_NONE,
  965. ACT_RP_SHORT_LEFT = '#',
  966. ACT_RP_LONG_LEFT = 'L',
  967. ACT_RP_SHORT_RIGHT = '%',
  968. ACT_RP_LONG_RIGHT = 'R',
  969. ACT_NULL_ERROR = '?',
  970. ACT_NULL_SUBST = '-',
  971. ACT_NONNULL_SUBST = '+',
  972. ACT_NULL_ASSIGN = '='
  973. };
  974. size_t env_length;
  975. size_t env_maxlen;
  976. size_t pat_length;
  977. size_t pat_maxlen;
  978. size_t start = *offset;
  979. char *env;
  980. char *pattern;
  981. char *value = NULL;
  982. enum action action = ACT_NONE;
  983. int depth = 0;
  984. int colon_seen = 0;
  985. int seen_hash = 0;
  986. int free_value = 0;
  987. int pattern_is_quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
  988. int error;
  989. int special = 0;
  990. char buffer[21];
  991. int brace = words[*offset] == '{';
  992. env = w_newword(&env_length, &env_maxlen);
  993. pattern = w_newword(&pat_length, &pat_maxlen);
  994. if (brace)
  995. ++ * offset;
  996. /* First collect the parameter name. */
  997. if (words[*offset] == '#') {
  998. seen_hash = 1;
  999. if (!brace)
  1000. goto envsubst;
  1001. ++*offset;
  1002. }
  1003. if (isalpha(words[*offset]) || words[*offset] == '_') {
  1004. /* Normal parameter name. */
  1005. do {
  1006. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  1007. if (env == NULL)
  1008. goto no_space;
  1009. }
  1010. while (isalnum(words[++*offset]) || words[*offset] == '_');
  1011. } else if (isdigit(words[*offset])) {
  1012. /* Numeric parameter name. */
  1013. special = 1;
  1014. do {
  1015. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  1016. if (env == NULL)
  1017. goto no_space;
  1018. if (!brace)
  1019. goto envsubst;
  1020. }
  1021. while (isdigit(words[++*offset]));
  1022. } else if (strchr("*@$", words[*offset]) != NULL) {
  1023. /* Special parameter. */
  1024. special = 1;
  1025. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  1026. if (env == NULL)
  1027. goto no_space;
  1028. ++*offset;
  1029. } else {
  1030. if (brace)
  1031. goto syntax;
  1032. }
  1033. if (brace) {
  1034. /* Check for special action to be applied to the value. */
  1035. switch (words[*offset]) {
  1036. case '}':
  1037. /* Evaluate. */
  1038. goto envsubst;
  1039. case '#':
  1040. action = ACT_RP_SHORT_LEFT;
  1041. if (words[1 + *offset] == '#') {
  1042. ++*offset;
  1043. action = ACT_RP_LONG_LEFT;
  1044. }
  1045. break;
  1046. case '%':
  1047. action = ACT_RP_SHORT_RIGHT;
  1048. if (words[1 + *offset] == '%') {
  1049. ++*offset;
  1050. action = ACT_RP_LONG_RIGHT;
  1051. }
  1052. break;
  1053. case ':':
  1054. if (strchr("-=?+", words[1 + *offset]) == NULL)
  1055. goto syntax;
  1056. colon_seen = 1;
  1057. action = words[++*offset];
  1058. break;
  1059. case '-':
  1060. case '=':
  1061. case '?':
  1062. case '+':
  1063. action = words[*offset];
  1064. break;
  1065. default:
  1066. goto syntax;
  1067. }
  1068. /* Now collect the pattern, but don't expand it yet. */
  1069. ++*offset;
  1070. for (; words[*offset]; ++(*offset)) {
  1071. switch (words[*offset]) {
  1072. case '{':
  1073. if (!pattern_is_quoted)
  1074. ++depth;
  1075. break;
  1076. case '}':
  1077. if (!pattern_is_quoted) {
  1078. if (depth == 0)
  1079. goto envsubst;
  1080. --depth;
  1081. }
  1082. break;
  1083. case '\\':
  1084. if (pattern_is_quoted)
  1085. /* Quoted; treat as normal character. */
  1086. break;
  1087. /* Otherwise, it's an escape: next character is literal. */
  1088. if (words[++*offset] == '\0')
  1089. goto syntax;
  1090. pattern = w_addchar(pattern, &pat_length, &pat_maxlen, '\\');
  1091. if (pattern == NULL)
  1092. goto no_space;
  1093. break;
  1094. case '\'':
  1095. if (pattern_is_quoted == 0)
  1096. pattern_is_quoted = 1;
  1097. else if (pattern_is_quoted == 1)
  1098. pattern_is_quoted = 0;
  1099. break;
  1100. case '"':
  1101. if (pattern_is_quoted == 0)
  1102. pattern_is_quoted = 2;
  1103. else if (pattern_is_quoted == 2)
  1104. pattern_is_quoted = 0;
  1105. break;
  1106. }
  1107. pattern = w_addchar(pattern, &pat_length, &pat_maxlen,
  1108. words[*offset]);
  1109. if (pattern == NULL)
  1110. goto no_space;
  1111. }
  1112. }
  1113. /* End of input string -- remember to reparse the character that we
  1114. * stopped at. */
  1115. --(*offset);
  1116. envsubst:
  1117. if (words[start] == '{' && words[*offset] != '}')
  1118. goto syntax;
  1119. if (env == NULL) {
  1120. if (seen_hash) {
  1121. /* $# expands to the number of positional parameters */
  1122. buffer[20] = '\0';
  1123. value = _itoa(__libc_argc - 1, &buffer[20]);
  1124. seen_hash = 0;
  1125. } else {
  1126. /* Just $ on its own */
  1127. *offset = start - 1;
  1128. *word = w_addchar(*word, word_length, max_length, '$');
  1129. return *word ? 0 : WRDE_NOSPACE;
  1130. }
  1131. }
  1132. /* Is it a numeric parameter? */
  1133. else if (isdigit(env[0])) {
  1134. int n = atoi(env);
  1135. if (n >= __libc_argc)
  1136. /* Substitute NULL. */
  1137. value = NULL;
  1138. else
  1139. /* Replace with appropriate positional parameter. */
  1140. value = __libc_argv[n];
  1141. }
  1142. /* Is it a special parameter? */
  1143. else if (special) {
  1144. /* Is it `$$'? */
  1145. if (*env == '$') {
  1146. buffer[20] = '\0';
  1147. value = _itoa(getpid(), &buffer[20]);
  1148. }
  1149. /* Is it `${#*}' or `${#@}'? */
  1150. else if ((*env == '*' || *env == '@') && seen_hash) {
  1151. buffer[20] = '\0';
  1152. value = _itoa(__libc_argc > 0 ? __libc_argc - 1 : 0,
  1153. &buffer[20]);
  1154. *word = w_addstr(*word, word_length, max_length, value);
  1155. free(env);
  1156. if (pattern)
  1157. free(pattern);
  1158. return *word ? 0 : WRDE_NOSPACE;
  1159. }
  1160. /* Is it `$*' or `$@' (unquoted) ? */
  1161. else if (*env == '*' || (*env == '@' && !quoted)) {
  1162. size_t plist_len = 0;
  1163. int p;
  1164. char *end;
  1165. /* Build up value parameter by parameter (copy them) */
  1166. for (p = 1; __libc_argv[p]; ++p)
  1167. plist_len += strlen(__libc_argv[p]) + 1; /* for space */
  1168. value = malloc(plist_len);
  1169. if (value == NULL)
  1170. goto no_space;
  1171. end = value;
  1172. *end = 0;
  1173. for (p = 1; __libc_argv[p]; ++p) {
  1174. if (p > 1)
  1175. *end++ = ' ';
  1176. end = stpcpy(end, __libc_argv[p]);
  1177. }
  1178. free_value = 1;
  1179. } else {
  1180. /* Must be a quoted `$@' */
  1181. assert(*env == '@' && quoted);
  1182. /* Each parameter is a separate word ("$@") */
  1183. if (__libc_argc == 2)
  1184. value = __libc_argv[1];
  1185. else if (__libc_argc > 2) {
  1186. int p;
  1187. /* Append first parameter to current word. */
  1188. value = w_addstr(*word, word_length, max_length,
  1189. __libc_argv[1]);
  1190. if (value == NULL || w_addword(pwordexp, value))
  1191. goto no_space;
  1192. for (p = 2; __libc_argv[p + 1]; p++) {
  1193. char *newword = strdup(__libc_argv[p]);
  1194. if (newword == NULL || w_addword(pwordexp, newword))
  1195. goto no_space;
  1196. }
  1197. /* Start a new word with the last parameter. */
  1198. *word = w_newword(word_length, max_length);
  1199. value = __libc_argv[p];
  1200. } else {
  1201. free(env);
  1202. free(pattern);
  1203. return 0;
  1204. }
  1205. }
  1206. } else
  1207. value = getenv(env);
  1208. if (value == NULL && (flags & WRDE_UNDEF)) {
  1209. /* Variable not defined. */
  1210. error = WRDE_BADVAL;
  1211. goto do_error;
  1212. }
  1213. if (action != ACT_NONE) {
  1214. int expand_pattern = 0;
  1215. /* First, find out if we need to expand pattern (i.e. if we will
  1216. * use it). */
  1217. switch (action) {
  1218. case ACT_RP_SHORT_LEFT:
  1219. case ACT_RP_LONG_LEFT:
  1220. case ACT_RP_SHORT_RIGHT:
  1221. case ACT_RP_LONG_RIGHT:
  1222. /* Always expand for these. */
  1223. expand_pattern = 1;
  1224. break;
  1225. case ACT_NULL_ERROR:
  1226. case ACT_NULL_SUBST:
  1227. case ACT_NULL_ASSIGN:
  1228. if (!value || (!*value && colon_seen))
  1229. /* If param is unset, or set but null and a colon has been seen,
  1230. the expansion of the pattern will be needed. */
  1231. expand_pattern = 1;
  1232. break;
  1233. case ACT_NONNULL_SUBST:
  1234. /* Expansion of word will be needed if parameter is set and not null,
  1235. or set null but no colon has been seen. */
  1236. if (value && (*value || !colon_seen))
  1237. expand_pattern = 1;
  1238. break;
  1239. default:
  1240. assert(!"Unrecognised action!");
  1241. }
  1242. if (expand_pattern) {
  1243. /* We need to perform tilde expansion, parameter expansion,
  1244. command substitution, and arithmetic expansion. We also
  1245. have to be a bit careful with wildcard characters, as
  1246. pattern might be given to fnmatch soon. To do this, we
  1247. convert quotes to escapes. */
  1248. char *expanded;
  1249. size_t exp_len;
  1250. size_t exp_maxl;
  1251. char *p;
  1252. int quoted = 0; /* 1: single quotes; 2: double */
  1253. expanded = w_newword(&exp_len, &exp_maxl);
  1254. for (p = pattern; p && *p; p++) {
  1255. size_t offset;
  1256. switch (*p) {
  1257. case '"':
  1258. if (quoted == 2)
  1259. quoted = 0;
  1260. else if (quoted == 0)
  1261. quoted = 2;
  1262. else
  1263. break;
  1264. continue;
  1265. case '\'':
  1266. if (quoted == 1)
  1267. quoted = 0;
  1268. else if (quoted == 0)
  1269. quoted = 1;
  1270. else
  1271. break;
  1272. continue;
  1273. case '*':
  1274. case '?':
  1275. if (quoted) {
  1276. /* Convert quoted wildchar to escaped wildchar. */
  1277. expanded = w_addchar(expanded, &exp_len,
  1278. &exp_maxl, '\\');
  1279. if (expanded == NULL)
  1280. goto no_space;
  1281. }
  1282. break;
  1283. case '$':
  1284. offset = 0;
  1285. error = parse_dollars(&expanded, &exp_len, &exp_maxl, p,
  1286. &offset, flags, NULL, NULL, NULL, 1);
  1287. if (error) {
  1288. if (free_value)
  1289. free(value);
  1290. if (expanded)
  1291. free(expanded);
  1292. goto do_error;
  1293. }
  1294. p += offset;
  1295. continue;
  1296. case '~':
  1297. if (quoted || exp_len)
  1298. break;
  1299. offset = 0;
  1300. error = parse_tilde(&expanded, &exp_len, &exp_maxl, p,
  1301. &offset, 0);
  1302. if (error) {
  1303. if (free_value)
  1304. free(value);
  1305. if (expanded)
  1306. free(expanded);
  1307. goto do_error;
  1308. }
  1309. p += offset;
  1310. continue;
  1311. case '\\':
  1312. expanded = w_addchar(expanded, &exp_len, &exp_maxl, '\\');
  1313. ++p;
  1314. assert(*p); /* checked when extracted initially */
  1315. if (expanded == NULL)
  1316. goto no_space;
  1317. }
  1318. expanded = w_addchar(expanded, &exp_len, &exp_maxl, *p);
  1319. if (expanded == NULL)
  1320. goto no_space;
  1321. }
  1322. if (pattern)
  1323. free(pattern);
  1324. pattern = expanded;
  1325. }
  1326. switch (action) {
  1327. case ACT_RP_SHORT_LEFT:
  1328. case ACT_RP_LONG_LEFT:
  1329. case ACT_RP_SHORT_RIGHT:
  1330. case ACT_RP_LONG_RIGHT:
  1331. {
  1332. char *p;
  1333. char c;
  1334. char *end;
  1335. if (value == NULL || pattern == NULL || *pattern == '\0')
  1336. break;
  1337. end = value + strlen(value);
  1338. switch (action) {
  1339. case ACT_RP_SHORT_LEFT:
  1340. for (p = value; p <= end; ++p) {
  1341. c = *p;
  1342. *p = '\0';
  1343. if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
  1344. *p = c;
  1345. if (free_value) {
  1346. char *newval = strdup(p);
  1347. if (newval == NULL) {
  1348. free(value);
  1349. goto no_space;
  1350. }
  1351. free(value);
  1352. value = newval;
  1353. } else
  1354. value = p;
  1355. break;
  1356. }
  1357. *p = c;
  1358. }
  1359. break;
  1360. case ACT_RP_LONG_LEFT:
  1361. for (p = end; p >= value; --p) {
  1362. c = *p;
  1363. *p = '\0';
  1364. if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
  1365. *p = c;
  1366. if (free_value) {
  1367. char *newval = strdup(p);
  1368. if (newval == NULL) {
  1369. free(value);
  1370. goto no_space;
  1371. }
  1372. free(value);
  1373. value = newval;
  1374. } else
  1375. value = p;
  1376. break;
  1377. }
  1378. *p = c;
  1379. }
  1380. break;
  1381. case ACT_RP_SHORT_RIGHT:
  1382. for (p = end; p >= value; --p) {
  1383. if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {
  1384. char *newval;
  1385. newval = malloc(p - value + 1);
  1386. if (newval == NULL) {
  1387. if (free_value)
  1388. free(value);
  1389. goto no_space;
  1390. }
  1391. *(char *) mempcpy(newval, value, p - value) = '\0';
  1392. if (free_value)
  1393. free(value);
  1394. value = newval;
  1395. free_value = 1;
  1396. break;
  1397. }
  1398. }
  1399. break;
  1400. case ACT_RP_LONG_RIGHT:
  1401. for (p = value; p <= end; ++p) {
  1402. if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {
  1403. char *newval;
  1404. newval = malloc(p - value + 1);
  1405. if (newval == NULL) {
  1406. if (free_value)
  1407. free(value);
  1408. goto no_space;
  1409. }
  1410. *(char *) mempcpy(newval, value, p - value) = '\0';
  1411. if (free_value)
  1412. free(value);
  1413. value = newval;
  1414. free_value = 1;
  1415. break;
  1416. }
  1417. }
  1418. break;
  1419. default:
  1420. break;
  1421. }
  1422. break;
  1423. }
  1424. case ACT_NULL_ERROR:
  1425. if (value && *value)
  1426. /* Substitute parameter */
  1427. break;
  1428. error = 0;
  1429. if (!colon_seen && value)
  1430. /* Substitute NULL */
  1431. ;
  1432. else if (*pattern)
  1433. fprintf(stderr, "%s: %s\n", env, pattern);
  1434. else {
  1435. fprintf(stderr, "%s: parameter null or not set\n", env);
  1436. error = WRDE_BADVAL;
  1437. }
  1438. if (free_value)
  1439. free(value);
  1440. goto do_error;
  1441. case ACT_NULL_SUBST:
  1442. if (value && *value)
  1443. /* Substitute parameter */
  1444. break;
  1445. if (free_value && value)
  1446. free(value);
  1447. if (!colon_seen && value)
  1448. /* Substitute NULL */
  1449. goto success;
  1450. value = pattern ? strdup(pattern) : pattern;
  1451. free_value = 1;
  1452. if (pattern && !value)
  1453. goto no_space;
  1454. break;
  1455. case ACT_NONNULL_SUBST:
  1456. if (value && (*value || !colon_seen)) {
  1457. if (free_value && value)
  1458. free(value);
  1459. value = pattern ? strdup(pattern) : pattern;
  1460. free_value = 1;
  1461. if (pattern && !value)
  1462. goto no_space;
  1463. break;
  1464. }
  1465. /* Substitute NULL */
  1466. if (free_value)
  1467. free(value);
  1468. goto success;
  1469. case ACT_NULL_ASSIGN:
  1470. if (value && *value)
  1471. /* Substitute parameter */
  1472. break;
  1473. if (!colon_seen && value) {
  1474. /* Substitute NULL */
  1475. if (free_value)
  1476. free(value);
  1477. goto success;
  1478. }
  1479. if (free_value && value)
  1480. free(value);
  1481. value = pattern ? strdup(pattern) : pattern;
  1482. free_value = 1;
  1483. if (pattern && !value)
  1484. goto no_space;
  1485. setenv(env, value, 1);
  1486. break;
  1487. default:
  1488. assert(!"Unrecognised action!");
  1489. }
  1490. }
  1491. free(env);
  1492. env = NULL;
  1493. free(pattern);
  1494. pattern = NULL;
  1495. if (seen_hash) {
  1496. char param_length[21];
  1497. param_length[20] = '\0';
  1498. *word = w_addstr(*word, word_length, max_length,
  1499. _itoa(value ? strlen(value) : 0,
  1500. &param_length[20]));
  1501. if (free_value) {
  1502. assert(value != NULL);
  1503. free(value);
  1504. }
  1505. return *word ? 0 : WRDE_NOSPACE;
  1506. }
  1507. if (value == NULL)
  1508. return 0;
  1509. if (quoted || !pwordexp) {
  1510. /* Quoted - no field split */
  1511. *word = w_addstr(*word, word_length, max_length, value);
  1512. if (free_value)
  1513. free(value);
  1514. return *word ? 0 : WRDE_NOSPACE;
  1515. } else {
  1516. /* Need to field-split */
  1517. char *value_copy = strdup(value); /* Don't modify value */
  1518. char *field_begin = value_copy;
  1519. int seen_nonws_ifs = 0;
  1520. if (free_value)
  1521. free(value);
  1522. if (value_copy == NULL)
  1523. goto no_space;
  1524. do {
  1525. char *field_end = field_begin;
  1526. char *next_field;
  1527. /* If this isn't the first field, start a new word */
  1528. if (field_begin != value_copy) {
  1529. if (w_addword(pwordexp, *word) == WRDE_NOSPACE) {
  1530. free(value_copy);
  1531. goto no_space;
  1532. }
  1533. *word = w_newword(word_length, max_length);
  1534. }
  1535. /* Skip IFS whitespace before the field */
  1536. field_begin += strspn(field_begin, ifs_white);
  1537. if (!seen_nonws_ifs && *field_begin == 0)
  1538. /* Nothing but whitespace */
  1539. break;
  1540. /* Search for the end of the field */
  1541. field_end = field_begin + strcspn(field_begin, ifs);
  1542. /* Set up pointer to the character after end of field and
  1543. skip whitespace IFS after it. */
  1544. next_field = field_end + strspn(field_end, ifs_white);
  1545. /* Skip at most one non-whitespace IFS character after the field */
  1546. seen_nonws_ifs = 0;
  1547. if (*next_field && strchr(ifs, *next_field)) {
  1548. seen_nonws_ifs = 1;
  1549. next_field++;
  1550. }
  1551. /* Null-terminate it */
  1552. *field_end = 0;
  1553. /* Tag a copy onto the current word */
  1554. *word = w_addstr(*word, word_length, max_length, field_begin);
  1555. if (*word == NULL && *field_begin != '\0') {
  1556. free(value_copy);
  1557. goto no_space;
  1558. }
  1559. field_begin = next_field;
  1560. }
  1561. while (seen_nonws_ifs || *field_begin);
  1562. free(value_copy);
  1563. }
  1564. return 0;
  1565. success:
  1566. error = 0;
  1567. goto do_error;
  1568. no_space:
  1569. error = WRDE_NOSPACE;
  1570. goto do_error;
  1571. syntax:
  1572. error = WRDE_SYNTAX;
  1573. do_error:
  1574. if (env)
  1575. free(env);
  1576. if (pattern)
  1577. free(pattern);
  1578. return error;
  1579. }
  1580. #else
  1581. static inline int
  1582. parse_backtick(char **word, size_t * word_length, size_t * max_length,
  1583. const char *words, size_t * offset, int flags,
  1584. wordexp_t * pwordexp, const char *ifs,
  1585. const char *ifs_white)
  1586. {
  1587. return 0;
  1588. }
  1589. #endif
  1590. static int
  1591. parse_dollars(char **word, size_t * word_length, size_t * max_length,
  1592. const char *words, size_t * offset, int flags,
  1593. wordexp_t * pwordexp, const char *ifs, const char *ifs_white,
  1594. int quoted)
  1595. {
  1596. /* We are poised _at_ "$" */
  1597. switch (words[1 + *offset]) {
  1598. case '"':
  1599. case '\'':
  1600. case 0:
  1601. *word = w_addchar(*word, word_length, max_length, '$');
  1602. return *word ? 0 : WRDE_NOSPACE;
  1603. #ifdef __WORDEXP_FULL
  1604. case '(':
  1605. if (words[2 + *offset] == '(') {
  1606. /* Differentiate between $((1+3)) and $((echo);(ls)) */
  1607. int i = 3 + *offset;
  1608. int depth = 0;
  1609. while (words[i] && !(depth == 0 && words[i] == ')')) {
  1610. if (words[i] == '(')
  1611. ++depth;
  1612. else if (words[i] == ')')
  1613. --depth;
  1614. ++i;
  1615. }
  1616. if (words[i] == ')' && words[i + 1] == ')') {
  1617. (*offset) += 3;
  1618. /* Call parse_arith -- 0 is for "no brackets" */
  1619. return parse_arith(word, word_length, max_length, words,
  1620. offset, flags, 0);
  1621. }
  1622. }
  1623. if (flags & WRDE_NOCMD)
  1624. return WRDE_CMDSUB;
  1625. (*offset) += 2;
  1626. return parse_comm(word, word_length, max_length, words, offset,
  1627. flags, quoted ? NULL : pwordexp, ifs, ifs_white);
  1628. case '[':
  1629. (*offset) += 2;
  1630. /* Call parse_arith -- 1 is for "brackets" */
  1631. return parse_arith(word, word_length, max_length, words, offset,
  1632. flags, 1);
  1633. case '{':
  1634. default:
  1635. ++(*offset); /* parse_param needs to know if "{" is there */
  1636. return parse_param(word, word_length, max_length, words, offset,
  1637. flags, pwordexp, ifs, ifs_white, quoted);
  1638. #else
  1639. default:
  1640. ++(*offset); /* parse_param needs to know if "{" is there */
  1641. return 0;
  1642. #endif
  1643. }
  1644. }
  1645. static int
  1646. parse_dquote(char **word, size_t * word_length, size_t * max_length,
  1647. const char *words, size_t * offset, int flags,
  1648. wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
  1649. {
  1650. /* We are poised just after a double-quote */
  1651. int error;
  1652. for (; words[*offset]; ++(*offset)) {
  1653. switch (words[*offset]) {
  1654. case '"':
  1655. return 0;
  1656. case '$':
  1657. error = parse_dollars(word, word_length, max_length, words, offset,
  1658. flags, pwordexp, ifs, ifs_white, 1);
  1659. /* The ``1'' here is to tell parse_dollars not to
  1660. * split the fields. It may need to, however ("$@").
  1661. */
  1662. if (error)
  1663. return error;
  1664. break;
  1665. case '`':
  1666. if (flags & WRDE_NOCMD)
  1667. return WRDE_CMDSUB;
  1668. ++(*offset);
  1669. error = parse_backtick(word, word_length, max_length, words,
  1670. offset, flags, NULL, NULL, NULL);
  1671. /* The first NULL here is to tell parse_backtick not to
  1672. * split the fields.
  1673. */
  1674. if (error)
  1675. return error;
  1676. break;
  1677. case '\\':
  1678. error = parse_qtd_backslash(word, word_length, max_length, words,
  1679. offset);
  1680. if (error)
  1681. return error;
  1682. break;
  1683. default:
  1684. *word = w_addchar(*word, word_length, max_length, words[*offset]);
  1685. if (*word == NULL)
  1686. return WRDE_NOSPACE;
  1687. }
  1688. }
  1689. /* Unterminated string */
  1690. return WRDE_SYNTAX;
  1691. }
  1692. /*
  1693. * wordfree() is to be called after pwordexp is finished with.
  1694. */
  1695. void attribute_hidden __wordfree(wordexp_t * pwordexp)
  1696. {
  1697. /* wordexp can set pwordexp to NULL */
  1698. if (pwordexp && pwordexp->we_wordv) {
  1699. char **wordv = pwordexp->we_wordv;
  1700. for (wordv += pwordexp->we_offs; *wordv; ++wordv)
  1701. free(*wordv);
  1702. free(pwordexp->we_wordv);
  1703. pwordexp->we_wordv = NULL;
  1704. }
  1705. }
  1706. strong_alias(__wordfree,wordfree)
  1707. /*
  1708. * wordexp()
  1709. */
  1710. int wordexp(const char *words, wordexp_t * we, int flags)
  1711. {
  1712. size_t words_offset;
  1713. size_t word_length;
  1714. size_t max_length;
  1715. char *word = w_newword(&word_length, &max_length);
  1716. int error;
  1717. char *ifs;
  1718. char ifs_white[4];
  1719. wordexp_t old_word = *we;
  1720. if (flags & WRDE_REUSE) {
  1721. /* Minimal implementation of WRDE_REUSE for now */
  1722. __wordfree(we);
  1723. old_word.we_wordv = NULL;
  1724. }
  1725. if ((flags & WRDE_APPEND) == 0) {
  1726. we->we_wordc = 0;
  1727. if (flags & WRDE_DOOFFS) {
  1728. we->we_wordv = calloc(1 + we->we_offs, sizeof(char *));
  1729. if (we->we_wordv == NULL) {
  1730. error = WRDE_NOSPACE;
  1731. goto do_error;
  1732. }
  1733. } else {
  1734. we->we_wordv = calloc(1, sizeof(char *));
  1735. if (we->we_wordv == NULL) {
  1736. error = WRDE_NOSPACE;
  1737. goto do_error;
  1738. }
  1739. we->we_offs = 0;
  1740. }
  1741. }
  1742. /* Find out what the field separators are.
  1743. * There are two types: whitespace and non-whitespace.
  1744. */
  1745. ifs = getenv("IFS");
  1746. if (!ifs)
  1747. /* IFS unset - use <space><tab><newline>. */
  1748. ifs = strcpy(ifs_white, " \t\n");
  1749. else {
  1750. char *ifsch = ifs;
  1751. char *whch = ifs_white;
  1752. /* Start off with no whitespace IFS characters */
  1753. ifs_white[0] = '\0';
  1754. while (*ifsch != '\0') {
  1755. if ((*ifsch == ' ') || (*ifsch == '\t') || (*ifsch == '\n')) {
  1756. /* Whitespace IFS. See first whether it is already in our
  1757. collection. */
  1758. char *runp = ifs_white;
  1759. while (runp < whch && *runp != '\0' && *runp != *ifsch)
  1760. ++runp;
  1761. if (runp == whch)
  1762. *whch++ = *ifsch;
  1763. }
  1764. ++ifsch;
  1765. }
  1766. *whch = '\0';
  1767. }
  1768. for (words_offset = 0; words[words_offset]; ++words_offset)
  1769. switch (words[words_offset]) {
  1770. case '\\':
  1771. error = parse_backslash(&word, &word_length, &max_length, words,
  1772. &words_offset);
  1773. if (error)
  1774. goto do_error;
  1775. break;
  1776. case '$':
  1777. error = parse_dollars(&word, &word_length, &max_length, words,
  1778. &words_offset, flags, we, ifs, ifs_white,
  1779. 0);
  1780. if (error)
  1781. goto do_error;
  1782. break;
  1783. case '`':
  1784. if (flags & WRDE_NOCMD) {
  1785. error = WRDE_CMDSUB;
  1786. goto do_error;
  1787. }
  1788. ++words_offset;
  1789. error = parse_backtick(&word, &word_length, &max_length, words,
  1790. &words_offset, flags, we, ifs,
  1791. ifs_white);
  1792. if (error)
  1793. goto do_error;
  1794. break;
  1795. case '"':
  1796. ++words_offset;
  1797. error = parse_dquote(&word, &word_length, &max_length, words,
  1798. &words_offset, flags, we, ifs, ifs_white);
  1799. if (error)
  1800. goto do_error;
  1801. if (!word_length) {
  1802. error = w_addword(we, NULL);
  1803. if (error)
  1804. return error;
  1805. }
  1806. break;
  1807. case '\'':
  1808. ++words_offset;
  1809. error = parse_squote(&word, &word_length, &max_length, words,
  1810. &words_offset);
  1811. if (error)
  1812. goto do_error;
  1813. if (!word_length) {
  1814. error = w_addword(we, NULL);
  1815. if (error)
  1816. return error;
  1817. }
  1818. break;
  1819. case '~':
  1820. error = parse_tilde(&word, &word_length, &max_length, words,
  1821. &words_offset, we->we_wordc);
  1822. if (error)
  1823. goto do_error;
  1824. break;
  1825. case '*':
  1826. case '[':
  1827. case '?':
  1828. error = parse_glob(&word, &word_length, &max_length, words,
  1829. &words_offset, flags, we, ifs, ifs_white);
  1830. if (error)
  1831. goto do_error;
  1832. break;
  1833. default:
  1834. /* Is it a word separator? */
  1835. if (strchr(" \t", words[words_offset]) == NULL) {
  1836. char ch = words[words_offset];
  1837. /* Not a word separator -- but is it a valid word char? */
  1838. if (strchr("\n|&;<>(){}", ch)) {
  1839. /* Fail */
  1840. error = WRDE_BADCHAR;
  1841. goto do_error;
  1842. }
  1843. /* "Ordinary" character -- add it to word */
  1844. word = w_addchar(word, &word_length, &max_length, ch);
  1845. if (word == NULL) {
  1846. error = WRDE_NOSPACE;
  1847. goto do_error;
  1848. }
  1849. break;
  1850. }
  1851. /* If a word has been delimited, add it to the list. */
  1852. if (word != NULL) {
  1853. error = w_addword(we, word);
  1854. if (error)
  1855. goto do_error;
  1856. }
  1857. word = w_newword(&word_length, &max_length);
  1858. }
  1859. /* End of string */
  1860. /* There was a word separator at the end */
  1861. if (word == NULL) /* i.e. w_newword */
  1862. return 0;
  1863. /* There was no field separator at the end */
  1864. return w_addword(we, word);
  1865. do_error:
  1866. /* Error:
  1867. * free memory used (unless error is WRDE_NOSPACE), and
  1868. * set we members back to what they were.
  1869. */
  1870. if (word != NULL)
  1871. free(word);
  1872. if (error == WRDE_NOSPACE)
  1873. return WRDE_NOSPACE;
  1874. if ((flags & WRDE_APPEND) == 0)
  1875. __wordfree(we);
  1876. *we = old_word;
  1877. return error;
  1878. }