wordexp.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275
  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 defined __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 attribute_noreturn
  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. free(comm);
  904. return WRDE_SYNTAX;
  905. }
  906. static int
  907. parse_backtick(char **word, size_t * word_length, size_t * max_length,
  908. const char *words, size_t * offset, int flags,
  909. wordexp_t * pwordexp, const char *ifs,
  910. const char *ifs_white)
  911. {
  912. /* We are poised just after "`" */
  913. int error;
  914. int squoting = 0;
  915. size_t comm_length;
  916. size_t comm_maxlen;
  917. char *comm = w_newword(&comm_length, &comm_maxlen);
  918. for (; words[*offset]; ++(*offset)) {
  919. switch (words[*offset]) {
  920. case '`':
  921. /* Go -- give the script to the shell */
  922. error = exec_comm(comm, word, word_length, max_length, flags,
  923. pwordexp, ifs, ifs_white);
  924. free(comm);
  925. return error;
  926. case '\\':
  927. if (squoting) {
  928. error = parse_qtd_backslash(&comm, &comm_length, &comm_maxlen,
  929. words, offset);
  930. if (error) {
  931. free(comm);
  932. return error;
  933. }
  934. break;
  935. }
  936. ++(*offset);
  937. error = parse_backslash(&comm, &comm_length, &comm_maxlen, words,
  938. offset);
  939. if (error) {
  940. free(comm);
  941. return error;
  942. }
  943. break;
  944. case '\'':
  945. squoting = 1 - squoting;
  946. default:
  947. comm = w_addchar(comm, &comm_length, &comm_maxlen,
  948. words[*offset]);
  949. if (comm == NULL)
  950. return WRDE_NOSPACE;
  951. }
  952. }
  953. /* Premature end */
  954. free(comm);
  955. return WRDE_SYNTAX;
  956. }
  957. static int
  958. parse_param(char **word, size_t * word_length, size_t * max_length,
  959. const char *words, size_t * offset, int flags,
  960. wordexp_t * pwordexp, const char *ifs, const char *ifs_white,
  961. int quoted)
  962. {
  963. /* We are poised just after "$" */
  964. enum action {
  965. ACT_NONE,
  966. ACT_RP_SHORT_LEFT = '#',
  967. ACT_RP_LONG_LEFT = 'L',
  968. ACT_RP_SHORT_RIGHT = '%',
  969. ACT_RP_LONG_RIGHT = 'R',
  970. ACT_NULL_ERROR = '?',
  971. ACT_NULL_SUBST = '-',
  972. ACT_NONNULL_SUBST = '+',
  973. ACT_NULL_ASSIGN = '='
  974. };
  975. size_t env_length;
  976. size_t env_maxlen;
  977. size_t pat_length;
  978. size_t pat_maxlen;
  979. size_t start = *offset;
  980. char *env;
  981. char *pattern;
  982. char *value = NULL;
  983. enum action action = ACT_NONE;
  984. int depth = 0;
  985. int colon_seen = 0;
  986. int seen_hash = 0;
  987. int free_value = 0;
  988. int pattern_is_quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
  989. int error;
  990. int special = 0;
  991. char buffer[21];
  992. int brace = words[*offset] == '{';
  993. env = w_newword(&env_length, &env_maxlen);
  994. pattern = w_newword(&pat_length, &pat_maxlen);
  995. if (brace)
  996. ++ * offset;
  997. /* First collect the parameter name. */
  998. if (words[*offset] == '#') {
  999. seen_hash = 1;
  1000. if (!brace)
  1001. goto envsubst;
  1002. ++*offset;
  1003. }
  1004. if (isalpha(words[*offset]) || words[*offset] == '_') {
  1005. /* Normal parameter name. */
  1006. do {
  1007. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  1008. if (env == NULL)
  1009. goto no_space;
  1010. }
  1011. while (isalnum(words[++*offset]) || words[*offset] == '_');
  1012. } else if (isdigit(words[*offset])) {
  1013. /* Numeric parameter name. */
  1014. special = 1;
  1015. do {
  1016. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  1017. if (env == NULL)
  1018. goto no_space;
  1019. if (!brace)
  1020. goto envsubst;
  1021. }
  1022. while (isdigit(words[++*offset]));
  1023. } else if (strchr("*@$", words[*offset]) != NULL) {
  1024. /* Special parameter. */
  1025. special = 1;
  1026. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  1027. if (env == NULL)
  1028. goto no_space;
  1029. ++*offset;
  1030. } else {
  1031. if (brace)
  1032. goto syntax;
  1033. }
  1034. if (brace) {
  1035. /* Check for special action to be applied to the value. */
  1036. switch (words[*offset]) {
  1037. case '}':
  1038. /* Evaluate. */
  1039. goto envsubst;
  1040. case '#':
  1041. action = ACT_RP_SHORT_LEFT;
  1042. if (words[1 + *offset] == '#') {
  1043. ++*offset;
  1044. action = ACT_RP_LONG_LEFT;
  1045. }
  1046. break;
  1047. case '%':
  1048. action = ACT_RP_SHORT_RIGHT;
  1049. if (words[1 + *offset] == '%') {
  1050. ++*offset;
  1051. action = ACT_RP_LONG_RIGHT;
  1052. }
  1053. break;
  1054. case ':':
  1055. if (strchr("-=?+", words[1 + *offset]) == NULL)
  1056. goto syntax;
  1057. colon_seen = 1;
  1058. action = words[++*offset];
  1059. break;
  1060. case '-':
  1061. case '=':
  1062. case '?':
  1063. case '+':
  1064. action = words[*offset];
  1065. break;
  1066. default:
  1067. goto syntax;
  1068. }
  1069. /* Now collect the pattern, but don't expand it yet. */
  1070. ++*offset;
  1071. for (; words[*offset]; ++(*offset)) {
  1072. switch (words[*offset]) {
  1073. case '{':
  1074. if (!pattern_is_quoted)
  1075. ++depth;
  1076. break;
  1077. case '}':
  1078. if (!pattern_is_quoted) {
  1079. if (depth == 0)
  1080. goto envsubst;
  1081. --depth;
  1082. }
  1083. break;
  1084. case '\\':
  1085. if (pattern_is_quoted)
  1086. /* Quoted; treat as normal character. */
  1087. break;
  1088. /* Otherwise, it's an escape: next character is literal. */
  1089. if (words[++*offset] == '\0')
  1090. goto syntax;
  1091. pattern = w_addchar(pattern, &pat_length, &pat_maxlen, '\\');
  1092. if (pattern == NULL)
  1093. goto no_space;
  1094. break;
  1095. case '\'':
  1096. if (pattern_is_quoted == 0)
  1097. pattern_is_quoted = 1;
  1098. else if (pattern_is_quoted == 1)
  1099. pattern_is_quoted = 0;
  1100. break;
  1101. case '"':
  1102. if (pattern_is_quoted == 0)
  1103. pattern_is_quoted = 2;
  1104. else if (pattern_is_quoted == 2)
  1105. pattern_is_quoted = 0;
  1106. break;
  1107. }
  1108. pattern = w_addchar(pattern, &pat_length, &pat_maxlen,
  1109. words[*offset]);
  1110. if (pattern == NULL)
  1111. goto no_space;
  1112. }
  1113. }
  1114. /* End of input string -- remember to reparse the character that we
  1115. * stopped at. */
  1116. --(*offset);
  1117. envsubst:
  1118. if (words[start] == '{' && words[*offset] != '}')
  1119. goto syntax;
  1120. if (env == NULL) {
  1121. if (seen_hash) {
  1122. /* $# expands to the number of positional parameters */
  1123. buffer[20] = '\0';
  1124. value = _itoa(__libc_argc - 1, &buffer[20]);
  1125. seen_hash = 0;
  1126. } else {
  1127. /* Just $ on its own */
  1128. *offset = start - 1;
  1129. *word = w_addchar(*word, word_length, max_length, '$');
  1130. return *word ? 0 : WRDE_NOSPACE;
  1131. }
  1132. }
  1133. /* Is it a numeric parameter? */
  1134. else if (isdigit(env[0])) {
  1135. int n = atoi(env);
  1136. if (n >= __libc_argc)
  1137. /* Substitute NULL. */
  1138. value = NULL;
  1139. else
  1140. /* Replace with appropriate positional parameter. */
  1141. value = __libc_argv[n];
  1142. }
  1143. /* Is it a special parameter? */
  1144. else if (special) {
  1145. /* Is it `$$'? */
  1146. if (*env == '$') {
  1147. buffer[20] = '\0';
  1148. value = _itoa(getpid(), &buffer[20]);
  1149. }
  1150. /* Is it `${#*}' or `${#@}'? */
  1151. else if ((*env == '*' || *env == '@') && seen_hash) {
  1152. buffer[20] = '\0';
  1153. value = _itoa(__libc_argc > 0 ? __libc_argc - 1 : 0,
  1154. &buffer[20]);
  1155. *word = w_addstr(*word, word_length, max_length, value);
  1156. free(env);
  1157. free(pattern);
  1158. return *word ? 0 : WRDE_NOSPACE;
  1159. }
  1160. /* Is it `$*' or `$@' (unquoted) ? */
  1161. else if (*env == '*' || (*env == '@' && !quoted)) {
  1162. size_t plist_len = 0;
  1163. int p;
  1164. char *end;
  1165. /* Build up value parameter by parameter (copy them) */
  1166. for (p = 1; __libc_argv[p]; ++p)
  1167. plist_len += strlen(__libc_argv[p]) + 1; /* for space */
  1168. value = malloc(plist_len);
  1169. if (value == NULL)
  1170. goto no_space;
  1171. end = value;
  1172. *end = 0;
  1173. for (p = 1; __libc_argv[p]; ++p) {
  1174. if (p > 1)
  1175. *end++ = ' ';
  1176. end = stpcpy(end, __libc_argv[p]);
  1177. }
  1178. free_value = 1;
  1179. } else {
  1180. /* Must be a quoted `$@' */
  1181. assert(*env == '@' && quoted);
  1182. /* Each parameter is a separate word ("$@") */
  1183. if (__libc_argc == 2)
  1184. value = __libc_argv[1];
  1185. else if (__libc_argc > 2) {
  1186. int p;
  1187. /* Append first parameter to current word. */
  1188. value = w_addstr(*word, word_length, max_length,
  1189. __libc_argv[1]);
  1190. if (value == NULL || w_addword(pwordexp, value))
  1191. goto no_space;
  1192. for (p = 2; __libc_argv[p + 1]; p++) {
  1193. char *newword = strdup(__libc_argv[p]);
  1194. if (newword == NULL || w_addword(pwordexp, newword))
  1195. goto no_space;
  1196. }
  1197. /* Start a new word with the last parameter. */
  1198. *word = w_newword(word_length, max_length);
  1199. value = __libc_argv[p];
  1200. } else {
  1201. free(env);
  1202. free(pattern);
  1203. return 0;
  1204. }
  1205. }
  1206. } else
  1207. value = getenv(env);
  1208. if (value == NULL && (flags & WRDE_UNDEF)) {
  1209. /* Variable not defined. */
  1210. error = WRDE_BADVAL;
  1211. goto do_error;
  1212. }
  1213. if (action != ACT_NONE) {
  1214. int expand_pattern = 0;
  1215. /* First, find out if we need to expand pattern (i.e. if we will
  1216. * use it). */
  1217. switch (action) {
  1218. case ACT_RP_SHORT_LEFT:
  1219. case ACT_RP_LONG_LEFT:
  1220. case ACT_RP_SHORT_RIGHT:
  1221. case ACT_RP_LONG_RIGHT:
  1222. /* Always expand for these. */
  1223. expand_pattern = 1;
  1224. break;
  1225. case ACT_NULL_ERROR:
  1226. case ACT_NULL_SUBST:
  1227. case ACT_NULL_ASSIGN:
  1228. if (!value || (!*value && colon_seen))
  1229. /* If param is unset, or set but null and a colon has been seen,
  1230. the expansion of the pattern will be needed. */
  1231. expand_pattern = 1;
  1232. break;
  1233. case ACT_NONNULL_SUBST:
  1234. /* Expansion of word will be needed if parameter is set and not null,
  1235. or set null but no colon has been seen. */
  1236. if (value && (*value || !colon_seen))
  1237. expand_pattern = 1;
  1238. break;
  1239. default:
  1240. assert(!"Unrecognised action!");
  1241. }
  1242. if (expand_pattern) {
  1243. /* We need to perform tilde expansion, parameter expansion,
  1244. command substitution, and arithmetic expansion. We also
  1245. have to be a bit careful with wildcard characters, as
  1246. pattern might be given to fnmatch soon. To do this, we
  1247. convert quotes to escapes. */
  1248. char *expanded;
  1249. size_t exp_len;
  1250. size_t exp_maxl;
  1251. char *p;
  1252. int quoted = 0; /* 1: single quotes; 2: double */
  1253. expanded = w_newword(&exp_len, &exp_maxl);
  1254. for (p = pattern; p && *p; p++) {
  1255. size_t offset;
  1256. switch (*p) {
  1257. case '"':
  1258. if (quoted == 2)
  1259. quoted = 0;
  1260. else if (quoted == 0)
  1261. quoted = 2;
  1262. else
  1263. break;
  1264. continue;
  1265. case '\'':
  1266. if (quoted == 1)
  1267. quoted = 0;
  1268. else if (quoted == 0)
  1269. quoted = 1;
  1270. else
  1271. break;
  1272. continue;
  1273. case '*':
  1274. case '?':
  1275. if (quoted) {
  1276. /* Convert quoted wildchar to escaped wildchar. */
  1277. expanded = w_addchar(expanded, &exp_len,
  1278. &exp_maxl, '\\');
  1279. if (expanded == NULL)
  1280. goto no_space;
  1281. }
  1282. break;
  1283. case '$':
  1284. offset = 0;
  1285. error = parse_dollars(&expanded, &exp_len, &exp_maxl, p,
  1286. &offset, flags, NULL, NULL, NULL, 1);
  1287. if (error) {
  1288. if (free_value)
  1289. free(value);
  1290. free(expanded);
  1291. goto do_error;
  1292. }
  1293. p += offset;
  1294. continue;
  1295. case '~':
  1296. if (quoted || exp_len)
  1297. break;
  1298. offset = 0;
  1299. error = parse_tilde(&expanded, &exp_len, &exp_maxl, p,
  1300. &offset, 0);
  1301. if (error) {
  1302. if (free_value)
  1303. free(value);
  1304. free(expanded);
  1305. goto do_error;
  1306. }
  1307. p += offset;
  1308. continue;
  1309. case '\\':
  1310. expanded = w_addchar(expanded, &exp_len, &exp_maxl, '\\');
  1311. ++p;
  1312. assert(*p); /* checked when extracted initially */
  1313. if (expanded == NULL)
  1314. goto no_space;
  1315. }
  1316. expanded = w_addchar(expanded, &exp_len, &exp_maxl, *p);
  1317. if (expanded == NULL)
  1318. goto no_space;
  1319. }
  1320. free(pattern);
  1321. pattern = expanded;
  1322. }
  1323. switch (action) {
  1324. case ACT_RP_SHORT_LEFT:
  1325. case ACT_RP_LONG_LEFT:
  1326. case ACT_RP_SHORT_RIGHT:
  1327. case ACT_RP_LONG_RIGHT:
  1328. {
  1329. char *p;
  1330. char c;
  1331. char *end;
  1332. if (value == NULL || pattern == NULL || *pattern == '\0')
  1333. break;
  1334. end = value + strlen(value);
  1335. switch (action) {
  1336. case ACT_RP_SHORT_LEFT:
  1337. for (p = value; p <= end; ++p) {
  1338. c = *p;
  1339. *p = '\0';
  1340. if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
  1341. *p = c;
  1342. if (free_value) {
  1343. char *newval = strdup(p);
  1344. if (newval == NULL) {
  1345. free(value);
  1346. goto no_space;
  1347. }
  1348. free(value);
  1349. value = newval;
  1350. } else
  1351. value = p;
  1352. break;
  1353. }
  1354. *p = c;
  1355. }
  1356. break;
  1357. case ACT_RP_LONG_LEFT:
  1358. for (p = end; p >= value; --p) {
  1359. c = *p;
  1360. *p = '\0';
  1361. if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
  1362. *p = c;
  1363. if (free_value) {
  1364. char *newval = strdup(p);
  1365. if (newval == NULL) {
  1366. free(value);
  1367. goto no_space;
  1368. }
  1369. free(value);
  1370. value = newval;
  1371. } else
  1372. value = p;
  1373. break;
  1374. }
  1375. *p = c;
  1376. }
  1377. break;
  1378. case ACT_RP_SHORT_RIGHT:
  1379. for (p = end; p >= value; --p) {
  1380. if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {
  1381. char *newval;
  1382. newval = malloc(p - value + 1);
  1383. if (newval == NULL) {
  1384. if (free_value)
  1385. free(value);
  1386. goto no_space;
  1387. }
  1388. *(char *) mempcpy(newval, value, p - value) = '\0';
  1389. if (free_value)
  1390. free(value);
  1391. value = newval;
  1392. free_value = 1;
  1393. break;
  1394. }
  1395. }
  1396. break;
  1397. case ACT_RP_LONG_RIGHT:
  1398. for (p = value; p <= end; ++p) {
  1399. if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {
  1400. char *newval;
  1401. newval = malloc(p - value + 1);
  1402. if (newval == NULL) {
  1403. if (free_value)
  1404. free(value);
  1405. goto no_space;
  1406. }
  1407. *(char *) mempcpy(newval, value, p - value) = '\0';
  1408. if (free_value)
  1409. free(value);
  1410. value = newval;
  1411. free_value = 1;
  1412. break;
  1413. }
  1414. }
  1415. break;
  1416. default:
  1417. break;
  1418. }
  1419. break;
  1420. }
  1421. case ACT_NULL_ERROR:
  1422. if (value && *value)
  1423. /* Substitute parameter */
  1424. break;
  1425. error = 0;
  1426. if (!colon_seen && value)
  1427. /* Substitute NULL */
  1428. ;
  1429. else if (*pattern)
  1430. fprintf(stderr, "%s: %s\n", env, pattern);
  1431. else {
  1432. fprintf(stderr, "%s: parameter null or not set\n", env);
  1433. error = WRDE_BADVAL;
  1434. }
  1435. if (free_value)
  1436. free(value);
  1437. goto do_error;
  1438. case ACT_NULL_SUBST:
  1439. if (value && *value)
  1440. /* Substitute parameter */
  1441. break;
  1442. if (free_value)
  1443. free(value);
  1444. if (!colon_seen && value)
  1445. /* Substitute NULL */
  1446. goto success;
  1447. value = pattern ? strdup(pattern) : pattern;
  1448. free_value = 1;
  1449. if (pattern && !value)
  1450. goto no_space;
  1451. break;
  1452. case ACT_NONNULL_SUBST:
  1453. if (value && (*value || !colon_seen)) {
  1454. if (free_value)
  1455. free(value);
  1456. value = pattern ? strdup(pattern) : pattern;
  1457. free_value = 1;
  1458. if (pattern && !value)
  1459. goto no_space;
  1460. break;
  1461. }
  1462. /* Substitute NULL */
  1463. if (free_value)
  1464. free(value);
  1465. goto success;
  1466. case ACT_NULL_ASSIGN:
  1467. if (value && *value)
  1468. /* Substitute parameter */
  1469. break;
  1470. if (!colon_seen && value) {
  1471. /* Substitute NULL */
  1472. if (free_value)
  1473. free(value);
  1474. goto success;
  1475. }
  1476. if (free_value)
  1477. free(value);
  1478. value = pattern ? strdup(pattern) : pattern;
  1479. free_value = 1;
  1480. if (pattern && !value)
  1481. goto no_space;
  1482. setenv(env, value, 1);
  1483. break;
  1484. default:
  1485. assert(!"Unrecognised action!");
  1486. }
  1487. }
  1488. free(env);
  1489. env = NULL;
  1490. free(pattern);
  1491. pattern = NULL;
  1492. if (seen_hash) {
  1493. char param_length[21];
  1494. param_length[20] = '\0';
  1495. *word = w_addstr(*word, word_length, max_length,
  1496. _itoa(value ? strlen(value) : 0,
  1497. &param_length[20]));
  1498. if (free_value) {
  1499. assert(value != NULL);
  1500. free(value);
  1501. }
  1502. return *word ? 0 : WRDE_NOSPACE;
  1503. }
  1504. if (value == NULL)
  1505. return 0;
  1506. if (quoted || !pwordexp) {
  1507. /* Quoted - no field split */
  1508. *word = w_addstr(*word, word_length, max_length, value);
  1509. if (free_value)
  1510. free(value);
  1511. return *word ? 0 : WRDE_NOSPACE;
  1512. } else {
  1513. /* Need to field-split */
  1514. char *value_copy = strdup(value); /* Don't modify value */
  1515. char *field_begin = value_copy;
  1516. int seen_nonws_ifs = 0;
  1517. if (free_value)
  1518. free(value);
  1519. if (value_copy == NULL)
  1520. goto no_space;
  1521. do {
  1522. char *field_end = field_begin;
  1523. char *next_field;
  1524. /* If this isn't the first field, start a new word */
  1525. if (field_begin != value_copy) {
  1526. if (w_addword(pwordexp, *word) == WRDE_NOSPACE) {
  1527. free(value_copy);
  1528. goto no_space;
  1529. }
  1530. *word = w_newword(word_length, max_length);
  1531. }
  1532. /* Skip IFS whitespace before the field */
  1533. field_begin += strspn(field_begin, ifs_white);
  1534. if (!seen_nonws_ifs && *field_begin == 0)
  1535. /* Nothing but whitespace */
  1536. break;
  1537. /* Search for the end of the field */
  1538. field_end = field_begin + strcspn(field_begin, ifs);
  1539. /* Set up pointer to the character after end of field and
  1540. skip whitespace IFS after it. */
  1541. next_field = field_end + strspn(field_end, ifs_white);
  1542. /* Skip at most one non-whitespace IFS character after the field */
  1543. seen_nonws_ifs = 0;
  1544. if (*next_field && strchr(ifs, *next_field)) {
  1545. seen_nonws_ifs = 1;
  1546. next_field++;
  1547. }
  1548. /* Null-terminate it */
  1549. *field_end = 0;
  1550. /* Tag a copy onto the current word */
  1551. *word = w_addstr(*word, word_length, max_length, field_begin);
  1552. if (*word == NULL && *field_begin != '\0') {
  1553. free(value_copy);
  1554. goto no_space;
  1555. }
  1556. field_begin = next_field;
  1557. }
  1558. while (seen_nonws_ifs || *field_begin);
  1559. free(value_copy);
  1560. }
  1561. return 0;
  1562. success:
  1563. error = 0;
  1564. goto do_error;
  1565. no_space:
  1566. error = WRDE_NOSPACE;
  1567. goto do_error;
  1568. syntax:
  1569. error = WRDE_SYNTAX;
  1570. do_error:
  1571. free(env);
  1572. free(pattern);
  1573. return error;
  1574. }
  1575. #else
  1576. static __inline__ int
  1577. parse_backtick(char **word, size_t * word_length, size_t * max_length,
  1578. const char *words, size_t * offset, int flags,
  1579. wordexp_t * pwordexp, const char *ifs,
  1580. const char *ifs_white)
  1581. {
  1582. return 0;
  1583. }
  1584. #endif
  1585. static int
  1586. parse_dollars(char **word, size_t * word_length, size_t * max_length,
  1587. const char *words, size_t * offset, int flags,
  1588. wordexp_t * pwordexp, const char *ifs, const char *ifs_white,
  1589. int quoted)
  1590. {
  1591. /* We are poised _at_ "$" */
  1592. switch (words[1 + *offset]) {
  1593. case '"':
  1594. case '\'':
  1595. case 0:
  1596. *word = w_addchar(*word, word_length, max_length, '$');
  1597. return *word ? 0 : WRDE_NOSPACE;
  1598. #ifdef __WORDEXP_FULL
  1599. case '(':
  1600. if (words[2 + *offset] == '(') {
  1601. /* Differentiate between $((1+3)) and $((echo);(ls)) */
  1602. int i = 3 + *offset;
  1603. int depth = 0;
  1604. while (words[i] && !(depth == 0 && words[i] == ')')) {
  1605. if (words[i] == '(')
  1606. ++depth;
  1607. else if (words[i] == ')')
  1608. --depth;
  1609. ++i;
  1610. }
  1611. if (words[i] == ')' && words[i + 1] == ')') {
  1612. (*offset) += 3;
  1613. /* Call parse_arith -- 0 is for "no brackets" */
  1614. return parse_arith(word, word_length, max_length, words,
  1615. offset, flags, 0);
  1616. }
  1617. }
  1618. if (flags & WRDE_NOCMD)
  1619. return WRDE_CMDSUB;
  1620. (*offset) += 2;
  1621. return parse_comm(word, word_length, max_length, words, offset,
  1622. flags, quoted ? NULL : pwordexp, ifs, ifs_white);
  1623. case '[':
  1624. (*offset) += 2;
  1625. /* Call parse_arith -- 1 is for "brackets" */
  1626. return parse_arith(word, word_length, max_length, words, offset,
  1627. flags, 1);
  1628. case '{':
  1629. default:
  1630. ++(*offset); /* parse_param needs to know if "{" is there */
  1631. return parse_param(word, word_length, max_length, words, offset,
  1632. flags, pwordexp, ifs, ifs_white, quoted);
  1633. #else
  1634. default:
  1635. ++(*offset); /* parse_param needs to know if "{" is there */
  1636. return 0;
  1637. #endif
  1638. }
  1639. }
  1640. static int
  1641. parse_dquote(char **word, size_t * word_length, size_t * max_length,
  1642. const char *words, size_t * offset, int flags,
  1643. wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
  1644. {
  1645. /* We are poised just after a double-quote */
  1646. int error;
  1647. for (; words[*offset]; ++(*offset)) {
  1648. switch (words[*offset]) {
  1649. case '"':
  1650. return 0;
  1651. case '$':
  1652. error = parse_dollars(word, word_length, max_length, words, offset,
  1653. flags, pwordexp, ifs, ifs_white, 1);
  1654. /* The ``1'' here is to tell parse_dollars not to
  1655. * split the fields. It may need to, however ("$@").
  1656. */
  1657. if (error)
  1658. return error;
  1659. break;
  1660. case '`':
  1661. if (flags & WRDE_NOCMD)
  1662. return WRDE_CMDSUB;
  1663. ++(*offset);
  1664. error = parse_backtick(word, word_length, max_length, words,
  1665. offset, flags, NULL, NULL, NULL);
  1666. /* The first NULL here is to tell parse_backtick not to
  1667. * split the fields.
  1668. */
  1669. if (error)
  1670. return error;
  1671. break;
  1672. case '\\':
  1673. error = parse_qtd_backslash(word, word_length, max_length, words,
  1674. offset);
  1675. if (error)
  1676. return error;
  1677. break;
  1678. default:
  1679. *word = w_addchar(*word, word_length, max_length, words[*offset]);
  1680. if (*word == NULL)
  1681. return WRDE_NOSPACE;
  1682. }
  1683. }
  1684. /* Unterminated string */
  1685. return WRDE_SYNTAX;
  1686. }
  1687. /*
  1688. * wordfree() is to be called after pwordexp is finished with.
  1689. */
  1690. void wordfree(wordexp_t * pwordexp)
  1691. {
  1692. /* wordexp can set pwordexp to NULL */
  1693. if (pwordexp && pwordexp->we_wordv) {
  1694. char **wordv = pwordexp->we_wordv;
  1695. for (wordv += pwordexp->we_offs; *wordv; ++wordv)
  1696. free(*wordv);
  1697. free(pwordexp->we_wordv);
  1698. pwordexp->we_wordv = NULL;
  1699. }
  1700. }
  1701. libc_hidden_def(wordfree)
  1702. /*
  1703. * wordexp()
  1704. */
  1705. int wordexp(const char *words, wordexp_t * we, int flags)
  1706. {
  1707. size_t words_offset;
  1708. size_t word_length;
  1709. size_t max_length;
  1710. char *word = w_newword(&word_length, &max_length);
  1711. int error;
  1712. char *ifs;
  1713. char ifs_white[4];
  1714. wordexp_t old_word = *we;
  1715. if (flags & WRDE_REUSE) {
  1716. /* Minimal implementation of WRDE_REUSE for now */
  1717. wordfree(we);
  1718. old_word.we_wordv = NULL;
  1719. }
  1720. if ((flags & WRDE_APPEND) == 0) {
  1721. we->we_wordc = 0;
  1722. if (flags & WRDE_DOOFFS) {
  1723. we->we_wordv = calloc(1 + we->we_offs, sizeof(char *));
  1724. if (we->we_wordv == NULL) {
  1725. error = WRDE_NOSPACE;
  1726. goto do_error;
  1727. }
  1728. } else {
  1729. we->we_wordv = calloc(1, sizeof(char *));
  1730. if (we->we_wordv == NULL) {
  1731. error = WRDE_NOSPACE;
  1732. goto do_error;
  1733. }
  1734. we->we_offs = 0;
  1735. }
  1736. }
  1737. /* Find out what the field separators are.
  1738. * There are two types: whitespace and non-whitespace.
  1739. */
  1740. ifs = getenv("IFS");
  1741. if (!ifs)
  1742. /* IFS unset - use <space><tab><newline>. */
  1743. ifs = strcpy(ifs_white, " \t\n");
  1744. else {
  1745. char *ifsch = ifs;
  1746. char *whch = ifs_white;
  1747. /* Start off with no whitespace IFS characters */
  1748. ifs_white[0] = '\0';
  1749. while (*ifsch != '\0') {
  1750. if ((*ifsch == ' ') || (*ifsch == '\t') || (*ifsch == '\n')) {
  1751. /* Whitespace IFS. See first whether it is already in our
  1752. collection. */
  1753. char *runp = ifs_white;
  1754. while (runp < whch && *runp != '\0' && *runp != *ifsch)
  1755. ++runp;
  1756. if (runp == whch)
  1757. *whch++ = *ifsch;
  1758. }
  1759. ++ifsch;
  1760. }
  1761. *whch = '\0';
  1762. }
  1763. for (words_offset = 0; words[words_offset]; ++words_offset)
  1764. switch (words[words_offset]) {
  1765. case '\\':
  1766. error = parse_backslash(&word, &word_length, &max_length, words,
  1767. &words_offset);
  1768. if (error)
  1769. goto do_error;
  1770. break;
  1771. case '$':
  1772. error = parse_dollars(&word, &word_length, &max_length, words,
  1773. &words_offset, flags, we, ifs, ifs_white,
  1774. 0);
  1775. if (error)
  1776. goto do_error;
  1777. break;
  1778. case '`':
  1779. if (flags & WRDE_NOCMD) {
  1780. error = WRDE_CMDSUB;
  1781. goto do_error;
  1782. }
  1783. ++words_offset;
  1784. error = parse_backtick(&word, &word_length, &max_length, words,
  1785. &words_offset, flags, we, ifs,
  1786. ifs_white);
  1787. if (error)
  1788. goto do_error;
  1789. break;
  1790. case '"':
  1791. ++words_offset;
  1792. error = parse_dquote(&word, &word_length, &max_length, words,
  1793. &words_offset, flags, we, ifs, ifs_white);
  1794. if (error)
  1795. goto do_error;
  1796. if (!word_length) {
  1797. error = w_addword(we, NULL);
  1798. if (error)
  1799. return error;
  1800. }
  1801. break;
  1802. case '\'':
  1803. ++words_offset;
  1804. error = parse_squote(&word, &word_length, &max_length, words,
  1805. &words_offset);
  1806. if (error)
  1807. goto do_error;
  1808. if (!word_length) {
  1809. error = w_addword(we, NULL);
  1810. if (error)
  1811. return error;
  1812. }
  1813. break;
  1814. case '~':
  1815. error = parse_tilde(&word, &word_length, &max_length, words,
  1816. &words_offset, we->we_wordc);
  1817. if (error)
  1818. goto do_error;
  1819. break;
  1820. case '*':
  1821. case '[':
  1822. case '?':
  1823. error = parse_glob(&word, &word_length, &max_length, words,
  1824. &words_offset, flags, we, ifs, ifs_white);
  1825. if (error)
  1826. goto do_error;
  1827. break;
  1828. default:
  1829. /* Is it a word separator? */
  1830. if (strchr(" \t", words[words_offset]) == NULL) {
  1831. char ch = words[words_offset];
  1832. /* Not a word separator -- but is it a valid word char? */
  1833. if (strchr("\n|&;<>(){}", ch)) {
  1834. /* Fail */
  1835. error = WRDE_BADCHAR;
  1836. goto do_error;
  1837. }
  1838. /* "Ordinary" character -- add it to word */
  1839. word = w_addchar(word, &word_length, &max_length, ch);
  1840. if (word == NULL) {
  1841. error = WRDE_NOSPACE;
  1842. goto do_error;
  1843. }
  1844. break;
  1845. }
  1846. /* If a word has been delimited, add it to the list. */
  1847. if (word != NULL) {
  1848. error = w_addword(we, word);
  1849. if (error)
  1850. goto do_error;
  1851. }
  1852. word = w_newword(&word_length, &max_length);
  1853. }
  1854. /* End of string */
  1855. /* There was a word separator at the end */
  1856. if (word == NULL) /* i.e. w_newword */
  1857. return 0;
  1858. /* There was no field separator at the end */
  1859. return w_addword(we, word);
  1860. do_error:
  1861. /* Error:
  1862. * free memory used (unless error is WRDE_NOSPACE), and
  1863. * set we members back to what they were.
  1864. */
  1865. free(word);
  1866. if (error == WRDE_NOSPACE)
  1867. return WRDE_NOSPACE;
  1868. if ((flags & WRDE_APPEND) == 0)
  1869. wordfree(we);
  1870. *we = old_word;
  1871. return error;
  1872. }