wordexp.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  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. int fd;
  667. const char *args[4] = { _PATH_BSHELL, "-c", comm, NULL };
  668. /* Execute the command, or just check syntax? */
  669. if (noexec)
  670. args[1] = "-nc";
  671. /* Redirect output. */
  672. fd = fildes[1];
  673. if (fd != 1) {
  674. dup2(fd, 1);
  675. close(fd);
  676. }
  677. /* Redirect stderr to /dev/null if we have to. */
  678. if (showerr == 0) {
  679. close(2);
  680. fd = open(_PATH_DEVNULL, O_WRONLY);
  681. if (fd >= 0 && fd != 2) {
  682. dup2(fd, 2);
  683. close(fd);
  684. }
  685. }
  686. /* Make sure the subshell doesn't field-split on our behalf. */
  687. unsetenv("IFS");
  688. if (fildes[0] != 1)
  689. close(fildes[0]);
  690. execve(_PATH_BSHELL, (char *const *) args, __environ);
  691. /* Bad. What now? */
  692. abort();
  693. }
  694. /* Function to execute a command and retrieve the results */
  695. /* pwordexp contains NULL if field-splitting is forbidden */
  696. static int
  697. exec_comm(char *comm, char **word, size_t * word_length,
  698. size_t * max_length, int flags, wordexp_t * pwordexp,
  699. const char *ifs, const char *ifs_white)
  700. {
  701. int fildes[2];
  702. int bufsize = 128;
  703. int buflen;
  704. int i;
  705. int status = 0;
  706. size_t maxnewlines = 0;
  707. char *buffer;
  708. pid_t pid;
  709. /* Don't fork() unless necessary */
  710. if (!comm || !*comm)
  711. return 0;
  712. if (pipe(fildes))
  713. /* Bad */
  714. return WRDE_NOSPACE;
  715. if ((pid = fork()) < 0) {
  716. /* Bad */
  717. close(fildes[0]);
  718. close(fildes[1]);
  719. return WRDE_NOSPACE;
  720. }
  721. if (pid == 0)
  722. exec_comm_child(comm, fildes, (flags & WRDE_SHOWERR), 0);
  723. /* Parent */
  724. close(fildes[1]);
  725. buffer = alloca(bufsize);
  726. if (!pwordexp)
  727. /* Quoted - no field splitting */
  728. {
  729. while (1) {
  730. if ((buflen = read(fildes[0], buffer, bufsize)) < 1) {
  731. if (waitpid(pid, &status, WNOHANG) == 0)
  732. continue;
  733. if ((buflen = read(fildes[0], buffer, bufsize)) < 1)
  734. break;
  735. }
  736. maxnewlines += buflen;
  737. *word = w_addmem(*word, word_length, max_length, buffer, buflen);
  738. if (*word == NULL)
  739. goto no_space;
  740. }
  741. } else
  742. /* Not quoted - split fields */
  743. {
  744. int copying = 0;
  745. /* 'copying' is:
  746. * 0 when searching for first character in a field not IFS white space
  747. * 1 when copying the text of a field
  748. * 2 when searching for possible non-whitespace IFS
  749. * 3 when searching for non-newline after copying field
  750. */
  751. while (1) {
  752. if ((buflen = read(fildes[0], buffer, bufsize)) < 1) {
  753. if (waitpid(pid, &status, WNOHANG) == 0)
  754. continue;
  755. if ((buflen = read(fildes[0], buffer, bufsize)) < 1)
  756. break;
  757. }
  758. for (i = 0; i < buflen; ++i) {
  759. if (strchr(ifs, buffer[i]) != NULL) {
  760. /* Current character is IFS */
  761. if (strchr(ifs_white, buffer[i]) == NULL) {
  762. /* Current character is IFS but not whitespace */
  763. if (copying == 2) {
  764. /* current character
  765. * |
  766. * V
  767. * eg: text<space><comma><space>moretext
  768. *
  769. * So, strip whitespace IFS (like at the start)
  770. */
  771. copying = 0;
  772. continue;
  773. }
  774. copying = 0;
  775. /* fall through and delimit field.. */
  776. } else {
  777. if (buffer[i] == '\n') {
  778. /* Current character is (IFS) newline */
  779. /* If copying a field, this is the end of it,
  780. but maybe all that's left is trailing newlines.
  781. So start searching for a non-newline. */
  782. if (copying == 1)
  783. copying = 3;
  784. continue;
  785. } else {
  786. /* Current character is IFS white space, but
  787. not a newline */
  788. /* If not either copying a field or searching
  789. for non-newline after a field, ignore it */
  790. if (copying != 1 && copying != 3)
  791. continue;
  792. /* End of field (search for non-ws IFS afterwards) */
  793. copying = 2;
  794. }
  795. }
  796. /* First IFS white space (non-newline), or IFS non-whitespace.
  797. * Delimit the field. Nulls are converted by w_addword. */
  798. if (w_addword(pwordexp, *word) == WRDE_NOSPACE)
  799. goto no_space;
  800. *word = w_newword(word_length, max_length);
  801. maxnewlines = 0;
  802. /* fall back round the loop.. */
  803. } else {
  804. /* Not IFS character */
  805. if (copying == 3) {
  806. /* Nothing but (IFS) newlines since the last field,
  807. so delimit it here before starting new word */
  808. if (w_addword(pwordexp, *word) == WRDE_NOSPACE)
  809. goto no_space;
  810. *word = w_newword(word_length, max_length);
  811. }
  812. copying = 1;
  813. if (buffer[i] == '\n') /* happens if newline not in IFS */
  814. maxnewlines++;
  815. else
  816. maxnewlines = 0;
  817. *word = w_addchar(*word, word_length, max_length,
  818. buffer[i]);
  819. if (*word == NULL)
  820. goto no_space;
  821. }
  822. }
  823. }
  824. }
  825. /* Chop off trailing newlines (required by POSIX.2) */
  826. /* Ensure we don't go back further than the beginning of the
  827. substitution (i.e. remove maxnewlines bytes at most) */
  828. while (maxnewlines-- != 0 &&
  829. *word_length > 0 && (*word)[*word_length - 1] == '\n') {
  830. (*word)[--*word_length] = '\0';
  831. /* If the last word was entirely newlines, turn it into a new word
  832. * which can be ignored if there's nothing following it. */
  833. if (*word_length == 0) {
  834. free(*word);
  835. *word = w_newword(word_length, max_length);
  836. break;
  837. }
  838. }
  839. close(fildes[0]);
  840. /* Check for syntax error (re-execute but with "-n" flag) */
  841. if (buflen < 1 && status != 0) {
  842. if ((pid = fork()) < 0) {
  843. /* Bad */
  844. return WRDE_NOSPACE;
  845. }
  846. if (pid == 0) {
  847. fildes[0] = fildes[1] = -1;
  848. exec_comm_child(comm, fildes, 0, 1);
  849. }
  850. if (waitpid(pid, &status, 0) == pid && status != 0)
  851. return WRDE_SYNTAX;
  852. }
  853. return 0;
  854. no_space:
  855. kill(pid, SIGKILL);
  856. waitpid(pid, NULL, 0);
  857. close(fildes[0]);
  858. return WRDE_NOSPACE;
  859. }
  860. static int
  861. parse_comm(char **word, size_t * word_length, size_t * max_length,
  862. const char *words, size_t * offset, int flags,
  863. wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
  864. {
  865. /* We are poised just after "$(" */
  866. int paren_depth = 1;
  867. int error = 0;
  868. int quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
  869. size_t comm_length;
  870. size_t comm_maxlen;
  871. char *comm = w_newword(&comm_length, &comm_maxlen);
  872. for (; words[*offset]; ++(*offset)) {
  873. switch (words[*offset]) {
  874. case '\'':
  875. if (quoted == 0)
  876. quoted = 1;
  877. else if (quoted == 1)
  878. quoted = 0;
  879. break;
  880. case '"':
  881. if (quoted == 0)
  882. quoted = 2;
  883. else if (quoted == 2)
  884. quoted = 0;
  885. break;
  886. case ')':
  887. if (!quoted && --paren_depth == 0) {
  888. /* Go -- give script to the shell */
  889. if (comm) {
  890. error = exec_comm(comm, word, word_length, max_length,
  891. flags, pwordexp, ifs, ifs_white);
  892. free(comm);
  893. }
  894. return error;
  895. }
  896. /* This is just part of the script */
  897. break;
  898. case '(':
  899. if (!quoted)
  900. ++paren_depth;
  901. }
  902. comm = w_addchar(comm, &comm_length, &comm_maxlen, words[*offset]);
  903. if (comm == NULL)
  904. return WRDE_NOSPACE;
  905. }
  906. /* Premature end */
  907. free(comm);
  908. return WRDE_SYNTAX;
  909. }
  910. static int
  911. parse_backtick(char **word, size_t * word_length, size_t * max_length,
  912. const char *words, size_t * offset, int flags,
  913. wordexp_t * pwordexp, const char *ifs,
  914. const char *ifs_white)
  915. {
  916. /* We are poised just after "`" */
  917. int error;
  918. int squoting = 0;
  919. size_t comm_length;
  920. size_t comm_maxlen;
  921. char *comm = w_newword(&comm_length, &comm_maxlen);
  922. for (; words[*offset]; ++(*offset)) {
  923. switch (words[*offset]) {
  924. case '`':
  925. /* Go -- give the script to the shell */
  926. error = exec_comm(comm, word, word_length, max_length, flags,
  927. pwordexp, ifs, ifs_white);
  928. free(comm);
  929. return error;
  930. case '\\':
  931. if (squoting) {
  932. error = parse_qtd_backslash(&comm, &comm_length, &comm_maxlen,
  933. words, offset);
  934. if (error) {
  935. free(comm);
  936. return error;
  937. }
  938. break;
  939. }
  940. ++(*offset);
  941. error = parse_backslash(&comm, &comm_length, &comm_maxlen, words,
  942. offset);
  943. if (error) {
  944. free(comm);
  945. return error;
  946. }
  947. break;
  948. case '\'':
  949. squoting = 1 - squoting;
  950. default:
  951. comm = w_addchar(comm, &comm_length, &comm_maxlen,
  952. words[*offset]);
  953. if (comm == NULL)
  954. return WRDE_NOSPACE;
  955. }
  956. }
  957. /* Premature end */
  958. free(comm);
  959. return WRDE_SYNTAX;
  960. }
  961. static int
  962. parse_param(char **word, size_t * word_length, size_t * max_length,
  963. const char *words, size_t * offset, int flags,
  964. wordexp_t * pwordexp, const char *ifs, const char *ifs_white,
  965. int quoted)
  966. {
  967. /* We are poised just after "$" */
  968. enum action {
  969. ACT_NONE,
  970. ACT_RP_SHORT_LEFT = '#',
  971. ACT_RP_LONG_LEFT = 'L',
  972. ACT_RP_SHORT_RIGHT = '%',
  973. ACT_RP_LONG_RIGHT = 'R',
  974. ACT_NULL_ERROR = '?',
  975. ACT_NULL_SUBST = '-',
  976. ACT_NONNULL_SUBST = '+',
  977. ACT_NULL_ASSIGN = '='
  978. };
  979. size_t env_length;
  980. size_t env_maxlen;
  981. size_t pat_length;
  982. size_t pat_maxlen;
  983. size_t start = *offset;
  984. char *env;
  985. char *pattern;
  986. char *value = NULL;
  987. enum action action = ACT_NONE;
  988. int depth = 0;
  989. int colon_seen = 0;
  990. int seen_hash = 0;
  991. int free_value = 0;
  992. int pattern_is_quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
  993. int error;
  994. int special = 0;
  995. char buffer[21];
  996. int brace = words[*offset] == '{';
  997. env = w_newword(&env_length, &env_maxlen);
  998. pattern = w_newword(&pat_length, &pat_maxlen);
  999. if (brace)
  1000. ++ * offset;
  1001. /* First collect the parameter name. */
  1002. if (words[*offset] == '#') {
  1003. seen_hash = 1;
  1004. if (!brace)
  1005. goto envsubst;
  1006. ++*offset;
  1007. }
  1008. if (isalpha(words[*offset]) || words[*offset] == '_') {
  1009. /* Normal parameter name. */
  1010. do {
  1011. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  1012. if (env == NULL)
  1013. goto no_space;
  1014. }
  1015. while (isalnum(words[++*offset]) || words[*offset] == '_');
  1016. } else if (isdigit(words[*offset])) {
  1017. /* Numeric parameter name. */
  1018. special = 1;
  1019. do {
  1020. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  1021. if (env == NULL)
  1022. goto no_space;
  1023. if (!brace)
  1024. goto envsubst;
  1025. }
  1026. while (isdigit(words[++*offset]));
  1027. } else if (strchr("*@$", words[*offset]) != NULL) {
  1028. /* Special parameter. */
  1029. special = 1;
  1030. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  1031. if (env == NULL)
  1032. goto no_space;
  1033. ++*offset;
  1034. } else {
  1035. if (brace)
  1036. goto syntax;
  1037. }
  1038. if (brace) {
  1039. /* Check for special action to be applied to the value. */
  1040. switch (words[*offset]) {
  1041. case '}':
  1042. /* Evaluate. */
  1043. goto envsubst;
  1044. case '#':
  1045. action = ACT_RP_SHORT_LEFT;
  1046. if (words[1 + *offset] == '#') {
  1047. ++*offset;
  1048. action = ACT_RP_LONG_LEFT;
  1049. }
  1050. break;
  1051. case '%':
  1052. action = ACT_RP_SHORT_RIGHT;
  1053. if (words[1 + *offset] == '%') {
  1054. ++*offset;
  1055. action = ACT_RP_LONG_RIGHT;
  1056. }
  1057. break;
  1058. case ':':
  1059. if (strchr("-=?+", words[1 + *offset]) == NULL)
  1060. goto syntax;
  1061. colon_seen = 1;
  1062. action = words[++*offset];
  1063. break;
  1064. case '-':
  1065. case '=':
  1066. case '?':
  1067. case '+':
  1068. action = words[*offset];
  1069. break;
  1070. default:
  1071. goto syntax;
  1072. }
  1073. /* Now collect the pattern, but don't expand it yet. */
  1074. ++*offset;
  1075. for (; words[*offset]; ++(*offset)) {
  1076. switch (words[*offset]) {
  1077. case '{':
  1078. if (!pattern_is_quoted)
  1079. ++depth;
  1080. break;
  1081. case '}':
  1082. if (!pattern_is_quoted) {
  1083. if (depth == 0)
  1084. goto envsubst;
  1085. --depth;
  1086. }
  1087. break;
  1088. case '\\':
  1089. if (pattern_is_quoted)
  1090. /* Quoted; treat as normal character. */
  1091. break;
  1092. /* Otherwise, it's an escape: next character is literal. */
  1093. if (words[++*offset] == '\0')
  1094. goto syntax;
  1095. pattern = w_addchar(pattern, &pat_length, &pat_maxlen, '\\');
  1096. if (pattern == NULL)
  1097. goto no_space;
  1098. break;
  1099. case '\'':
  1100. if (pattern_is_quoted == 0)
  1101. pattern_is_quoted = 1;
  1102. else if (pattern_is_quoted == 1)
  1103. pattern_is_quoted = 0;
  1104. break;
  1105. case '"':
  1106. if (pattern_is_quoted == 0)
  1107. pattern_is_quoted = 2;
  1108. else if (pattern_is_quoted == 2)
  1109. pattern_is_quoted = 0;
  1110. break;
  1111. }
  1112. pattern = w_addchar(pattern, &pat_length, &pat_maxlen,
  1113. words[*offset]);
  1114. if (pattern == NULL)
  1115. goto no_space;
  1116. }
  1117. }
  1118. /* End of input string -- remember to reparse the character that we
  1119. * stopped at. */
  1120. --(*offset);
  1121. envsubst:
  1122. if (words[start] == '{' && words[*offset] != '}')
  1123. goto syntax;
  1124. if (env == NULL) {
  1125. if (seen_hash) {
  1126. /* $# expands to the number of positional parameters */
  1127. buffer[20] = '\0';
  1128. value = _itoa(__libc_argc - 1, &buffer[20]);
  1129. seen_hash = 0;
  1130. } else {
  1131. /* Just $ on its own */
  1132. *offset = start - 1;
  1133. *word = w_addchar(*word, word_length, max_length, '$');
  1134. return *word ? 0 : WRDE_NOSPACE;
  1135. }
  1136. }
  1137. /* Is it a numeric parameter? */
  1138. else if (isdigit(env[0])) {
  1139. int n = atoi(env);
  1140. if (n >= __libc_argc)
  1141. /* Substitute NULL. */
  1142. value = NULL;
  1143. else
  1144. /* Replace with appropriate positional parameter. */
  1145. value = __libc_argv[n];
  1146. }
  1147. /* Is it a special parameter? */
  1148. else if (special) {
  1149. /* Is it `$$'? */
  1150. if (*env == '$') {
  1151. buffer[20] = '\0';
  1152. value = _itoa(getpid(), &buffer[20]);
  1153. }
  1154. /* Is it `${#*}' or `${#@}'? */
  1155. else if ((*env == '*' || *env == '@') && seen_hash) {
  1156. buffer[20] = '\0';
  1157. value = _itoa(__libc_argc > 0 ? __libc_argc - 1 : 0,
  1158. &buffer[20]);
  1159. *word = w_addstr(*word, word_length, max_length, value);
  1160. free(env);
  1161. free(pattern);
  1162. return *word ? 0 : WRDE_NOSPACE;
  1163. }
  1164. /* Is it `$*' or `$@' (unquoted) ? */
  1165. else if (*env == '*' || (*env == '@' && !quoted)) {
  1166. size_t plist_len = 0;
  1167. int p;
  1168. char *end;
  1169. /* Build up value parameter by parameter (copy them) */
  1170. for (p = 1; __libc_argv[p]; ++p)
  1171. plist_len += strlen(__libc_argv[p]) + 1; /* for space */
  1172. value = malloc(plist_len);
  1173. if (value == NULL)
  1174. goto no_space;
  1175. end = value;
  1176. *end = 0;
  1177. for (p = 1; __libc_argv[p]; ++p) {
  1178. if (p > 1)
  1179. *end++ = ' ';
  1180. end = stpcpy(end, __libc_argv[p]);
  1181. }
  1182. free_value = 1;
  1183. } else {
  1184. /* Must be a quoted `$@' */
  1185. assert(*env == '@' && quoted);
  1186. /* Each parameter is a separate word ("$@") */
  1187. if (__libc_argc == 2)
  1188. value = __libc_argv[1];
  1189. else if (__libc_argc > 2) {
  1190. int p;
  1191. /* Append first parameter to current word. */
  1192. value = w_addstr(*word, word_length, max_length,
  1193. __libc_argv[1]);
  1194. if (value == NULL || w_addword(pwordexp, value))
  1195. goto no_space;
  1196. for (p = 2; __libc_argv[p + 1]; p++) {
  1197. char *newword = strdup(__libc_argv[p]);
  1198. if (newword == NULL || w_addword(pwordexp, newword))
  1199. goto no_space;
  1200. }
  1201. /* Start a new word with the last parameter. */
  1202. *word = w_newword(word_length, max_length);
  1203. value = __libc_argv[p];
  1204. } else {
  1205. free(env);
  1206. free(pattern);
  1207. return 0;
  1208. }
  1209. }
  1210. } else
  1211. value = getenv(env);
  1212. if (value == NULL && (flags & WRDE_UNDEF)) {
  1213. /* Variable not defined. */
  1214. error = WRDE_BADVAL;
  1215. goto do_error;
  1216. }
  1217. if (action != ACT_NONE) {
  1218. int expand_pattern = 0;
  1219. /* First, find out if we need to expand pattern (i.e. if we will
  1220. * use it). */
  1221. switch (action) {
  1222. case ACT_RP_SHORT_LEFT:
  1223. case ACT_RP_LONG_LEFT:
  1224. case ACT_RP_SHORT_RIGHT:
  1225. case ACT_RP_LONG_RIGHT:
  1226. /* Always expand for these. */
  1227. expand_pattern = 1;
  1228. break;
  1229. case ACT_NULL_ERROR:
  1230. case ACT_NULL_SUBST:
  1231. case ACT_NULL_ASSIGN:
  1232. if (!value || (!*value && colon_seen))
  1233. /* If param is unset, or set but null and a colon has been seen,
  1234. the expansion of the pattern will be needed. */
  1235. expand_pattern = 1;
  1236. break;
  1237. case ACT_NONNULL_SUBST:
  1238. /* Expansion of word will be needed if parameter is set and not null,
  1239. or set null but no colon has been seen. */
  1240. if (value && (*value || !colon_seen))
  1241. expand_pattern = 1;
  1242. break;
  1243. default:
  1244. assert(!"Unrecognised action!");
  1245. }
  1246. if (expand_pattern) {
  1247. /* We need to perform tilde expansion, parameter expansion,
  1248. command substitution, and arithmetic expansion. We also
  1249. have to be a bit careful with wildcard characters, as
  1250. pattern might be given to fnmatch soon. To do this, we
  1251. convert quotes to escapes. */
  1252. char *expanded;
  1253. size_t exp_len;
  1254. size_t exp_maxl;
  1255. char *p;
  1256. int quoted = 0; /* 1: single quotes; 2: double */
  1257. expanded = w_newword(&exp_len, &exp_maxl);
  1258. for (p = pattern; p && *p; p++) {
  1259. size_t offset;
  1260. switch (*p) {
  1261. case '"':
  1262. if (quoted == 2)
  1263. quoted = 0;
  1264. else if (quoted == 0)
  1265. quoted = 2;
  1266. else
  1267. break;
  1268. continue;
  1269. case '\'':
  1270. if (quoted == 1)
  1271. quoted = 0;
  1272. else if (quoted == 0)
  1273. quoted = 1;
  1274. else
  1275. break;
  1276. continue;
  1277. case '*':
  1278. case '?':
  1279. if (quoted) {
  1280. /* Convert quoted wildchar to escaped wildchar. */
  1281. expanded = w_addchar(expanded, &exp_len,
  1282. &exp_maxl, '\\');
  1283. if (expanded == NULL)
  1284. goto no_space;
  1285. }
  1286. break;
  1287. case '$':
  1288. offset = 0;
  1289. error = parse_dollars(&expanded, &exp_len, &exp_maxl, p,
  1290. &offset, flags, NULL, NULL, NULL, 1);
  1291. if (error) {
  1292. if (free_value)
  1293. free(value);
  1294. free(expanded);
  1295. goto do_error;
  1296. }
  1297. p += offset;
  1298. continue;
  1299. case '~':
  1300. if (quoted || exp_len)
  1301. break;
  1302. offset = 0;
  1303. error = parse_tilde(&expanded, &exp_len, &exp_maxl, p,
  1304. &offset, 0);
  1305. if (error) {
  1306. if (free_value)
  1307. free(value);
  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. free(pattern);
  1325. pattern = expanded;
  1326. }
  1327. switch (action) {
  1328. case ACT_RP_SHORT_LEFT:
  1329. case ACT_RP_LONG_LEFT:
  1330. case ACT_RP_SHORT_RIGHT:
  1331. case ACT_RP_LONG_RIGHT:
  1332. {
  1333. char *p;
  1334. char c;
  1335. char *end;
  1336. if (value == NULL || pattern == NULL || *pattern == '\0')
  1337. break;
  1338. end = value + strlen(value);
  1339. switch (action) {
  1340. case ACT_RP_SHORT_LEFT:
  1341. for (p = value; p <= end; ++p) {
  1342. c = *p;
  1343. *p = '\0';
  1344. if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
  1345. *p = c;
  1346. if (free_value) {
  1347. char *newval = strdup(p);
  1348. if (newval == NULL) {
  1349. free(value);
  1350. goto no_space;
  1351. }
  1352. free(value);
  1353. value = newval;
  1354. } else
  1355. value = p;
  1356. break;
  1357. }
  1358. *p = c;
  1359. }
  1360. break;
  1361. case ACT_RP_LONG_LEFT:
  1362. for (p = end; p >= value; --p) {
  1363. c = *p;
  1364. *p = '\0';
  1365. if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
  1366. *p = c;
  1367. if (free_value) {
  1368. char *newval = strdup(p);
  1369. if (newval == NULL) {
  1370. free(value);
  1371. goto no_space;
  1372. }
  1373. free(value);
  1374. value = newval;
  1375. } else
  1376. value = p;
  1377. break;
  1378. }
  1379. *p = c;
  1380. }
  1381. break;
  1382. case ACT_RP_SHORT_RIGHT:
  1383. for (p = end; p >= value; --p) {
  1384. if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {
  1385. char *newval;
  1386. newval = malloc(p - value + 1);
  1387. if (newval == NULL) {
  1388. if (free_value)
  1389. free(value);
  1390. goto no_space;
  1391. }
  1392. *(char *) mempcpy(newval, value, p - value) = '\0';
  1393. if (free_value)
  1394. free(value);
  1395. value = newval;
  1396. free_value = 1;
  1397. break;
  1398. }
  1399. }
  1400. break;
  1401. case ACT_RP_LONG_RIGHT:
  1402. for (p = value; p <= end; ++p) {
  1403. if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {
  1404. char *newval;
  1405. newval = malloc(p - value + 1);
  1406. if (newval == NULL) {
  1407. if (free_value)
  1408. free(value);
  1409. goto no_space;
  1410. }
  1411. *(char *) mempcpy(newval, value, p - value) = '\0';
  1412. if (free_value)
  1413. free(value);
  1414. value = newval;
  1415. free_value = 1;
  1416. break;
  1417. }
  1418. }
  1419. break;
  1420. default:
  1421. break;
  1422. }
  1423. break;
  1424. }
  1425. case ACT_NULL_ERROR:
  1426. if (value && *value)
  1427. /* Substitute parameter */
  1428. break;
  1429. error = 0;
  1430. if (!colon_seen && value)
  1431. /* Substitute NULL */
  1432. ;
  1433. else if (*pattern)
  1434. fprintf(stderr, "%s: %s\n", env, pattern);
  1435. else {
  1436. fprintf(stderr, "%s: parameter null or not set\n", env);
  1437. error = WRDE_BADVAL;
  1438. }
  1439. if (free_value)
  1440. free(value);
  1441. goto do_error;
  1442. case ACT_NULL_SUBST:
  1443. if (value && *value)
  1444. /* Substitute parameter */
  1445. break;
  1446. if (free_value)
  1447. free(value);
  1448. if (!colon_seen && value)
  1449. /* Substitute NULL */
  1450. goto success;
  1451. value = pattern ? strdup(pattern) : pattern;
  1452. free_value = 1;
  1453. if (pattern && !value)
  1454. goto no_space;
  1455. break;
  1456. case ACT_NONNULL_SUBST:
  1457. if (value && (*value || !colon_seen)) {
  1458. if (free_value)
  1459. free(value);
  1460. value = pattern ? strdup(pattern) : pattern;
  1461. free_value = 1;
  1462. if (pattern && !value)
  1463. goto no_space;
  1464. break;
  1465. }
  1466. /* Substitute NULL */
  1467. if (free_value)
  1468. free(value);
  1469. goto success;
  1470. case ACT_NULL_ASSIGN:
  1471. if (value && *value)
  1472. /* Substitute parameter */
  1473. break;
  1474. if (!colon_seen && value) {
  1475. /* Substitute NULL */
  1476. if (free_value)
  1477. free(value);
  1478. goto success;
  1479. }
  1480. if (free_value)
  1481. free(value);
  1482. value = pattern ? strdup(pattern) : pattern;
  1483. free_value = 1;
  1484. if (pattern && !value)
  1485. goto no_space;
  1486. setenv(env, value, 1);
  1487. break;
  1488. default:
  1489. assert(!"Unrecognised action!");
  1490. }
  1491. }
  1492. free(env);
  1493. env = NULL;
  1494. free(pattern);
  1495. pattern = NULL;
  1496. if (seen_hash) {
  1497. char param_length[21];
  1498. param_length[20] = '\0';
  1499. *word = w_addstr(*word, word_length, max_length,
  1500. _itoa(value ? strlen(value) : 0,
  1501. &param_length[20]));
  1502. if (free_value) {
  1503. assert(value != NULL);
  1504. free(value);
  1505. }
  1506. return *word ? 0 : WRDE_NOSPACE;
  1507. }
  1508. if (value == NULL)
  1509. return 0;
  1510. if (quoted || !pwordexp) {
  1511. /* Quoted - no field split */
  1512. *word = w_addstr(*word, word_length, max_length, value);
  1513. if (free_value)
  1514. free(value);
  1515. return *word ? 0 : WRDE_NOSPACE;
  1516. } else {
  1517. /* Need to field-split */
  1518. char *value_copy = strdup(value); /* Don't modify value */
  1519. char *field_begin = value_copy;
  1520. int seen_nonws_ifs = 0;
  1521. if (free_value)
  1522. free(value);
  1523. if (value_copy == NULL)
  1524. goto no_space;
  1525. do {
  1526. char *field_end = field_begin;
  1527. char *next_field;
  1528. /* If this isn't the first field, start a new word */
  1529. if (field_begin != value_copy) {
  1530. if (w_addword(pwordexp, *word) == WRDE_NOSPACE) {
  1531. free(value_copy);
  1532. goto no_space;
  1533. }
  1534. *word = w_newword(word_length, max_length);
  1535. }
  1536. /* Skip IFS whitespace before the field */
  1537. field_begin += strspn(field_begin, ifs_white);
  1538. if (!seen_nonws_ifs && *field_begin == 0)
  1539. /* Nothing but whitespace */
  1540. break;
  1541. /* Search for the end of the field */
  1542. field_end = field_begin + strcspn(field_begin, ifs);
  1543. /* Set up pointer to the character after end of field and
  1544. skip whitespace IFS after it. */
  1545. next_field = field_end + strspn(field_end, ifs_white);
  1546. /* Skip at most one non-whitespace IFS character after the field */
  1547. seen_nonws_ifs = 0;
  1548. if (*next_field && strchr(ifs, *next_field)) {
  1549. seen_nonws_ifs = 1;
  1550. next_field++;
  1551. }
  1552. /* Null-terminate it */
  1553. *field_end = 0;
  1554. /* Tag a copy onto the current word */
  1555. *word = w_addstr(*word, word_length, max_length, field_begin);
  1556. if (*word == NULL && *field_begin != '\0') {
  1557. free(value_copy);
  1558. goto no_space;
  1559. }
  1560. field_begin = next_field;
  1561. }
  1562. while (seen_nonws_ifs || *field_begin);
  1563. free(value_copy);
  1564. }
  1565. return 0;
  1566. success:
  1567. error = 0;
  1568. goto do_error;
  1569. no_space:
  1570. error = WRDE_NOSPACE;
  1571. goto do_error;
  1572. syntax:
  1573. error = WRDE_SYNTAX;
  1574. do_error:
  1575. free(env);
  1576. free(pattern);
  1577. return error;
  1578. }
  1579. #else
  1580. static __inline__ int
  1581. parse_backtick(char **word, size_t * word_length, size_t * max_length,
  1582. const char *words, size_t * offset, int flags,
  1583. wordexp_t * pwordexp, const char *ifs,
  1584. const char *ifs_white)
  1585. {
  1586. return 0;
  1587. }
  1588. #endif
  1589. static int
  1590. parse_dollars(char **word, size_t * word_length, size_t * max_length,
  1591. const char *words, size_t * offset, int flags,
  1592. wordexp_t * pwordexp, const char *ifs, const char *ifs_white,
  1593. int quoted)
  1594. {
  1595. /* We are poised _at_ "$" */
  1596. switch (words[1 + *offset]) {
  1597. case '"':
  1598. case '\'':
  1599. case 0:
  1600. *word = w_addchar(*word, word_length, max_length, '$');
  1601. return *word ? 0 : WRDE_NOSPACE;
  1602. #ifdef __WORDEXP_FULL
  1603. case '(':
  1604. if (words[2 + *offset] == '(') {
  1605. /* Differentiate between $((1+3)) and $((echo);(ls)) */
  1606. int i = 3 + *offset;
  1607. int depth = 0;
  1608. while (words[i] && !(depth == 0 && words[i] == ')')) {
  1609. if (words[i] == '(')
  1610. ++depth;
  1611. else if (words[i] == ')')
  1612. --depth;
  1613. ++i;
  1614. }
  1615. if (words[i] == ')' && words[i + 1] == ')') {
  1616. (*offset) += 3;
  1617. /* Call parse_arith -- 0 is for "no brackets" */
  1618. return parse_arith(word, word_length, max_length, words,
  1619. offset, flags, 0);
  1620. }
  1621. }
  1622. if (flags & WRDE_NOCMD)
  1623. return WRDE_CMDSUB;
  1624. (*offset) += 2;
  1625. return parse_comm(word, word_length, max_length, words, offset,
  1626. flags, quoted ? NULL : pwordexp, ifs, ifs_white);
  1627. case '[':
  1628. (*offset) += 2;
  1629. /* Call parse_arith -- 1 is for "brackets" */
  1630. return parse_arith(word, word_length, max_length, words, offset,
  1631. flags, 1);
  1632. case '{':
  1633. default:
  1634. ++(*offset); /* parse_param needs to know if "{" is there */
  1635. return parse_param(word, word_length, max_length, words, offset,
  1636. flags, pwordexp, ifs, ifs_white, quoted);
  1637. #else
  1638. default:
  1639. ++(*offset); /* parse_param needs to know if "{" is there */
  1640. return 0;
  1641. #endif
  1642. }
  1643. }
  1644. static int
  1645. parse_dquote(char **word, size_t * word_length, size_t * max_length,
  1646. const char *words, size_t * offset, int flags,
  1647. wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
  1648. {
  1649. /* We are poised just after a double-quote */
  1650. int error;
  1651. for (; words[*offset]; ++(*offset)) {
  1652. switch (words[*offset]) {
  1653. case '"':
  1654. return 0;
  1655. case '$':
  1656. error = parse_dollars(word, word_length, max_length, words, offset,
  1657. flags, pwordexp, ifs, ifs_white, 1);
  1658. /* The ``1'' here is to tell parse_dollars not to
  1659. * split the fields. It may need to, however ("$@").
  1660. */
  1661. if (error)
  1662. return error;
  1663. break;
  1664. case '`':
  1665. if (flags & WRDE_NOCMD)
  1666. return WRDE_CMDSUB;
  1667. ++(*offset);
  1668. error = parse_backtick(word, word_length, max_length, words,
  1669. offset, flags, NULL, NULL, NULL);
  1670. /* The first NULL here is to tell parse_backtick not to
  1671. * split the fields.
  1672. */
  1673. if (error)
  1674. return error;
  1675. break;
  1676. case '\\':
  1677. error = parse_qtd_backslash(word, word_length, max_length, words,
  1678. offset);
  1679. if (error)
  1680. return error;
  1681. break;
  1682. default:
  1683. *word = w_addchar(*word, word_length, max_length, words[*offset]);
  1684. if (*word == NULL)
  1685. return WRDE_NOSPACE;
  1686. }
  1687. }
  1688. /* Unterminated string */
  1689. return WRDE_SYNTAX;
  1690. }
  1691. /*
  1692. * wordfree() is to be called after pwordexp is finished with.
  1693. */
  1694. void wordfree(wordexp_t * pwordexp)
  1695. {
  1696. /* wordexp can set pwordexp to NULL */
  1697. if (pwordexp && pwordexp->we_wordv) {
  1698. char **wordv = pwordexp->we_wordv;
  1699. for (wordv += pwordexp->we_offs; *wordv; ++wordv)
  1700. free(*wordv);
  1701. free(pwordexp->we_wordv);
  1702. pwordexp->we_wordv = NULL;
  1703. }
  1704. }
  1705. libc_hidden_def(wordfree)
  1706. /*
  1707. * wordexp()
  1708. */
  1709. int wordexp(const char *words, wordexp_t * we, int flags)
  1710. {
  1711. size_t words_offset;
  1712. size_t word_length;
  1713. size_t max_length;
  1714. char *word = w_newword(&word_length, &max_length);
  1715. int error;
  1716. char *ifs;
  1717. char ifs_white[4];
  1718. wordexp_t old_word = *we;
  1719. if (flags & WRDE_REUSE) {
  1720. /* Minimal implementation of WRDE_REUSE for now */
  1721. wordfree(we);
  1722. old_word.we_wordv = NULL;
  1723. }
  1724. if ((flags & WRDE_APPEND) == 0) {
  1725. we->we_wordc = 0;
  1726. if (flags & WRDE_DOOFFS) {
  1727. we->we_wordv = calloc(1 + we->we_offs, sizeof(char *));
  1728. if (we->we_wordv == NULL) {
  1729. error = WRDE_NOSPACE;
  1730. goto do_error;
  1731. }
  1732. } else {
  1733. we->we_wordv = calloc(1, sizeof(char *));
  1734. if (we->we_wordv == NULL) {
  1735. error = WRDE_NOSPACE;
  1736. goto do_error;
  1737. }
  1738. we->we_offs = 0;
  1739. }
  1740. }
  1741. /* Find out what the field separators are.
  1742. * There are two types: whitespace and non-whitespace.
  1743. */
  1744. ifs = getenv("IFS");
  1745. if (!ifs)
  1746. /* IFS unset - use <space><tab><newline>. */
  1747. ifs = strcpy(ifs_white, " \t\n");
  1748. else {
  1749. char *ifsch = ifs;
  1750. char *whch = ifs_white;
  1751. /* Start off with no whitespace IFS characters */
  1752. ifs_white[0] = '\0';
  1753. while (*ifsch != '\0') {
  1754. if ((*ifsch == ' ') || (*ifsch == '\t') || (*ifsch == '\n')) {
  1755. /* Whitespace IFS. See first whether it is already in our
  1756. collection. */
  1757. char *runp = ifs_white;
  1758. while (runp < whch && *runp != '\0' && *runp != *ifsch)
  1759. ++runp;
  1760. if (runp == whch)
  1761. *whch++ = *ifsch;
  1762. }
  1763. ++ifsch;
  1764. }
  1765. *whch = '\0';
  1766. }
  1767. for (words_offset = 0; words[words_offset]; ++words_offset)
  1768. switch (words[words_offset]) {
  1769. case '\\':
  1770. error = parse_backslash(&word, &word_length, &max_length, words,
  1771. &words_offset);
  1772. if (error)
  1773. goto do_error;
  1774. break;
  1775. case '$':
  1776. error = parse_dollars(&word, &word_length, &max_length, words,
  1777. &words_offset, flags, we, ifs, ifs_white,
  1778. 0);
  1779. if (error)
  1780. goto do_error;
  1781. break;
  1782. case '`':
  1783. if (flags & WRDE_NOCMD) {
  1784. error = WRDE_CMDSUB;
  1785. goto do_error;
  1786. }
  1787. ++words_offset;
  1788. error = parse_backtick(&word, &word_length, &max_length, words,
  1789. &words_offset, flags, we, ifs,
  1790. ifs_white);
  1791. if (error)
  1792. goto do_error;
  1793. break;
  1794. case '"':
  1795. ++words_offset;
  1796. error = parse_dquote(&word, &word_length, &max_length, words,
  1797. &words_offset, flags, we, ifs, ifs_white);
  1798. if (error)
  1799. goto do_error;
  1800. if (!word_length) {
  1801. error = w_addword(we, NULL);
  1802. if (error)
  1803. return error;
  1804. }
  1805. break;
  1806. case '\'':
  1807. ++words_offset;
  1808. error = parse_squote(&word, &word_length, &max_length, words,
  1809. &words_offset);
  1810. if (error)
  1811. goto do_error;
  1812. if (!word_length) {
  1813. error = w_addword(we, NULL);
  1814. if (error)
  1815. return error;
  1816. }
  1817. break;
  1818. case '~':
  1819. error = parse_tilde(&word, &word_length, &max_length, words,
  1820. &words_offset, we->we_wordc);
  1821. if (error)
  1822. goto do_error;
  1823. break;
  1824. case '*':
  1825. case '[':
  1826. case '?':
  1827. error = parse_glob(&word, &word_length, &max_length, words,
  1828. &words_offset, flags, we, ifs, ifs_white);
  1829. if (error)
  1830. goto do_error;
  1831. break;
  1832. default:
  1833. /* Is it a word separator? */
  1834. if (strchr(" \t", words[words_offset]) == NULL) {
  1835. char ch = words[words_offset];
  1836. /* Not a word separator -- but is it a valid word char? */
  1837. if (strchr("\n|&;<>(){}", ch)) {
  1838. /* Fail */
  1839. error = WRDE_BADCHAR;
  1840. goto do_error;
  1841. }
  1842. /* "Ordinary" character -- add it to word */
  1843. word = w_addchar(word, &word_length, &max_length, ch);
  1844. if (word == NULL) {
  1845. error = WRDE_NOSPACE;
  1846. goto do_error;
  1847. }
  1848. break;
  1849. }
  1850. /* If a word has been delimited, add it to the list. */
  1851. if (word != NULL) {
  1852. error = w_addword(we, word);
  1853. if (error)
  1854. goto do_error;
  1855. }
  1856. word = w_newword(&word_length, &max_length);
  1857. }
  1858. /* End of string */
  1859. /* There was a word separator at the end */
  1860. if (word == NULL) /* i.e. w_newword */
  1861. return 0;
  1862. /* There was no field separator at the end */
  1863. return w_addword(we, word);
  1864. do_error:
  1865. /* Error:
  1866. * free memory used (unless error is WRDE_NOSPACE), and
  1867. * set we members back to what they were.
  1868. */
  1869. free(word);
  1870. if (error == WRDE_NOSPACE)
  1871. return WRDE_NOSPACE;
  1872. if ((flags & WRDE_APPEND) == 0)
  1873. wordfree(we);
  1874. *we = old_word;
  1875. return error;
  1876. }