wordexp.c 49 KB

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