wordexp.c 49 KB

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