wordexp.c 49 KB

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